├── .gitignore ├── Dockerfile ├── database ├── data.json └── database.go ├── go.mod ├── go.sum └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:alpine AS builder 2 | 3 | # Set necessary environmet variables needed for our image 4 | ENV GO111MODULE=on \ 5 | CGO_ENABLED=0 \ 6 | GOOS=linux \ 7 | GOARCH=amd64 8 | 9 | # Move to working directory /build 10 | WORKDIR /build 11 | 12 | # Copy and download dependency using go mod 13 | COPY go.mod . 14 | COPY go.sum . 15 | RUN go mod download 16 | 17 | # Copy the code into the container 18 | COPY . . 19 | 20 | # Run test 21 | RUN go test ./... 22 | 23 | # Build the application 24 | RUN go build -o main . 25 | 26 | # Move to /dist directory as the place for resulting binary folder 27 | WORKDIR /dist 28 | 29 | # Copy binary from build to main folder 30 | RUN cp /build/main . 31 | 32 | ############################ 33 | # STEP 2 build a small image 34 | ############################ 35 | FROM scratch 36 | 37 | COPY --from=builder /dist/main / 38 | COPY ./database/data.json /database/data.json 39 | 40 | # Command to run the executable 41 | ENTRYPOINT ["/main"] -------------------------------------------------------------------------------- /database/data.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "name": "cat", 3 | "sound": "meow" 4 | }, 5 | { 6 | "name": "dog", 7 | "sound": "woof" 8 | }, 9 | { 10 | "name": "cow", 11 | "sound": "mooo" 12 | } 13 | ] -------------------------------------------------------------------------------- /database/database.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "fmt" 7 | "io/ioutil" 8 | "os" 9 | ) 10 | 11 | type ( 12 | animal struct { 13 | Name string `json:"name"` 14 | Sound string `json:"sound"` 15 | } 16 | ) 17 | 18 | var animals []animal 19 | 20 | func init() { 21 | wd, err := os.Getwd() 22 | if err != nil { 23 | er(err) 24 | } 25 | 26 | file, err := os.Open(fmt.Sprintf("%v/database/data.json", wd)) 27 | if err != nil { 28 | er(err) 29 | } 30 | 31 | byteFile, err := ioutil.ReadAll(file) 32 | if err != nil { 33 | er(err) 34 | } 35 | 36 | err = json.Unmarshal(byteFile, &animals) 37 | if err != nil { 38 | er(err) 39 | } 40 | } 41 | 42 | func GetAnimal(name string) (*animal, error) { 43 | for _, v := range animals { 44 | if name == v.Name { 45 | return &v, nil 46 | } 47 | } 48 | return nil, errors.New("No animal found") 49 | } 50 | 51 | func er(msg interface{}) { 52 | fmt.Println("Error:", msg) 53 | os.Exit(1) 54 | } 55 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/afdolriski/golang-docker 2 | 3 | go 1.13 4 | 5 | require github.com/gin-gonic/gin v1.5.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 5 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 6 | github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc= 7 | github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= 8 | github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc= 9 | github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= 10 | github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= 11 | github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= 12 | github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= 13 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 14 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 15 | github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 16 | github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= 17 | github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= 18 | github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= 19 | github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= 20 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 21 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 22 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 23 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 24 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 25 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 26 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 27 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 28 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 29 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 30 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 31 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 32 | golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 33 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 34 | gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= 35 | gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc= 36 | gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= 37 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 38 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 39 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/afdolriski/golang-docker/database" 5 | "github.com/gin-gonic/gin" 6 | ) 7 | 8 | func main() { 9 | r := gin.Default() 10 | r.GET("/ping", func(c *gin.Context) { 11 | c.JSON(200, gin.H{ 12 | "message": "pong", 13 | }) 14 | }) 15 | 16 | r.GET("/animal/:name", func(c *gin.Context) { 17 | animal, err := database.GetAnimal(c.Param("name")) 18 | if err != nil { 19 | c.String(404, err.Error()) 20 | return 21 | } 22 | c.JSON(200, animal) 23 | }) 24 | 25 | r.Run(":3000") 26 | } 27 | --------------------------------------------------------------------------------