Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logger middleware for dev/local environment #38

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/server/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package logger

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)

func NewLogger() fiber.Handler {
return logger.New(logger.Config{
Format: "${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n",
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
})
}
27 changes: 18 additions & 9 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ package server

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
"gorm.io/gorm"
"onepixel_backend/src/config"
"onepixel_backend/src/docs"
_ "onepixel_backend/src/docs"
"onepixel_backend/src/routes/api"
"onepixel_backend/src/server/logger"

"onepixel_backend/src/routes/redirect"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
"gorm.io/gorm"
)

const (
ENVIRONMENT_NAME_PRODUCTION = "production"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in Go the culture is not to use ALL_CAP_CASE for variables like Java world no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant should use all capital letters and use underscore _ to separate words. Eg. INT_MAX

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://go.dev/talks/2014/names.slide#5
Please try to follow the official blog and its rules

Constant should use all capital letters and use underscore _ to separate words. Eg. INT_MAX

Can't have capital always as in Go, capital suggests exporting of values outside package scope

)

// CreateAdminApp creates the fiber app
Expand Down Expand Up @@ -40,17 +47,19 @@ import (
func CreateAdminApp(db *gorm.DB) *fiber.App {
app := fiber.New()

switch config.Env {
case ENVIRONMENT_NAME_PRODUCTION:
docs.SwaggerInfo.Host = config.AdminHost
default:
app.Use(logger.NewLogger())
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%s", config.AdminHost, config.Port)
}

apiV1 := app.Group("/api/v1")

apiV1.Route("/users", api.UsersRoute(db))
apiV1.Route("/urls", api.UrlsRoute(db))

if config.Env == "production" {
docs.SwaggerInfo.Host = config.AdminHost
} else {
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%s", config.AdminHost, config.Port)
}

app.Get("/docs/*", swagger.HandlerDefault)

return app
Expand Down
Loading