From 265a103ed8d162521db57e831c2eaa6529c1a20d Mon Sep 17 00:00:00 2001 From: tgs266 <siegel.tucker@gmail.com> Date: Fri, 24 Sep 2021 18:30:18 -0400 Subject: [PATCH] deployment updates --- Dockerfile | 18 ++++++++++++++++++ README.md | 6 +++++- common/config.go | 34 ++++++++++++++++++++++++++++++++++ common/context.go | 10 ++++++++++ common/logger.go | 2 +- config/config.go | 19 ------------------- config/dev | 5 +++++ config/prod | 7 +++++++ go.mod | 18 +++++++++--------- main.go | 23 +++++++++++++++-------- 10 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 Dockerfile create mode 100644 common/config.go delete mode 100644 config/config.go create mode 100644 config/dev create mode 100644 config/prod diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ee5e89e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM golang:1.16.8-bullseye +## We create an /app directory within our +## image that will hold our application source +## files +RUN mkdir /app +## We copy everything in the root directory +## into our /app directory +ADD . /app +## We specify that we now wish to execute +## any further commands inside our /app +## directory +WORKDIR /app +## we run go build to compile the binary +## executable of our Go program +RUN go build -o main . +## Our start command which kicks off +## our newly created binary executable +CMD ["/app/main"] \ No newline at end of file diff --git a/README.md b/README.md index c490275..7093a9f 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,8 @@ To rebuild swagger: `swag init` Call `go test -covermode=count -coverprofile=coverage.out ./...` To generate code coverage html page, call `go tool cover -html=coverage.out` -Swagger [Here](http://localhost:8080/api/weather/gdd-swagger/index.html#/) \ No newline at end of file +Swagger [Here](http://localhost:8080/api/weather/gdd-swagger/index.html#/) + +## Docker + +To build the image: `docker build -t dawn/dawn-weather .` \ No newline at end of file diff --git a/common/config.go b/common/config.go new file mode 100644 index 0000000..b88c607 --- /dev/null +++ b/common/config.go @@ -0,0 +1,34 @@ +package common + +import ( + "log" + "os" + + "github.com/spf13/viper" +) + +func GetConfig(configName string) { + viper.SetConfigName("local") + viper.SetConfigType("yaml") + viper.AddConfigPath(".") + viper.AddConfigPath("./config/") + viper.AutomaticEnv() + + err := viper.ReadInConfig() + if err != nil { + log.Fatalln("Env local not found") + os.Exit(1) + } + + viper.SetConfigName(configName) + viper.SetConfigType("yaml") + viper.AddConfigPath(".") + viper.AddConfigPath("./config/") + viper.AutomaticEnv() + + err = viper.MergeInConfig() + if err != nil { + log.Fatalln("Env " + configName + " not found") + os.Exit(1) + } +} diff --git a/common/context.go b/common/context.go index 6dfdc76..542fe34 100644 --- a/common/context.go +++ b/common/context.go @@ -19,3 +19,13 @@ func (ctx DawnCtx) DEBUG(message string) { func (ctx DawnCtx) TRACE(message string) { TRACE(ctx.FiberCtx, message) } + +func BuildCtx(c *fiber.Ctx) DawnCtx { + return DawnCtx{ + FiberCtx: c, + } +} + +func (ctx DawnCtx) BodyParser(out interface{}) error { + return ctx.FiberCtx.BodyParser(out) +} diff --git a/common/logger.go b/common/logger.go index efa3111..6812919 100644 --- a/common/logger.go +++ b/common/logger.go @@ -92,7 +92,7 @@ func LogRequest(message RequestLog) { fmt.Println(logString) } -func New() fiber.Handler { +func FiberLogger() fiber.Handler { return func(c *fiber.Ctx) error { errHandler := c.App().Config().ErrorHandler diff --git a/config/config.go b/config/config.go deleted file mode 100644 index 7aba663..0000000 --- a/config/config.go +++ /dev/null @@ -1,19 +0,0 @@ -package config - -import ( - "github.com/spf13/viper" - "os" -) - -func GetConfig() { - viper.SetConfigName("local") - viper.SetConfigType("yaml") - viper.AddConfigPath(".") - viper.AddConfigPath("./config/") - viper.AutomaticEnv() - - err := viper.ReadInConfig() - if err != nil { - os.Exit(1) - } -} diff --git a/config/dev b/config/dev new file mode 100644 index 0000000..4d8b5b6 --- /dev/null +++ b/config/dev @@ -0,0 +1,5 @@ +app: + logLevel: DEBUG + +db: + uri: "mongodb://host.docker.internal:27017/" diff --git a/config/prod b/config/prod new file mode 100644 index 0000000..09a53cc --- /dev/null +++ b/config/prod @@ -0,0 +1,7 @@ +app: + logType: json + logLevel: INFO + swagger: false + +db: + uri: "mongodb://host.docker.internal:27017/" diff --git a/go.mod b/go.mod index 1b382c5..4930e71 100644 --- a/go.mod +++ b/go.mod @@ -5,32 +5,32 @@ go 1.16 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/andybalholm/brotli v1.0.3 // indirect - github.com/ansrivas/fiberprometheus/v2 v2.1.2 // indirect + github.com/ansrivas/fiberprometheus/v2 v2.1.2 github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect - github.com/arsmn/fiber-swagger/v2 v2.15.0 // indirect - github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013 // indirect + github.com/arsmn/fiber-swagger/v2 v2.15.0 + github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013 github.com/coreos/etcd v3.3.10+incompatible // indirect github.com/coreos/go-etcd v2.0.0+incompatible // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/go-openapi/jsonreference v0.19.6 // indirect github.com/go-openapi/spec v0.20.3 // indirect github.com/go-openapi/swag v0.19.15 // indirect - github.com/go-ozzo/ozzo-validation v3.6.0+incompatible // indirect + github.com/go-ozzo/ozzo-validation v3.6.0+incompatible github.com/gofiber/adaptor/v2 v2.1.9 // indirect github.com/gofiber/fiber/v2 v2.16.0 - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.3.0 github.com/klauspost/compress v1.13.3 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/prometheus/client_golang v1.11.0 // indirect github.com/prometheus/common v0.30.0 // indirect github.com/prometheus/procfs v0.7.2 // indirect - github.com/spf13/viper v1.8.1 // indirect + github.com/spf13/viper v1.8.1 github.com/stretchr/testify v1.7.0 // indirect - github.com/swaggo/swag v1.7.1 // indirect + github.com/swaggo/swag v1.7.1 github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect - github.com/valyala/fasthttp v1.28.0 // indirect + github.com/valyala/fasthttp v1.28.0 github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect - go.mongodb.org/mongo-driver v1.7.1 // indirect + go.mongodb.org/mongo-driver v1.7.1 go4.org v0.0.0-20201209231011-d4a079459e60 // indirect golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect diff --git a/main.go b/main.go index 343fba4..07c9d28 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,12 @@ import ( "dawn-weather/config" "dawn-weather/controllers" "dawn-weather/persistence" + "flag" "strconv" "github.com/ansrivas/fiberprometheus/v2" swagger "github.com/arsmn/fiber-swagger/v2" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/fiber/v2/middleware/requestid" "github.com/gofiber/fiber/v2/utils" @@ -33,10 +32,10 @@ func registerSwagger(app *fiber.App) { } func registerCors(app *fiber.App) { - app.Use(cors.New(cors.Config{ - AllowOrigins: "http://localhost:3000, http://localhost:4200, http://localhost:5000, http://localhost:12321", - AllowHeaders: "Origin, Content-Type, Accept", - })) + // app.Use(cors.New(cors.Config{ + // AllowOrigins: "http://localhost:3000, http://localhost:4200, http://localhost:5000, http://localhost:12321", + // AllowHeaders: "Origin, Content-Type, Accept", + // })) } func registerLogging(app *fiber.App) { @@ -49,7 +48,7 @@ func registerLogging(app *fiber.App) { ContextKey: "requestId", })) - app.Use(logger.New()) + app.Use(common.FiberLogger()) } func registerPrometheus(app *fiber.App) { @@ -91,6 +90,7 @@ func createFiberConfig() fiber.Config { } func CreateFiberApp() *fiber.App { + app := fiber.New(createFiberConfig()) app.Use(recover.New()) registerCors(app) @@ -101,6 +101,12 @@ func CreateFiberApp() *fiber.App { return app } +var flagVal *string + +func init() { + flagVal = flag.String("env", "local", "environment (local, prod)") +} + // @title Dawn GDD Service // @version 1.0 // @description All operations for GDD/Freezing Date data @@ -109,7 +115,8 @@ func CreateFiberApp() *fiber.App { // @host localhost:8080 // @BasePath / func main() { - config.GetConfig() + flag.Parse() + common.GetConfig(*flagVal) persistence.CreateDBSession() app := CreateFiberApp() -- GitLab