From 05cc265bdf2d58a2a85ab3b352dfb6f237804cd2 Mon Sep 17 00:00:00 2001 From: Pascal Bertrand Date: Wed, 17 Feb 2021 00:56:11 +0100 Subject: [PATCH 1/8] So don't play with me, 'cause you're playing with fire --- .idea/.gitignore | 8 +++++ .idea/misc.xml | 72 +++++++++++++++++++++++++++++++++++++++ .idea/modules.xml | 8 +++++ .idea/sonarIssues.xml | 39 +++++++++++++++++++++ .idea/square.iml | 9 +++++ .idea/vcs.xml | 6 ++++ Makefile | 4 +++ cmd/main.go | 11 ++++++ go.mod | 8 +++++ go.sum | 57 +++++++++++++++++++++++++++++++ internal/server.go | 28 +++++++++++++++ internal/square/square.go | 28 +++++++++++++++ 12 files changed, 278 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sonarIssues.xml create mode 100644 .idea/square.iml create mode 100644 .idea/vcs.xml create mode 100644 Makefile create mode 100644 cmd/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/server.go create mode 100644 internal/square/square.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d819570 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9fa8fad --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml new file mode 100644 index 0000000..48cae64 --- /dev/null +++ b/.idea/sonarIssues.xml @@ -0,0 +1,39 @@ + + + + + + \ No newline at end of file diff --git a/.idea/square.iml b/.idea/square.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/square.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d300ece --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ + +compile: + go build -o build/square.out cmd/main.go + diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..dd35bf4 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "github.com/floppyzedolfin/square/internal" +) + +func main() { + s := internal.NewServer() + + s.Listen(3000) +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bc0f0b9 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/floppyzedolfin/square + +go 1.15 + +require ( + github.com/gofiber/fiber v1.14.6 + github.com/jinzhu/gorm v1.9.16 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..96b80cc --- /dev/null +++ b/go.sum @@ -0,0 +1,57 @@ +github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= +github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4= +github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= +github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/gofiber/fiber v1.14.6 h1:QRUPvPmr8ijQuGo1MgupHBn8E+wW0IKqiOvIZPtV70o= +github.com/gofiber/fiber v1.14.6/go.mod h1:Yw2ekF1YDPreO9V6TMYjynu94xRxZBdaa8X5HhHsjCM= +github.com/gofiber/utils v0.0.10 h1:3Mr7X7JdCUo7CWf/i5sajSaDmArEDtti8bM1JUVso2U= +github.com/gofiber/utils v0.0.10/go.mod h1:9J5aHFUIjq0XfknT4+hdSMG6/jzfaAgCu4HEbWDeBlo= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= +github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= +github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= +github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= +github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg= +github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= +github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= +github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= +github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ= +github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA= +github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc= +github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= +golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= +golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/internal/server.go b/internal/server.go new file mode 100644 index 0000000..cf5685f --- /dev/null +++ b/internal/server.go @@ -0,0 +1,28 @@ +package internal + +import ( + "github.com/floppyzedolfin/square/internal/square" + "github.com/gofiber/fiber" +) + +type Server struct { + app *fiber.App +} + +func NewServer() *Server { + s := Server{app: fiber.New()} + s.createEndpoints() + return &s +} + +func (s *Server) Listen(port int) { + s.app.Listen(port) +} + +func (s* Server) createEndpoints() { + s.createSquareEndpoint() +} + +func (s* Server) createSquareEndpoint() { + s.app.Post("/square", square.Square) +} \ No newline at end of file diff --git a/internal/square/square.go b/internal/square/square.go new file mode 100644 index 0000000..548f090 --- /dev/null +++ b/internal/square/square.go @@ -0,0 +1,28 @@ +package square + +import ( + "fmt" + + "github.com/gofiber/fiber" + "github.com/jinzhu/gorm" +) + +func Square(c *fiber.Ctx) { + req := new(SquareRequest) + if err := c.BodyParser(req); err != nil { + c.Status(503).Send(err) + return + } + fmt.Printf("%v\n",req) + resp := SquareResponse{Value: req.Value*req.Value} + c.JSON(resp) +} + +type SquareRequest struct { + gorm.Model + Value int `json:"value"` +} + +type SquareResponse struct { + Value int `json:"value"` +} \ No newline at end of file From 2122faabeb6409e73dd287266cb05a13e82cdb9f Mon Sep 17 00:00:00 2001 From: Pascal Bertrand Date: Sat, 27 Feb 2021 17:54:22 +0100 Subject: [PATCH 2/8] Well, you've got your diamonds and you've got your pretty clothes --- .gitignore | 7 ++++++- Dockerfile | 12 ++++++++++++ README.md | 12 +++++++++++- cmd/main.go | 2 +- go.mod | 3 ++- go.sum | 11 +++++++++++ internal/server.go | 4 ++-- internal/square/square.go | 29 ++++++++++++----------------- internal/square/square_impl.go | 17 +++++++++++++++++ pkg/square.go | 14 ++++++++++++++ 10 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 Dockerfile create mode 100644 internal/square/square_impl.go create mode 100644 pkg/square.go diff --git a/.gitignore b/.gitignore index 66fd13c..aad09f1 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,9 @@ *.out # Dependency directories (remove the comment below to include it) -# vendor/ +vendor/ + +# IDE files +.idea/* + + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f0b3df0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.16 as builder + +WORKDIR /go/src/github.com/floppyzedolfin/square +COPY . /go/src/github.com/floppyzedolfin/square +RUN go mod tidy +RUN go mod vendor +RUN make build + +FROM scratch +COPY --from=builder /go/src/github.com/floppyzedolfin/square/build/square.out /app/square.out + +CMD ["/app/square.out"] \ No newline at end of file diff --git a/README.md b/README.md index e0ed4cd..e04774e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ # square -Playing with Fiber +Playing with Fiber v2 + +This project was aimed at playing with Fiber. It builds a microservice, inside a Docker image, that exposes an endpoint that squares an integer. + +## Run +Clone repository +```bash +git clone https://github.com/floppyzedolfin/square.git +``` + +Build the image \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index dd35bf4..43d9685 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,5 +7,5 @@ import ( func main() { s := internal.NewServer() - s.Listen(3000) + s.Listen(":3000") } \ No newline at end of file diff --git a/go.mod b/go.mod index bc0f0b9..89e37f7 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/floppyzedolfin/square go 1.15 require ( - github.com/gofiber/fiber v1.14.6 + github.com/gofiber/fiber v1.14.6 // indirect + github.com/gofiber/fiber/v2 v2.5.0 github.com/jinzhu/gorm v1.9.16 ) diff --git a/go.sum b/go.sum index 96b80cc..e2e709e 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gG github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/gofiber/fiber v1.14.6 h1:QRUPvPmr8ijQuGo1MgupHBn8E+wW0IKqiOvIZPtV70o= github.com/gofiber/fiber v1.14.6/go.mod h1:Yw2ekF1YDPreO9V6TMYjynu94xRxZBdaa8X5HhHsjCM= +github.com/gofiber/fiber/v2 v2.5.0 h1:yml405Um7b98EeMjx63OjSFTATLmX985HPWFfNUPV0w= +github.com/gofiber/fiber/v2 v2.5.0/go.mod h1:f8BRRIMjMdRyt2qmJ/0Sea3j3rwwfufPrh9WNBRiVZ0= github.com/gofiber/utils v0.0.10 h1:3Mr7X7JdCUo7CWf/i5sajSaDmArEDtti8bM1JUVso2U= github.com/gofiber/utils v0.0.10/go.mod h1:9J5aHFUIjq0XfknT4+hdSMG6/jzfaAgCu4HEbWDeBlo= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= @@ -36,17 +38,21 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ= github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA= +github.com/valyala/fasthttp v1.18.0 h1:IV0DdMlatq9QO1Cr6wGJPVW1sV1Q8HvZXAIcjorylyM= +github.com/valyala/fasthttp v1.18.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A= github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc= github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -54,4 +60,9 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201210223839-7e3030f88018 h1:XKi8B/gRBuTZN1vU9gFsLMm6zVz5FSCDzm8JYACnjy8= +golang.org/x/sys v0.0.0-20201210223839-7e3030f88018/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/internal/server.go b/internal/server.go index cf5685f..48e43ca 100644 --- a/internal/server.go +++ b/internal/server.go @@ -2,7 +2,7 @@ package internal import ( "github.com/floppyzedolfin/square/internal/square" - "github.com/gofiber/fiber" + "github.com/gofiber/fiber/v2" ) type Server struct { @@ -15,7 +15,7 @@ func NewServer() *Server { return &s } -func (s *Server) Listen(port int) { +func (s *Server) Listen(port string) { s.app.Listen(port) } diff --git a/internal/square/square.go b/internal/square/square.go index 548f090..236b9f7 100644 --- a/internal/square/square.go +++ b/internal/square/square.go @@ -3,26 +3,21 @@ package square import ( "fmt" - "github.com/gofiber/fiber" - "github.com/jinzhu/gorm" + "github.com/floppyzedolfin/square/pkg" + "github.com/gofiber/fiber/v2" ) -func Square(c *fiber.Ctx) { - req := new(SquareRequest) +// Square parses the request, performs the computation, and returns the result +func Square(c *fiber.Ctx) error { + req := new(pkg.SquareRequest) if err := c.BodyParser(req); err != nil { - c.Status(503).Send(err) - return + return c.Status(fiber.StatusBadRequest).SendString(fmt.Sprintf("unable to parse body as request: %s", err.Error())) } - fmt.Printf("%v\n",req) - resp := SquareResponse{Value: req.Value*req.Value} - c.JSON(resp) -} -type SquareRequest struct { - gorm.Model - Value int `json:"value"` -} + res, err := squareImpl(*c, *req) + if err != nil{ + return c.Status(err.Code).SendString(err.Message) + } -type SquareResponse struct { - Value int `json:"value"` -} \ No newline at end of file + return c.JSON(res) +} diff --git a/internal/square/square_impl.go b/internal/square/square_impl.go new file mode 100644 index 0000000..6fbb113 --- /dev/null +++ b/internal/square/square_impl.go @@ -0,0 +1,17 @@ +package square + +import ( + "github.com/floppyzedolfin/square/pkg" + "github.com/gofiber/fiber/v2" +) + +func squareImpl(_ fiber.Ctx, req pkg.SquareRequest) (pkg.SquareResponse, *fiber.Error) { + if req.Value == 0 { + return pkg.SquareResponse{}, fiber.NewError(fiber.StatusNotAcceptable, "naught, naught, naught") + } + + // implement logic here + result := req.Value * req.Value + + return pkg.SquareResponse{Value: result}, nil +} diff --git a/pkg/square.go b/pkg/square.go new file mode 100644 index 0000000..5e9659d --- /dev/null +++ b/pkg/square.go @@ -0,0 +1,14 @@ +package pkg + +import ( + "github.com/jinzhu/gorm" +) + +type SquareRequest struct { + gorm.Model // needed to parse this body + Value int `json:"value"` +} + +type SquareResponse struct { + Value int `json:"value"` +} From f8ef8a6ea5c2d99bba480dedba0ea0baa8139b2e Mon Sep 17 00:00:00 2001 From: Pascal Bertrand Date: Sun, 28 Feb 2021 00:57:36 +0100 Subject: [PATCH 3/8] And the chauffeur drives your car, you let everybody know --- .idea/sonarIssues.xml | 55 +++++++++++++++++++++++++++++++++ CHANGEBLOG.md | 12 ++++++++ Dockerfile | 9 +++--- Makefile | 15 +++++++-- README.md | 23 ++++++++++++-- build/Dockerfile | 5 +++ cmd/main.go | 1 + go.mod | 1 - go.sum | 18 +---------- internal/server.go | 14 ++++++--- internal/square/square.go | 6 ++-- internal/square/square_impl.go | 9 +++--- pkg/logger/log.go | 56 ++++++++++++++++++++++++++++++++++ pkg/square.go | 14 --------- pkg/square/square_def.go | 23 ++++++++++++++ 15 files changed, 209 insertions(+), 52 deletions(-) create mode 100644 CHANGEBLOG.md create mode 100644 build/Dockerfile create mode 100644 pkg/logger/log.go delete mode 100644 pkg/square.go create mode 100644 pkg/square/square_def.go diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml index 48cae64..6651b24 100644 --- a/.idea/sonarIssues.xml +++ b/.idea/sonarIssues.xml @@ -3,16 +3,46 @@ diff --git a/CHANGEBLOG.md b/CHANGEBLOG.md new file mode 100644 index 0000000..bb8559a --- /dev/null +++ b/CHANGEBLOG.md @@ -0,0 +1,12 @@ +# CHANGEBLOG + +This is a personal project, with no intent whatsoever on making any money out of it. The aim is to play with [Fiber](https://gofiber.io/), as you've probably already guessed after checking the commit messages. + +Why Fiber, and not something else? Well, I can't / won't write a microservice with several of them, so I had to pick. Fiber I hadn't worked with hitherto. The first major bump I had was when I faced was discovering Fiber v2 existed, after having written all my code. Well, that might be ~100 lines, but still. Updating the dependencies isn't always fun. + +Then I added the logger, and tried to play with `runtime.Callers`. Fun thing, depending on the `build` command, the logs won't be the same - local path for a `make build`, and something in `/go/src/github.com/floppyzedolfin/square/..` for the `stagedbuild` command. + +Oh, one fun thing I learnt as I was building my staged Dockerfile: build go without specifying `CGO_ENABLED=0` will somehow leave C dependencies in the produced binary, when using the `net` package - which, obviously, is what I was doing. +[This answer](https://stackoverflow.com/a/36308464/2106703) saved me countless hours of browsing the internet wondering why running my microservice on centos would work, but it wouldn't on alpine or scratch. + +For the sake of coverage, I've added an error case: when requesting the square of `0`, an error is returned. diff --git a/Dockerfile b/Dockerfile index f0b3df0..b7f22f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,10 @@ FROM golang:1.16 as builder WORKDIR /go/src/github.com/floppyzedolfin/square COPY . /go/src/github.com/floppyzedolfin/square -RUN go mod tidy -RUN go mod vendor -RUN make build +RUN make compile +RUN chmod +x /go/src/github.com/floppyzedolfin/square/build/square.out -FROM scratch +FROM alpine COPY --from=builder /go/src/github.com/floppyzedolfin/square/build/square.out /app/square.out -CMD ["/app/square.out"] \ No newline at end of file +ENTRYPOINT ["/app/square.out"] \ No newline at end of file diff --git a/Makefile b/Makefile index d300ece..11dac15 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,15 @@ +NAME=square +VERSION=0.1 -compile: - go build -o build/square.out cmd/main.go +build: + CGO_ENABLED=0 go build -o build/square.out cmd/main.go + docker build -t ${NAME}:{VERSION} build +run: stop + docker run --rm -p 8530:3000 --name ${NAME} ${NAME}:${VERSION} + +stagedbuild: + docker build -t ${NAME}:${VERSION} . + +stop: + docker stop ${NAME} || true diff --git a/README.md b/README.md index e04774e..5d4d530 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,27 @@ Playing with Fiber v2 This project was aimed at playing with Fiber. It builds a microservice, inside a Docker image, that exposes an endpoint that squares an integer. -## Run -Clone repository +## Clone the repository ```bash git clone https://github.com/floppyzedolfin/square.git ``` -Build the image \ No newline at end of file +## Make targets +There are two options to build the image. Both options will generate the docker image, and should generate the same docker image. Read below for potential differences. + +### make build +`make build` generates the image based on the deployed vendors, and on everything your environment has. This includes GOPATH values, overwritten commands - such as `go`, etc. It's useful for a dev point of view, as it provides an on-the-fly mechanism to build the current state of dev + +`make stagedbuild` is a more generic build, as it happens inside a pristine environment. The overhead of this is that we'll need to download the dependencies each time. This command is used in CI for deliveries. + +### make run +`make run` launches the service, exposing its port 3000 on the localhost's port 8530. The docker container will be removed when stopped. + +### make stop +`make stop` ends the docker process (if you've forgotten wherein terminal tab it's running) + +## TODOs +- Add some UTs, over `square_impl.go` and `log.go` +- Refactor the `internal/*` files, I'm not sure I like the way they are +- Currently, the `square/build/Dockerfile` is an excerpt of the `square/Dockerfile`. I'd rather it wouldn't. +- Add a real CI diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..b56080a --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine + +COPY square.out /app/square.out + +ENTRYPOINT ["/app/square.out"] diff --git a/cmd/main.go b/cmd/main.go index 43d9685..10d5459 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,6 +5,7 @@ import ( ) func main() { + // Create the server that will have all the necessary endpoints s := internal.NewServer() s.Listen(":3000") diff --git a/go.mod b/go.mod index 89e37f7..83aeb1e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/floppyzedolfin/square go 1.15 require ( - github.com/gofiber/fiber v1.14.6 // indirect github.com/gofiber/fiber/v2 v2.5.0 github.com/jinzhu/gorm v1.9.16 ) diff --git a/go.sum b/go.sum index e2e709e..021c1f4 100644 --- a/go.sum +++ b/go.sum @@ -8,16 +8,10 @@ github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DP github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/gofiber/fiber v1.14.6 h1:QRUPvPmr8ijQuGo1MgupHBn8E+wW0IKqiOvIZPtV70o= -github.com/gofiber/fiber v1.14.6/go.mod h1:Yw2ekF1YDPreO9V6TMYjynu94xRxZBdaa8X5HhHsjCM= github.com/gofiber/fiber/v2 v2.5.0 h1:yml405Um7b98EeMjx63OjSFTATLmX985HPWFfNUPV0w= github.com/gofiber/fiber/v2 v2.5.0/go.mod h1:f8BRRIMjMdRyt2qmJ/0Sea3j3rwwfufPrh9WNBRiVZ0= -github.com/gofiber/utils v0.0.10 h1:3Mr7X7JdCUo7CWf/i5sajSaDmArEDtti8bM1JUVso2U= -github.com/gofiber/utils v0.0.10/go.mod h1:9J5aHFUIjq0XfknT4+hdSMG6/jzfaAgCu4HEbWDeBlo= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= -github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= -github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -28,16 +22,10 @@ github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqy github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= -github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ= -github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA= github.com/valyala/fasthttp v1.18.0 h1:IV0DdMlatq9QO1Cr6wGJPVW1sV1Q8HvZXAIcjorylyM= github.com/valyala/fasthttp v1.18.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A= github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc= @@ -46,20 +34,16 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= -golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201210223839-7e3030f88018 h1:XKi8B/gRBuTZN1vU9gFsLMm6zVz5FSCDzm8JYACnjy8= golang.org/x/sys v0.0.0-20201210223839-7e3030f88018/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/server.go b/internal/server.go index 48e43ca..0547aa7 100644 --- a/internal/server.go +++ b/internal/server.go @@ -5,24 +5,30 @@ import ( "github.com/gofiber/fiber/v2" ) +// Server is the microservice application type Server struct { app *fiber.App } +// NewServer returns a fully operation server, ready to listen func NewServer() *Server { s := Server{app: fiber.New()} - s.createEndpoints() + s.attachEndpoints() return &s } +// Listen starts the server on the associated port func (s *Server) Listen(port string) { s.app.Listen(port) } -func (s* Server) createEndpoints() { - s.createSquareEndpoint() +// attachEndpoints adds all necessary endpoints to the server +// add each endpoint here +func (s* Server) attachEndpoints() { + s.attachSquareEndpoint() } -func (s* Server) createSquareEndpoint() { +// attachSquareEndpoint exposes the Square endpoint on the server +func (s* Server) attachSquareEndpoint() { s.app.Post("/square", square.Square) } \ No newline at end of file diff --git a/internal/square/square.go b/internal/square/square.go index 236b9f7..5ffe5a0 100644 --- a/internal/square/square.go +++ b/internal/square/square.go @@ -3,16 +3,18 @@ package square import ( "fmt" - "github.com/floppyzedolfin/square/pkg" + "github.com/floppyzedolfin/square/pkg/logger" + squaredef "github.com/floppyzedolfin/square/pkg/square" "github.com/gofiber/fiber/v2" ) // Square parses the request, performs the computation, and returns the result func Square(c *fiber.Ctx) error { - req := new(pkg.SquareRequest) + req := new(squaredef.Request) if err := c.BodyParser(req); err != nil { return c.Status(fiber.StatusBadRequest).SendString(fmt.Sprintf("unable to parse body as request: %s", err.Error())) } + logger.Log(logger.Info, "received request for endpoint square: %s", req) res, err := squareImpl(*c, *req) if err != nil{ diff --git a/internal/square/square_impl.go b/internal/square/square_impl.go index 6fbb113..215cd75 100644 --- a/internal/square/square_impl.go +++ b/internal/square/square_impl.go @@ -1,17 +1,18 @@ package square import ( - "github.com/floppyzedolfin/square/pkg" + squaredef "github.com/floppyzedolfin/square/pkg/square" "github.com/gofiber/fiber/v2" ) -func squareImpl(_ fiber.Ctx, req pkg.SquareRequest) (pkg.SquareResponse, *fiber.Error) { +// squareImpl squares the value contained in the request +func squareImpl(_ fiber.Ctx, req squaredef.Request) (squaredef.Response, *fiber.Error) { if req.Value == 0 { - return pkg.SquareResponse{}, fiber.NewError(fiber.StatusNotAcceptable, "naught, naught, naught") + return squaredef.Response{}, fiber.NewError(fiber.StatusNotAcceptable, "naught, naught, naught") } // implement logic here result := req.Value * req.Value - return pkg.SquareResponse{Value: result}, nil + return squaredef.Response{Value: result}, nil } diff --git a/pkg/logger/log.go b/pkg/logger/log.go new file mode 100644 index 0000000..35608cf --- /dev/null +++ b/pkg/logger/log.go @@ -0,0 +1,56 @@ +package logger + +import ( + "encoding/json" + "fmt" + "runtime" + "time" +) + +type Level int + +const ( + Debug Level = iota + Info + Warning + Error + noLog // set the defaultLevel to noLog to discard all logs +) + +const defaultLevel = Debug + +// Log prints a message on the standard output. The message has a json format and contains information about the caller +func Log(lvl Level, format string, a ...interface{}) { + if lvl >= defaultLevel { + msg := buildLogMessage(lvl, fmt.Sprintf(format, a), 2) + marshalledLog, _ := json.Marshal(msg) + fmt.Printf("%v", string(marshalledLog)) + } +} + +var levelNames = map[Level]string{ + Debug: "Debug", + Info: "Info", + Warning: "Warning", + Error: "Error", +} + +type logMessage struct { + Time string `json:"time"` + Level string `json:"level"` + Message string `json:"message"` + Caller string `json:"caller"` + Line int `json:"line"` +} + +func buildLogMessage(lvl Level, msg string, skip int) logMessage { + // need to skip twice: the buildLogMessage func and the Log func. + _, file, no, _ := runtime.Caller(skip) + return logMessage{ + Time: time.Now().Format(time.RFC3339), + Level: levelNames[lvl], + Message: msg, + Caller: file, + Line: no, + } +} diff --git a/pkg/square.go b/pkg/square.go deleted file mode 100644 index 5e9659d..0000000 --- a/pkg/square.go +++ /dev/null @@ -1,14 +0,0 @@ -package pkg - -import ( - "github.com/jinzhu/gorm" -) - -type SquareRequest struct { - gorm.Model // needed to parse this body - Value int `json:"value"` -} - -type SquareResponse struct { - Value int `json:"value"` -} diff --git a/pkg/square/square_def.go b/pkg/square/square_def.go new file mode 100644 index 0000000..52d5591 --- /dev/null +++ b/pkg/square/square_def.go @@ -0,0 +1,23 @@ +package square + +import ( + "fmt" + + "github.com/jinzhu/gorm" +) + +// Request holds the contents of the request, along with its json fields' names +type Request struct { + gorm.Model // needed to parse this body + Value int `json:"value"` +} + +// String implements the stringer interface, and hides the gorm thingy +func (r *Request) String() string { + return fmt.Sprintf("Value: %d", r.Value) +} + +// Response holds the contents of the response, along with its json fields's names +type Response struct { + Value int `json:"value"` +} From b8c484b47af95d54356ceb88af4e708d39361040 Mon Sep 17 00:00:00 2001 From: Pascal Bertrand Date: Sun, 28 Feb 2021 14:09:51 +0100 Subject: [PATCH 4/8] You better run, you better take cover --- .github/workflows/build.yaml | 17 +++++++ .github/workflows/coverage.yaml | 11 +++++ .gitignore | 16 +------ .idea/sonarIssues.xml | 55 +++++++++++++++++++++ CHANGEBLOG.md | 2 +- Makefile | 21 +++++++- README.md | 70 ++++++++++++++++++++++----- cmd/main.go | 6 +-- go.mod | 1 + go.sum | 10 ++++ internal/server.go | 34 ------------- internal/server/server.go | 28 +++++++++++ internal/server/server_test.go | 85 +++++++++++++++++++++++++++++++++ internal/server/square.go | 31 ++++++++++++ internal/square/square.go | 25 +++++----- internal/square/square_impl.go | 18 ------- internal/square/square_test.go | 53 ++++++++++++++++++++ pkg/logger/log.go | 38 +++++++++++---- pkg/logger/log_test.go | 60 +++++++++++++++++++++++ pkg/square/square_def.go | 9 +++- 20 files changed, 482 insertions(+), 108 deletions(-) create mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/coverage.yaml delete mode 100644 internal/server.go create mode 100644 internal/server/server.go create mode 100644 internal/server/server_test.go create mode 100644 internal/server/square.go delete mode 100644 internal/square/square_impl.go create mode 100644 internal/square/square_test.go create mode 100644 pkg/logger/log_test.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..28fa010 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,17 @@ +name: build + +on: [push] + +jobs: + stagedbuild: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: stagedbuild + run: make stagedbuild + localbuild: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: localbuild + run: make localbuild diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml new file mode 100644 index 0000000..21b4d33 --- /dev/null +++ b/.github/workflows/coverage.yaml @@ -0,0 +1,11 @@ +name: coverage + +on: [push] + +jobs: + cover: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: unit tests, with coverage + run: make checkcoverage diff --git a/.gitignore b/.gitignore index aad09f1..84293ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,20 +1,8 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, built with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE +# Outputs, be the from test or from localbuild *.out -# Dependency directories (remove the comment below to include it) +# Dependency directories vendor/ # IDE files .idea/* - - diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml index 6651b24..d91084a 100644 --- a/.idea/sonarIssues.xml +++ b/.idea/sonarIssues.xml @@ -3,6 +3,21 @@