package server import ( "fmt" "net" "time" "github.com/gin-gonic/gin" "github.com/rs/zerolog" "kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/util" ) func (srv *Service) Default(c *gin.Context) { path := c.Request.URL.Path start := time.Now() defer func() { var cl *zerolog.Event err := recover() if err != nil { cl = srv.l.Error() switch v := err.(type) { case errors.Error: c.AbortWithStatusJSON(v.Code, v) cl.Err(v) case error: c.String(500, v.Error()) c.Abort() cl.Err(v) default: c.String(500, fmt.Sprint(err)) c.Abort() cl.Str("error", fmt.Sprint(err)) } // if !util.PROD { // cl.Str("trace", util.Stack()) // } } else if c.Writer.Status() >= 400 { cl = srv.l.Error().Strs("error", c.Errors.Errors()) } else { cl = srv.l.Info() } if srv.SkipLog != nil && srv.SkipLog(c) { return } if usr, ok := c.Get("user"); ok { cl.Str("user", usr.(string)) } if !util.PROD { cl.Caller(2) } cl. Str("method", c.Request.Method). Str("ip", c.ClientIP()). Int("status", c.Writer.Status()). Dur("duration", time.Since(start)). Str("url", path). Msg("") }() if srv.Allow != nil { if !srv.Allow.Contains(net.ParseIP(c.ClientIP())) { panic(errors.ErrorForbidden) } } c.Next() }