From 90765beb35c2e1ed3dc6568fd5b9addbffdd112d Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Mon, 20 Mar 2023 17:11:37 +0100 Subject: [PATCH] update --- src/go.sum | 2 -- src/internal/app/definition/auth.go | 10 ---------- src/internal/app/definition/health.go | 5 ----- src/internal/app/server/config_handler.go | 9 +++++++++ src/internal/app/server/health_handler.go | 11 +++++------ src/internal/app/server/login_handler.go | 14 +++++++++++--- src/internal/app/server/router.go | 21 ++++++++++++++++----- src/internal/app/server/traefik_handler.go | 18 ++++++++++++++++++ 8 files changed, 59 insertions(+), 31 deletions(-) delete mode 100644 src/internal/app/definition/auth.go delete mode 100644 src/internal/app/definition/health.go create mode 100644 src/internal/app/server/config_handler.go create mode 100644 src/internal/app/server/traefik_handler.go diff --git a/src/go.sum b/src/go.sum index 41cf4fe..f426663 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,6 +1,4 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.134 h1:ToujzrSBAD0Yt62T4+Ak+5pRCw6sMYnry3FDTo3eRnM= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.134/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= git.pbiernat.dev/egommerce/go-api-pkg v0.0.136 h1:SzJRAkqJKdng/3d0V7o/R0yGh7QaZynPBn/P++on9RA= git.pbiernat.dev/egommerce/go-api-pkg v0.0.136/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/src/internal/app/definition/auth.go b/src/internal/app/definition/auth.go deleted file mode 100644 index 2516eeb..0000000 --- a/src/internal/app/definition/auth.go +++ /dev/null @@ -1,10 +0,0 @@ -package definition - -type AuthLoginRequest struct { - Username string `json:"username"` - Password string `json:"password"` -} - -type AuthLoginResponse struct { - JWTToken string `json:"jwt_token"` -} diff --git a/src/internal/app/definition/health.go b/src/internal/app/definition/health.go deleted file mode 100644 index 6920fca..0000000 --- a/src/internal/app/definition/health.go +++ /dev/null @@ -1,5 +0,0 @@ -package definition - -type HealthResponse struct { - Status string `json:"status,omitempty"` -} diff --git a/src/internal/app/server/config_handler.go b/src/internal/app/server/config_handler.go new file mode 100644 index 0000000..cfb9949 --- /dev/null +++ b/src/internal/app/server/config_handler.go @@ -0,0 +1,9 @@ +package server + +import ( + "github.com/gofiber/fiber/v2" +) + +func (s *Server) ConfigHandler(c *fiber.Ctx) error { + return c.JSON(s.conf) +} diff --git a/src/internal/app/server/health_handler.go b/src/internal/app/server/health_handler.go index 3f8f866..3181e8e 100644 --- a/src/internal/app/server/health_handler.go +++ b/src/internal/app/server/health_handler.go @@ -1,16 +1,15 @@ package server import ( - def "git.pbiernat.dev/egommerce/identity-service/internal/app/definition" "github.com/gofiber/fiber/v2" ) +type HealthResponse struct { + Status string `json:"status,omitempty"` +} + func (s *Server) HealthHandler(c *fiber.Ctx) error { - return c.JSON(&def.HealthResponse{ + return c.JSON(&HealthResponse{ Status: "OK", }) } - -func (s *Server) ConfigHandler(c *fiber.Ctx) error { - return c.JSON(s.conf) -} diff --git a/src/internal/app/server/login_handler.go b/src/internal/app/server/login_handler.go index ee1f500..fb97e34 100644 --- a/src/internal/app/server/login_handler.go +++ b/src/internal/app/server/login_handler.go @@ -1,13 +1,21 @@ package server import ( - def "git.pbiernat.dev/egommerce/identity-service/internal/app/definition" "git.pbiernat.dev/egommerce/identity-service/internal/app/service" "github.com/gofiber/fiber/v2" ) +type AuthLoginRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type AuthLoginResponse struct { + JWTToken string `json:"jwt_token"` +} + func (s *Server) LoginHandler(c *fiber.Ctx) error { - data := new(def.AuthLoginRequest) + data := new(AuthLoginRequest) if err := c.BodyParser(data); err != nil { return err } @@ -20,5 +28,5 @@ func (s *Server) LoginHandler(c *fiber.Ctx) error { cookie := service.AuthService.Cookie("auth_token", token) c.Cookie(cookie) - return c.JSON(&def.AuthLoginResponse{JWTToken: token}) + return c.JSON(&AuthLoginResponse{JWTToken: token}) } diff --git a/src/internal/app/server/router.go b/src/internal/app/server/router.go index bede929..0d8352b 100644 --- a/src/internal/app/server/router.go +++ b/src/internal/app/server/router.go @@ -17,6 +17,11 @@ var ( }) ) +func SetupMiddlewares(s *Server) { + s.App.Use(defaultCORS) + s.App.Use(LoggingMiddleware(s.log)) +} + func SetupRoutes(s *Server) { s.App.Options("*", defaultCORS) @@ -26,11 +31,7 @@ func SetupRoutes(s *Server) { api := s.App.Group("/api") v1 := api.Group("/v1") v1.Post("/login", s.LoginHandler) -} - -func SetupMiddlewares(s *Server) { - s.App.Use(defaultCORS) - s.App.Use(LoggingMiddleware(s.log)) + v1.All("/traefik", s.TraefikHandler) } // Middlewares @@ -41,6 +42,16 @@ func LoggingMiddleware(log *fluentd.Logger) func(c *fiber.Ctx) error { return c.Next() } + if strings.Contains(path, "/traefik") { + log.Log("Request: %s, Headers: %v, remote: %s, via: %s", + c.Request().URI().String(), + c.GetRespHeaders(), + c.Context().RemoteIP().String(), + string(c.Context().UserAgent())) + + return c.Next() + } + log.Log("Request: %s, remote: %s, via: %s", c.Request().URI().String(), c.Context().RemoteIP().String(), diff --git a/src/internal/app/server/traefik_handler.go b/src/internal/app/server/traefik_handler.go new file mode 100644 index 0000000..a29fa29 --- /dev/null +++ b/src/internal/app/server/traefik_handler.go @@ -0,0 +1,18 @@ +package server + +import ( + "git.pbiernat.dev/egommerce/identity-service/internal/app/service" + "github.com/gofiber/fiber/v2" +) + +type TraefikAuthResponse struct { + Status string `json:"status"` +} + +func (s *Server) TraefikHandler(c *fiber.Ctx) error { + cookie := service.AuthService.Cookie("traefik", "tmp-dummy-traefik-token") + c.Cookie(cookie) + s.log.Log("Traefik action set cookie. done.") + + return c.JSON(&TraefikAuthResponse{Status: "OK"}) +}