En cours de développement

master
losyme 11 months ago
parent 05e68ef663
commit f3dd125744
  1. 2
      Taskfile.yml
  2. 2
      go.mod
  3. 4
      go.sum
  4. 63
      middleware/middleware.go

@ -7,7 +7,9 @@ tasks:
update:
cmds:
- go get -u forge.chapril.org/losyme/errors
- go get -u forge.chapril.org/losyme/logger
- go get -u forge.chapril.org/losyme/util
- go get -u forge.chapril.org/losyme/uuid
- go mod tidy
lint:
cmds:

@ -4,7 +4,9 @@ go 1.17
require (
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40
forge.chapril.org/losyme/logger v0.0.0-20211020125912-674a399f933c
forge.chapril.org/losyme/util v0.0.0-20211025142031-fd89d32d86fe
forge.chapril.org/losyme/uuid v0.0.0-20211007082235-8d502169e034
)
require (

@ -4,5 +4,9 @@ forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40 h1:EoVVFHoIsW
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40/go.mod h1:saYfIRBW9zjiCik+/+5ZaajMEnTPBNI00lF9EnO+2kM=
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae h1:RdYWXKy+OQNJueb4Qqqo7sL4OY3/TpfUvmoIoH8ua9E=
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae/go.mod h1:wnVjwnCO2vFLLZ1EgFuFws1n8XLluufsGsdozGYSA7c=
forge.chapril.org/losyme/logger v0.0.0-20211020125912-674a399f933c h1:AsWWDqFMigGp33sHDqGdJVVACiPYcY1ChSmBN7olBrk=
forge.chapril.org/losyme/logger v0.0.0-20211020125912-674a399f933c/go.mod h1:a3fN8yTjq5S/ramr03hIkutKM/35UsijUgRNKHjjlMU=
forge.chapril.org/losyme/util v0.0.0-20211025142031-fd89d32d86fe h1:xjMUKHxtG8jz57JyOxgNLlRaPLJeQvMV9K3HNlhKDB8=
forge.chapril.org/losyme/util v0.0.0-20211025142031-fd89d32d86fe/go.mod h1:lKYvkuBOTU3E2HuQZDh4ZSA4u3VcFopQbdPUKaHGHEk=
forge.chapril.org/losyme/uuid v0.0.0-20211007082235-8d502169e034 h1:4NMOPXxwRiXS7wltj2ZZmMO1nFRMPmG1dXK4Ta4MtiM=
forge.chapril.org/losyme/uuid v0.0.0-20211007082235-8d502169e034/go.mod h1:AgvbvGzTkQkbiM+s9eVhvLUSHbA0V6N+q5oBFI2v0LA=

@ -0,0 +1,63 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package middleware
import (
"forge.chapril.org/losyme/logger"
"forge.chapril.org/losyme/uuid"
"forge.chapril.org/losyme/kong/context"
"forge.chapril.org/losyme/kong/router"
)
const HeaderXRequestID = "X-Request-ID"
func Logger(logger *logger.Component) router.Middleware {
return func(next router.HandlerFunc) router.HandlerFunc {
return func(c *context.Context) error {
r := c.Request
logger.Trace( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Request",
"id", c.RequestID(),
"from", r.RemoteAddr,
"method", r.Method,
"uri", r.URL.RequestURI(),
)
err := next(c)
logger.Trace( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Response",
"request", c.RequestID(),
"status", c.Status,
)
return err
}
}
}
func RequestID() router.Middleware {
return func(next router.HandlerFunc) router.HandlerFunc {
return func(c *context.Context) error {
rid := c.Request.Header.Get(HeaderXRequestID)
if rid == "" {
rid = uuid.New()
}
c.SetRequestID(rid)
c.Response.Header().Set(HeaderXRequestID, rid)
return next(c)
}
}
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save