Skip to content

Commit 8cec300

Browse files
Merge pull request #17 from laironacosta/develop
Develop
2 parents 30c5383 + a89ee66 commit 8cec300

File tree

10 files changed

+194
-128
lines changed

10 files changed

+194
-128
lines changed

Diff for: .env

-5
This file was deleted.

Diff for: .github/workflows/aws.yml

-81
This file was deleted.

Diff for: Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ COPY ./migrations ./migrations
3434

3535
# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
3636
COPY --from=builder /app/main .
37-
COPY --from=builder /app/.env .
3837

3938
# Expose port 8080 to the outside world
4039
EXPOSE 8080

Diff for: README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,75 @@
1-
# ms-gin-go
1+
# Microservice: Gin Framework Example
2+
This project shows the use of Gin Framework in Golang.
3+
4+
This repository runs a simple main daemon (main) that implements a REST API for users. The daemon uses a postgres database to persist data and after creation uses migrations to create the user schema.
5+
6+
## Table of Contents
7+
8+
- [Running](#running)
9+
- [Dependencies](#dependencies)
10+
- [Environment Variables](#environment-variables)
11+
- [Make Rule](#make-rule)
12+
- [Testing](#testing)
13+
- Coming soon
14+
15+
## Running
16+
17+
### Dependencies
18+
19+
The only dependencies to run the services in this repository are:
20+
21+
- `docker`
22+
- `docker-compose`
23+
24+
### Environment Variables
25+
26+
The program looks for the following environment variables:
27+
28+
- `DB_USER`: The postgres database username that gets used within the postgres connection
29+
string (Default: `root`).
30+
- `DB_PASS`: The postgres database password that gets used within the postgres connection
31+
string (Default: `root`).
32+
- `DB_NAME`: The postgres database name that gets used within the postgres connection string
33+
(Default: `user`).
34+
- `DB_HOST`: The postgres database host name that gets used within the postgres connection
35+
string (Default `db`).
36+
- `DB_PORT`: The postgres database port that gets used within the postgres connection string
37+
(Default: `5432`).
38+
39+
If the environment variable has a supplied default and none are set within the context of the host
40+
machine, then the default will be used.
41+
42+
To set any given environment variable, simply execute the following
43+
pattern, replacing `[ENV_NAME]` with the name of the environment variable and `[ENV_VALUE]` with the
44+
desired value of the environment variable: `export [ENV_NAME]=[ENV_VALUE]`. To unset any set environment
45+
variable, simply execute the following pattern, replacing `[ENV_NAME]` with the name of the environment
46+
variable: `unset [ENV_NAME]`.
47+
48+
### Make Rule
49+
50+
To run the services simply execute the following command:
51+
52+
```shell
53+
make run
54+
```
55+
56+
This will stop any containers defined by the compose file if already running
57+
and then rebuild the containers using the compose file. The main daemon (`main`)
58+
will be available at `localhost:8080` and the postgres instance will be available
59+
at `localhost:5432`.
60+
61+
To stop the services simply execute the following command:
62+
63+
```shell
64+
make stop
65+
```
66+
67+
To down the services simply execute the following command:
68+
69+
```shell
70+
make down
71+
```
72+
73+
## Testing
74+
75+
Coming soon

Diff for: database/database.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"github.com/go-pg/pg/v10"
6+
log "github.com/sirupsen/logrus"
7+
"sync"
8+
"time"
9+
)
10+
11+
var (
12+
instance *pg.DB
13+
once sync.Once
14+
)
15+
16+
func NewPgDB(pgOptions *pg.Options) *pg.DB {
17+
once.Do(func() {
18+
instance = Connect(pgOptions)
19+
})
20+
21+
return instance
22+
}
23+
24+
func Connect(pgOptions *pg.Options) *pg.DB {
25+
log.Info("connecting to postgres database...")
26+
ctx := context.Background()
27+
db := pg.Connect(pgOptions)
28+
29+
log.Info("verifying postgres connection...")
30+
if err := db.Ping(ctx); err != nil {
31+
log.Errorln(err)
32+
time.Sleep(2 * time.Second)
33+
34+
db = pg.Connect(pgOptions)
35+
if err := db.Ping(ctx); err == nil {
36+
log.Info("successfully connected to postgres database...")
37+
return db
38+
}
39+
40+
log.Panicf("Postgres connection error %+v\n", err)
41+
}
42+
43+
log.Info("Connection to postgres verified...")
44+
return db
45+
}

Diff for: docker-compose.yml

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
11
version: '3'
2+
23
networks:
34
ms-go-network:
45
driver: bridge
6+
57
services:
6-
db-postgres:
7-
image: postgres:latest
8-
container_name: full_db_postgres
9-
environment:
10-
- POSTGRES_USER=${DB_USER}
11-
- POSTGRES_PASSWORD=${DB_PASSWORD}
12-
- POSTGRES_DB=${DB_NAME}
8+
app:
9+
build:
10+
context: .
11+
dockerfile: ./Dockerfile
1312
ports:
14-
- '5432:5432'
13+
- "8080:8080"
14+
depends_on:
15+
- db
1516
restart: on-failure
16-
volumes:
17-
- database_postgres:/var/lib/postgresql/data
1817
networks:
1918
- ms-go-network
20-
app:
21-
container_name: full_app
22-
build: .
19+
db:
20+
image: postgres:13.2
2321
ports:
24-
- 8080:8080
22+
- "5432:5432"
23+
expose:
24+
- "5432"
25+
environment:
26+
POSTGRES_USER: root
27+
POSTGRES_PASSWORD: root
28+
POSTGRES_DB: user
2529
restart: on-failure
26-
volumes:
27-
- api:/usr/src/app/
28-
env_file:
29-
- .env
30-
depends_on:
31-
- db-postgres
3230
networks:
33-
- ms-go-network
34-
volumes:
35-
api:
36-
database_postgres:
31+
- ms-go-network

Diff for: go.mod

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ module github.com/laironacosta/ms-gin-go
33
go 1.16
44

55
require (
6-
github.com/Lairon/db-go v0.0.0-20210328034946-ed191835cebb
76
github.com/gin-gonic/gin v1.6.3
87
github.com/go-pg/migrations/v8 v8.1.0
98
github.com/go-pg/pg/v10 v10.9.0
109
github.com/go-playground/validator/v10 v10.4.1 // indirect
1110
github.com/golang/protobuf v1.5.1 // indirect
1211
github.com/json-iterator/go v1.1.10 // indirect
12+
github.com/kelseyhightower/envconfig v1.4.0
1313
github.com/leodido/go-urn v1.2.1 // indirect
1414
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1515
github.com/modern-go/reflect2 v1.0.1 // indirect
16+
github.com/onsi/ginkgo v1.15.0 // indirect
17+
github.com/onsi/gomega v1.10.5 // indirect
18+
github.com/pkg/errors v0.9.1
19+
github.com/sirupsen/logrus v1.8.1
1620
github.com/ugorji/go v1.2.4 // indirect
17-
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 // indirect
21+
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54 // indirect
1822
gopkg.in/yaml.v2 v2.4.0 // indirect
1923
)

Diff for: go.sum

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3-
github.com/Lairon/db-go v0.0.0-20210328034946-ed191835cebb h1:TymkFrVlntF+wIx2ocKei4jHKn8+8tlDnpoSTDNp2MA=
4-
github.com/Lairon/db-go v0.0.0-20210328034946-ed191835cebb/go.mod h1:FZoBm6eGMtnp2kxzbrWqhzizi+4D41GJMJsYOtKHqis=
53
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
6-
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
74
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
85
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
96
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
107
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11-
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
128
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
139
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
1410
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
@@ -34,7 +30,6 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
3430
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
3531
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
3632
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
37-
github.com/go-redis/redis/v8 v8.8.0/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y=
3833
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
3934
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
4035
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -68,6 +63,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
6863
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
6964
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
7065
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
66+
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
67+
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
7168
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7269
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
7370
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
@@ -97,10 +94,15 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
9794
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
9895
github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ=
9996
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
97+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
98+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10099
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
101100
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
102101
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
102+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
103+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
103104
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
105+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
104106
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
105107
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
106108
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
@@ -177,6 +179,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
177179
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
178180
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
179181
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
182+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
180183
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
181184
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
182185
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -187,8 +190,8 @@ golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7w
187190
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
188191
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
189192
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
190-
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 h1:64ChN/hjER/taL4YJuA+gpLfIMT+/NFherRZixbxOhg=
191-
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
193+
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54 h1:rF3Ohx8DRyl8h2zw9qojyLHLhrJpEMgyPOImREEryf0=
194+
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
192195
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
193196
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
194197
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

0 commit comments

Comments
 (0)