1
- # example-golang
1
+ # Example Golang
2
2
3
3
Golang, GraphQL, Resful, Auth JWT example and much more.
4
4
5
- ##
5
+ A example of bank transaction, made with GOlang, Gin Framework, Restful and GraphQL.
6
6
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 )
7
13
- [ 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
+ ```
8
124
9
125
## Step By Step
10
126
@@ -45,6 +161,40 @@ Run to see you first hello world
45
161
go run main.go
46
162
```
47
163
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
+
48
198
### Install GraphQL
49
199
50
200
``` bash
@@ -62,3 +212,64 @@ gqlgen generate
62
212
``` bash
63
213
~ /go/bin/gqlgen generate
64
214
```
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/ )
0 commit comments