feat: create session. (also print the x-rid into the log)
This commit is contained in:
@ -28,4 +28,6 @@ type AppController struct {
|
||||
User interface{ User }
|
||||
|
||||
Admin interface{ Admin }
|
||||
|
||||
Session interface{ Session }
|
||||
}
|
||||
|
20
internal/howmuch/adapter/controller/session.go
Normal file
20
internal/howmuch/adapter/controller/session.go
Normal file
@ -0,0 +1,20 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Session interface {
|
||||
Create(*gin.Context)
|
||||
}
|
||||
|
||||
type SessionController struct{}
|
||||
|
||||
func NewSessionController() Session {
|
||||
return &SessionController{}
|
||||
}
|
||||
|
||||
func (sc *SessionController) Create(ctx *gin.Context) {
|
||||
log.CtxLog(ctx).DebugLog("session create")
|
||||
}
|
@ -54,6 +54,8 @@ func Routes(engine *gin.Engine, c controller.AppController) *gin.Engine {
|
||||
{
|
||||
userV1.POST("/create", func(ctx *gin.Context) { c.User.Create(ctx) })
|
||||
}
|
||||
|
||||
v1.POST("/session/create", func(ctx *gin.Context) { c.Session.Create(ctx) })
|
||||
}
|
||||
|
||||
return engine
|
||||
|
@ -51,7 +51,8 @@ func NewRegistry(db *pgx.Conn) Registry {
|
||||
// each domain.
|
||||
func (r *registry) NewAppController() controller.AppController {
|
||||
return controller.AppController{
|
||||
User: r.NewUserController(),
|
||||
Admin: r.NewAdminController(),
|
||||
User: r.NewUserController(),
|
||||
Admin: r.NewAdminController(),
|
||||
Session: r.NewSessionController(),
|
||||
}
|
||||
}
|
||||
|
9
internal/howmuch/registry/session.go
Normal file
9
internal/howmuch/registry/session.go
Normal file
@ -0,0 +1,9 @@
|
||||
package registry
|
||||
|
||||
import "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller"
|
||||
|
||||
// NewSessionController returns a session controller's implementation
|
||||
func (r *registry) NewSessionController() controller.Session {
|
||||
// u := usecase.NewSessionUsecase(repo.NewSessionRepository(r.db), repo.NewDBRepository(r.db))
|
||||
return controller.NewSessionController()
|
||||
}
|
@ -33,6 +33,7 @@ type Context interface {
|
||||
|
||||
// Request
|
||||
Bind(obj any) error
|
||||
GetHeader(key string) string
|
||||
|
||||
// Response
|
||||
JSON(code int, obj any)
|
||||
|
@ -26,6 +26,8 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
|
||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/middleware"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
@ -100,6 +102,26 @@ func NewLogger(opts *Options) *zapLogger {
|
||||
return &zapLogger{z: z}
|
||||
}
|
||||
|
||||
// CtxLog writes context's information into the log
|
||||
func CtxLog(ctx core.Context) *zapLogger {
|
||||
return std.CtxLog(ctx)
|
||||
}
|
||||
|
||||
func (z *zapLogger) CtxLog(ctx core.Context) *zapLogger {
|
||||
zz := z.clone()
|
||||
|
||||
if rid := ctx.GetHeader(middleware.XRequestID); rid != "" {
|
||||
zz.z = zz.z.With(zap.Any(middleware.XRequestID, rid))
|
||||
}
|
||||
|
||||
return zz
|
||||
}
|
||||
|
||||
func (z *zapLogger) clone() *zapLogger {
|
||||
zz := *z
|
||||
return &zz
|
||||
}
|
||||
|
||||
func (z *zapLogger) FatalLog(msg string, keyValues ...interface{}) {
|
||||
z.z.Sugar().Fatalw(msg, keyValues...)
|
||||
}
|
||||
|
@ -27,20 +27,20 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const requestID = "X-Request-Id"
|
||||
const XRequestID = "X-Request-Id"
|
||||
|
||||
func RequestID() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var rid string
|
||||
|
||||
if rid = ctx.GetHeader(requestID); rid != "" {
|
||||
ctx.Request.Header.Add(requestID, rid)
|
||||
if rid = ctx.GetHeader(XRequestID); rid != "" {
|
||||
ctx.Request.Header.Add(XRequestID, rid)
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
rid = uuid.NewString()
|
||||
ctx.Request.Header.Add(requestID, rid)
|
||||
ctx.Header(requestID, rid)
|
||||
ctx.Request.Header.Add(XRequestID, rid)
|
||||
ctx.Header(XRequestID, rid)
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
@ -58,21 +58,21 @@ func TestRequestID(t *testing.T) {
|
||||
wanted := "123"
|
||||
|
||||
r.GET("/example", func(c *gin.Context) {
|
||||
got = c.GetHeader(requestID)
|
||||
got = c.GetHeader(XRequestID)
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
||||
r.POST("/example", func(c *gin.Context) {
|
||||
got = c.GetHeader(requestID)
|
||||
got = c.GetHeader(XRequestID)
|
||||
c.String(http.StatusAccepted, "ok")
|
||||
})
|
||||
|
||||
// Test with Request ID
|
||||
_ = performRequest(r, "GET", "/example?a=100", header{requestID, wanted})
|
||||
_ = performRequest(r, "GET", "/example?a=100", header{XRequestID, wanted})
|
||||
assert.Equal(t, "123", got)
|
||||
|
||||
res := performRequest(r, "GET", "/example?a=100")
|
||||
assert.NotEqual(t, "", got)
|
||||
assert.NoError(t, uuid.Validate(got))
|
||||
assert.Equal(t, res.Header()[requestID][0], got)
|
||||
assert.Equal(t, res.Header()[XRequestID][0], got)
|
||||
}
|
||||
|
Reference in New Issue
Block a user