diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..ee5e89e1880acae25978705f49a7267997e89781 --- /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 c49027542cb5aa8075516f2822a5b9c4c58792ad..7093a9f6749385bd0a1e25abace4adc53b7a684e 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 0000000000000000000000000000000000000000..b88c60753b3c5cbfd487e4257465b63b545d9a43 --- /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 6dfdc760695aee066cd173cf21e7c66571640d0c..542fe34db26df963f6203d8245756b3a007713b8 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 efa3111653d8620e4fcd90f29eeed6bb858ffd04..68129199f2e079df138af4ab06d5ef36dd2b6e14 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 7aba663f3d20aab8b0ce7ee4e9141abe10c16277..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..4d8b5b622e811981e92f423a43af54ac953d54eb --- /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 0000000000000000000000000000000000000000..09a53cc6aa8a3a65125fb6ec96ea51d47e76718d --- /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 1b382c54873918fc2a518bc59b716ab5170c7ded..4930e71a3bef4aa5eb5acce5812e8859cfe1ed2b 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 343fba43d10cd370215db795c1d4b1ca54d33dc4..07c9d2899a3a82ca4a0427b9a28d4cb329fbad96 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()