Skip to content

Commit b116e37

Browse files
committed
🔨 Add new features like graphql with gqlgen, gin framework, finished restful, update README and docker-compose for just only command
1 parent 2af620e commit b116e37

File tree

26 files changed

+831
-108
lines changed

26 files changed

+831
-108
lines changed

README.md

Lines changed: 213 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,126 @@
1-
# example-golang
1+
# Example Golang
22

33
Golang, GraphQL, Resful, Auth JWT example and much more.
44

5-
##
5+
A example of bank transaction, made with GOlang, Gin Framework, Restful and GraphQL.
66

7+
- [Endpoints](#Endpoints)
8+
- [Insomnia](#Insomnia)
9+
- [Get Started](#Get-Started)
10+
- [Get Started - without docker](##Get-Started-without-docker)
11+
- [Run Debug with VScode and DB in docker](##Run-Debugg)
12+
- [Unity Test and Test Integration](##Unity-Test-and-Test-Integration)
713
- [Step By Step](#Step-By-Step)
14+
- [Commands](#Commands)
15+
- [Technologies](#Technologies)
16+
- [References](#References)
17+
18+
## Endpoints
19+
20+
| Endpoint | Method | Action |
21+
| ---------- | ------ | ------- |
22+
| /graphql | POST | |
23+
| /graphiql | GET | |
24+
| /users | GET | Index |
25+
| /users | POST | Store |
26+
| /users | PATCH | Update |
27+
| /users/:id | DELETE | Destroy |
28+
| /users/:id | GET | Show |
29+
30+
## Insomnia
31+
32+
If you use Insomnia, just import a [insomnia-v4.yaml](insomnia-v4.yaml)
33+
34+
## Get Started
35+
36+
Just one command
37+
38+
```bash
39+
docker-compose up --build
40+
```
41+
42+
> Open on you best browser: http://localhost:8085 <br />
43+
> GraphQL Playground: http://localhost:8085/gaphiql<br />
44+
> To see a database open adminer on: http://localhost:8086 <br />
45+
> Configurations for connect on database see: [/docker/local.env](/docker/local.env)
46+
> IMPORTANT! If you not have a docker see: [Get Started - without docker](#Get-Stared-without-docker)
47+
48+
## Structure
49+
50+
```
51+
├── config - (configuration)
52+
├── controllers (RESTfull methods)
53+
├── core (Base methos for controllers, models and services)
54+
├── database - (database configuration, migrate and seed data)
55+
├── graphql
56+
│ ├── generated - A package that only contains the generated runtime
57+
│ │ └── generated.go
58+
│ ├── model - A package for all your graph models, generated or otherwise
59+
│ │ └── models_gen.go
60+
│ ├── resolver.go - The root graph resolver type. This file wont get regenerated
61+
│ ├── schema.graphqls - Some schema. You can split the schema into as many graphql files as you like
62+
│ └── schema.resolvers.go - the resolver implementation for schema.graphql
63+
├── models (ORM models based on Database tables)
64+
├── routes (files with routes)
65+
├── services (with business rules and intermediate a database)controlling the generated code.
66+
├── go.mod
67+
├── go.sum
68+
├── gqlgen.yml - The gqlgen config file, knobs for
69+
└── main.go - The entry point to your app. Customize it however you see fit
70+
```
71+
72+
## Commands
73+
74+
Enter on docker container to exec any command.
75+
76+
```bash
77+
docker exec -it labbankgo-api /bin/bash
78+
```
79+
80+
To generate graphql files.
81+
82+
```bash
83+
gqlgen generate
84+
```
85+
86+
> On docker
87+
88+
```bash
89+
~/go/bin/gqlgen generate
90+
```
91+
92+
> Out of docker
93+
94+
#### Get Started without docker
95+
96+
> A Database PostgreSQL is needed, configurations for it in docker/local.env
97+
98+
```bash
99+
gin --port=8080 #or ~/go/bin/gin --port=8080
100+
```
101+
102+
## Run Debugg
103+
104+
> With VScode and SGBD on Docker
105+
106+
```bash
107+
docker-compose -f docker-compose-db.yml up
108+
```
109+
110+
> Open your debug on VScode and run "Launch file"
111+
112+
## Unity Test and Test Integration
113+
114+
```bash
115+
go test -v ./src/tests/
116+
```
117+
118+
With code coverage
119+
120+
```bash
121+
go test -cover -coverprofile=c.out ./src/tests/
122+
go tool cover -html=c.out -o coverage.html
123+
```
8124

9125
## Step By Step
10126

@@ -45,6 +161,40 @@ Run to see you first hello world
45161
go run main.go
46162
```
47163

164+
### Install Gin Framework
165+
166+
```bash
167+
go get github.com/codegangsta/gin
168+
```
169+
170+
Update a file `main.go` with this example code
171+
172+
```bash
173+
package main
174+
175+
import (
176+
"net/http"
177+
178+
"github.com/gin-gonic/gin"
179+
)
180+
181+
func setupRouter() *gin.Engine {
182+
r := gin.Default()
183+
184+
r.GET("/:name", func(c *gin.Context) {
185+
name := c.Params.ByName("name")
186+
c.String(http.StatusOK, "Hello World", name)
187+
})
188+
189+
return r
190+
}
191+
192+
func main() {
193+
r := setupRouter()
194+
r.Run(":8080")
195+
}
196+
```
197+
48198
### Install GraphQL
49199

50200
```bash
@@ -62,3 +212,64 @@ gqlgen generate
62212
```bash
63213
~/go/bin/gqlgen generate
64214
```
215+
216+
Install GORM
217+
218+
```bash
219+
go get -u gorm.io/gorm && \
220+
go get -u gorm.io/driver/postgres
221+
```
222+
223+
Start project with Hot Reload
224+
225+
```bash
226+
~/go/bin/gin
227+
```
228+
229+
Default start project
230+
231+
```bash
232+
go run main.go
233+
```
234+
235+
> Open on you best browser: http://localhost:8085
236+
237+
## Run Debugg
238+
239+
> With VScode and SGBD on Docker
240+
241+
```bash
242+
docker-compose -f docker-compose-db.yml up
243+
```
244+
245+
## Unity Test and Test Integration
246+
247+
```bash
248+
go test -v ./src/tests/
249+
```
250+
251+
With code coverage
252+
253+
```bash
254+
go test -cover -coverprofile=c.out ./src/tests/
255+
go tool cover -html=c.out -o coverage.html
256+
```
257+
258+
## Technologies:
259+
260+
- [Golang (Language)](https://golang.org)
261+
- [GORM (ORM)](https://gorm.io/docs/index.html)
262+
- [Gqlgen (GraphQL)](https://gqlgen.com)
263+
- [PostgreSQL (SGBD)](https://www.postgresql.org/docs/online-resources/)
264+
- [Docker Compose (Local Environment)](https://docs.docker.com/compose/compose-file/)
265+
266+
## References
267+
268+
- [Effective GO](https://golang.org/doc/effective_go.html)
269+
- [JsonAPI (Restful API Specification)](https://jsonapi.org)
270+
- [Style Guide - GOlang Code Review](https://github.com/golang/go/wiki/CodeReviewComments)
271+
- [Style Guide - Google](https://google.github.io/styleguide/)
272+
- [Style Guide - Uber GO](https://github.com/uber-go/guide/blob/master/style.md/)
273+
- [Style Guide - Source Graph](https://about.sourcegraph.com/handbook/engineering/languages/go/)
274+
- [How to GraphQL with GO](https://www.howtographql.com/graphql-go/0-introduction/)
275+
- [Examples with GO](https://gobyexample.com/)

controllers/user.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/ruyjfs/example-golang/config"
9+
"github.com/ruyjfs/example-golang/core"
10+
"github.com/ruyjfs/example-golang/models"
11+
"github.com/ruyjfs/example-golang/services"
12+
)
13+
14+
type User struct {
15+
core.Controller
16+
}
17+
18+
func (a *User) Index(c *gin.Context) {
19+
var params *models.User
20+
c.BindJSON(&params)
21+
// c.ShouldBind(&params)
22+
result, list := new(services.Users).All(params)
23+
a.Json(c, result, list)
24+
}
25+
26+
func (a *User) Store(c *gin.Context) {
27+
var params *models.User
28+
c.BindJSON(&params)
29+
result, data := new(services.Users).Create(params)
30+
a.Json(c, result, data)
31+
}
32+
33+
func (a *User) Update(c *gin.Context) {
34+
var params *models.User
35+
c.BindJSON(&params)
36+
result, data := new(services.Users).Update(params)
37+
a.Json(c, result, data)
38+
}
39+
40+
func (a *User) Destroy(c *gin.Context) {
41+
id, err := strconv.Atoi(c.Params.ByName("id"))
42+
if err == nil {
43+
result, data := new(services.Users).Delete(id)
44+
a.Json(c, result, data)
45+
return
46+
}
47+
a.JsonFail(c, http.StatusBadRequest, "Not have params")
48+
}
49+
50+
func (a *User) Show(c *gin.Context) {
51+
var account models.User
52+
result := config.Db().First(&account, c.Params.ByName("id"))
53+
a.Json(c, result, account)
54+
}

core/controller.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package core
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"gorm.io/gorm"
8+
)
9+
10+
type Controller struct {
11+
}
12+
13+
func (controller *Controller) Json(c *gin.Context, result *gorm.DB, data interface{}) {
14+
if result.Error == nil {
15+
controller.JsonSuccess(c, http.StatusOK, gin.H{"data": data})
16+
return
17+
}
18+
controller.JsonFail(c, http.StatusBadRequest, result.Error.Error())
19+
return
20+
}
21+
22+
func (controller *Controller) JsonSuccess(c *gin.Context, status int, h gin.H) {
23+
h["status"] = "success"
24+
c.JSON(status, h)
25+
return
26+
}
27+
28+
func (controller *Controller) JsonFail(c *gin.Context, status int, message string) {
29+
c.JSON(status, gin.H{
30+
"status": "fail",
31+
"message": message,
32+
"data": nil,
33+
"code": status,
34+
})
35+
}

core/model.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package core
22

3-
import "time"
3+
import (
4+
"time"
5+
6+
"github.com/ruyjfs/example-golang/config"
7+
"gorm.io/gorm"
8+
)
49

510
type Model struct {
611
ID int `json:"id" gorm:"primarykey"`
712
CreatedAt time.Time `json:"createdAt,omitempty"`
813
UpdatedAt time.Time `json:"updatedAt,omitempty"`
914
DeletedAt *time.Time `json:"deletedAt,omitempty" gorm:"index"`
1015
}
16+
17+
func (m *Model) Db() *gorm.DB {
18+
return config.Db()
19+
}

core/resolver.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package core
2+
3+
import (
4+
"github.com/ruyjfs/example-golang/config"
5+
"gorm.io/gorm"
6+
)
7+
8+
type Resolver struct {
9+
}
10+
11+
func (r *Resolver) Db() *gorm.DB {
12+
return config.Db()
13+
}

core/service.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package core
22

3+
import (
4+
"github.com/ruyjfs/example-golang/config"
5+
"gorm.io/gorm"
6+
)
7+
38
type Service struct {
49
}
510

11+
func (m *Service) Db() *gorm.DB {
12+
return config.Db()
13+
}
14+
615
func (s *Service) Find(m interface{}) {
716

817
}

database/seed.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package database
2+
3+
import (
4+
"log"
5+
6+
"github.com/ruyjfs/example-golang/database/seeds"
7+
)
8+
9+
func Seeder() {
10+
log.Printf("Seed: Start")
11+
seeds.Perfils()
12+
log.Printf("Seed: Success")
13+
}

0 commit comments

Comments
 (0)