update
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2023-03-20 17:11:37 +01:00
parent 167d067aed
commit 90765beb35
8 changed files with 59 additions and 31 deletions

View File

@ -1,6 +1,4 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 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 h1:SzJRAkqJKdng/3d0V7o/R0yGh7QaZynPBn/P++on9RA=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.136/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= 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= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

View File

@ -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"`
}

View File

@ -1,5 +0,0 @@
package definition
type HealthResponse struct {
Status string `json:"status,omitempty"`
}

View File

@ -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)
}

View File

@ -1,16 +1,15 @@
package server package server
import ( import (
def "git.pbiernat.dev/egommerce/identity-service/internal/app/definition"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
type HealthResponse struct {
Status string `json:"status,omitempty"`
}
func (s *Server) HealthHandler(c *fiber.Ctx) error { func (s *Server) HealthHandler(c *fiber.Ctx) error {
return c.JSON(&def.HealthResponse{ return c.JSON(&HealthResponse{
Status: "OK", Status: "OK",
}) })
} }
func (s *Server) ConfigHandler(c *fiber.Ctx) error {
return c.JSON(s.conf)
}

View File

@ -1,13 +1,21 @@
package server package server
import ( import (
def "git.pbiernat.dev/egommerce/identity-service/internal/app/definition"
"git.pbiernat.dev/egommerce/identity-service/internal/app/service" "git.pbiernat.dev/egommerce/identity-service/internal/app/service"
"github.com/gofiber/fiber/v2" "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 { func (s *Server) LoginHandler(c *fiber.Ctx) error {
data := new(def.AuthLoginRequest) data := new(AuthLoginRequest)
if err := c.BodyParser(data); err != nil { if err := c.BodyParser(data); err != nil {
return err return err
} }
@ -20,5 +28,5 @@ func (s *Server) LoginHandler(c *fiber.Ctx) error {
cookie := service.AuthService.Cookie("auth_token", token) cookie := service.AuthService.Cookie("auth_token", token)
c.Cookie(cookie) c.Cookie(cookie)
return c.JSON(&def.AuthLoginResponse{JWTToken: token}) return c.JSON(&AuthLoginResponse{JWTToken: token})
} }

View File

@ -17,6 +17,11 @@ var (
}) })
) )
func SetupMiddlewares(s *Server) {
s.App.Use(defaultCORS)
s.App.Use(LoggingMiddleware(s.log))
}
func SetupRoutes(s *Server) { func SetupRoutes(s *Server) {
s.App.Options("*", defaultCORS) s.App.Options("*", defaultCORS)
@ -26,11 +31,7 @@ func SetupRoutes(s *Server) {
api := s.App.Group("/api") api := s.App.Group("/api")
v1 := api.Group("/v1") v1 := api.Group("/v1")
v1.Post("/login", s.LoginHandler) v1.Post("/login", s.LoginHandler)
} v1.All("/traefik", s.TraefikHandler)
func SetupMiddlewares(s *Server) {
s.App.Use(defaultCORS)
s.App.Use(LoggingMiddleware(s.log))
} }
// Middlewares // Middlewares
@ -41,6 +42,16 @@ func LoggingMiddleware(log *fluentd.Logger) func(c *fiber.Ctx) error {
return c.Next() 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", log.Log("Request: %s, remote: %s, via: %s",
c.Request().URI().String(), c.Request().URI().String(),
c.Context().RemoteIP().String(), c.Context().RemoteIP().String(),

View File

@ -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"})
}