Skip to content

Commit 2af620e

Browse files
committed
🎉 First commit \o/.
1 parent de4950f commit 2af620e

26 files changed

+4155
-0
lines changed

.vscode/launch.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
// Use o IntelliSense para saber mais sobre os atributos possíveis.
3+
// Focalizar para exibir as descrições dos atributos existentes.
4+
// Para obter mais informações, acesse: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch file",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"program": "${workspaceFolder}/main.go",
13+
"envFile": "${workspaceFolder}/docker/local.env"
14+
},
15+
{
16+
"name": "Attach to Process",
17+
"type": "go",
18+
"request": "attach",
19+
"mode": "local",
20+
"processId": 0
21+
},
22+
{
23+
"name": "Connect to server",
24+
"type": "go",
25+
"request": "attach",
26+
"mode": "remote",
27+
"remotePath": "${workspaceFolder}",
28+
"port": 2345,
29+
"host": "127.0.0.1"
30+
},
31+
{
32+
"name": "Launch",
33+
"type": "go",
34+
"request": "launch",
35+
"mode": "auto",
36+
"program": "${fileDirname}",
37+
"env": {},
38+
"args": []
39+
}
40+
]
41+
}

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,64 @@
11
# example-golang
2+
23
Golang, GraphQL, Resful, Auth JWT example and much more.
4+
5+
##
6+
7+
- [Step By Step](#Step-By-Step)
8+
9+
## Step By Step
10+
11+
All steps necessary to make this example project.
12+
13+
### Create a mod init
14+
15+
All libs in Golang have a module name.
16+
This is for a package manage for Golang, like a npm/yarm for nodeJS, gradlew/mavem for Java, compose for PHP, mix for Elixir and many more.
17+
18+
```bash
19+
go mod init github.com/ruyjfs/example-golang
20+
```
21+
22+
> See: go.mod
23+
24+
### Create a file main.go - The Hello World
25+
26+
```bash
27+
echo '' >> main.go
28+
```
29+
30+
```go
31+
package main
32+
33+
import (
34+
"log"
35+
)
36+
37+
func main() {
38+
log.Printf("Hello World!")
39+
}
40+
```
41+
42+
Run to see you first hello world
43+
44+
```bash
45+
go run main.go
46+
```
47+
48+
### Install GraphQL
49+
50+
```bash
51+
go run github.com/99designs/gqlgen init
52+
```
53+
54+
To generate graphql files.
55+
56+
```bash
57+
gqlgen generate
58+
```
59+
60+
> On docker
61+
62+
```bash
63+
~/go/bin/gqlgen generate
64+
```

__debug_bin

15.9 MB
Binary file not shown.

config/db.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"gorm.io/driver/postgres"
8+
"gorm.io/gorm"
9+
)
10+
11+
func Db() *gorm.DB {
12+
dsn := fmt.Sprintf(
13+
"host=%s port=%s dbname=%s user=%s password=%s sslmode=disable TimeZone=%s",
14+
os.Getenv("DB_HOST"),
15+
os.Getenv("DB_PORT"),
16+
os.Getenv("DB_NAME"),
17+
os.Getenv("DB_USER"),
18+
os.Getenv("DB_PASSWORD"),
19+
os.Getenv("DB_TIMEZONE"),
20+
)
21+
22+
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
23+
if err != nil {
24+
panic("failed to connect database")
25+
}
26+
27+
return db
28+
}

controllers/index.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"html"
6+
"net/http"
7+
)
8+
9+
type Index struct {
10+
// core.Controller
11+
}
12+
13+
func (i *Index) Index(w http.ResponseWriter, r *http.Request) {
14+
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
15+
16+
}

core/model.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package core
2+
3+
import "time"
4+
5+
type Model struct {
6+
ID int `json:"id" gorm:"primarykey"`
7+
CreatedAt time.Time `json:"createdAt,omitempty"`
8+
UpdatedAt time.Time `json:"updatedAt,omitempty"`
9+
DeletedAt *time.Time `json:"deletedAt,omitempty" gorm:"index"`
10+
}

core/service.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package core
2+
3+
type Service struct {
4+
}
5+
6+
func (s *Service) Find(m interface{}) {
7+
8+
}
9+
10+
// func Min (type T Ordered) (a, b T) T {
11+
// if a < b {
12+
// return a
13+
// }
14+
// return b
15+
// }
16+
17+
// type List[type T] struct {
18+
// elems []T
19+
// }
20+
21+
// contract Sequence(T) {
22+
// T string, []byte
23+
// }

database/migrate.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package database
2+
3+
import (
4+
"log"
5+
6+
"github.com/ruyjfs/example-golang/config"
7+
"github.com/ruyjfs/example-golang/models"
8+
)
9+
10+
func Migrate() {
11+
log.Printf("Migrate: Start")
12+
db := config.Db()
13+
db.AutoMigrate(
14+
&models.User{},
15+
)
16+
log.Printf("Migrate: Success")
17+
}

docker-compose-db.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: "3.5"
2+
services:
3+
example-golang-db-service:
4+
container_name: example-golang-db-only
5+
image: postgres:alpine
6+
env_file: ./docker/local.env
7+
volumes:
8+
- ./docker/database/postgres/data:/var/lib/postgresql/data
9+
- ./docker/database/postgres/scripts:/docker-entrypoint-initdb.d
10+
ports:
11+
- "5435:5432"
12+
tty: true
13+
restart: always
14+
healthcheck:
15+
#IMPORTANT! change this if you have changed a local.env
16+
test: ["CMD-SHELL", "pg_isready -U example-golang-usr -d example-golang-db"]
17+
interval: 10s
18+
timeout: 5s
19+
retries: 5
20+
example-golang-adminer-service:
21+
container_name: example-golang-adminer-only
22+
image: adminer
23+
restart: always
24+
ports:
25+
- 8086:8080
26+
env_file: ./docker/local.env
27+
links:
28+
- example-golang-db-service
29+
depends_on:
30+
- example-golang-db-service

docker/database/postgres/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data

docker/database/postgres/scripts/.gitignore

Whitespace-only changes.

docker/docker.env

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DB_HOST="example-golang-db-service"
2+
DB_NAME="example-golang-db"
3+
DB_USER="example-golang-usr"
4+
DB_PASSWORD="example-golang-psw"
5+
DB_PORT="5432"
6+
DB_TIMEZONE="America/Recife"
7+
8+
POSTGRES_DB="${DB_NAME}"
9+
POSTGRES_USER="${DB_USER}"
10+
POSTGRES_PASSWORD="${DB_PASSWORD}"
11+
12+
ADMINER_DESIGN="nette"
13+
ADMINER_DEFAULT_SERVER="${DB_HOST}"

docker/local.env

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DB_HOST="localhost"
2+
DB_NAME="example-golang-db"
3+
DB_USER="example-golang-usr"
4+
DB_PASSWORD="example-golang-psw"
5+
DB_PORT="5435"
6+
DB_TIMEZONE="America/Recife"
7+
8+
POSTGRES_DB="${DB_NAME}"
9+
POSTGRES_USER="${DB_USER}"
10+
POSTGRES_PASSWORD="${DB_PASSWORD}"
11+
12+
ADMINER_DESIGN="nette"
13+
ADMINER_DEFAULT_SERVER="${DB_HOST}"

go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/ruyjfs/example-golang
2+
3+
go 1.15
4+
5+
require (
6+
github.com/99designs/gqlgen v0.13.0
7+
github.com/mholt/binding v0.3.0 // indirect
8+
github.com/ruyjfs/lab-bank-go v0.0.0-20201201022907-74dec24f28ee
9+
github.com/vektah/gqlparser/v2 v2.1.0
10+
gorm.io/driver/postgres v1.0.5
11+
gorm.io/gorm v1.20.8
12+
)

0 commit comments

Comments
 (0)