2021-01-10 00:44:10 +00:00
|
|
|
package middleware
|
2021-01-09 20:39:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-11-05 07:29:17 +00:00
|
|
|
"github.com/rs/xid"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-01-18 23:13:35 +00:00
|
|
|
"go.sebtobie.de/httpserver/auth"
|
2021-11-11 22:05:07 +00:00
|
|
|
"go.sebtobie.de/httpserver/constants"
|
2021-01-09 20:39:05 +00:00
|
|
|
)
|
|
|
|
|
2022-11-05 07:44:37 +00:00
|
|
|
// LogMiddleware is an middleware to log requests to rs/zerolog and catches panics.
|
2021-01-18 23:18:08 +00:00
|
|
|
// If it is added multiple times, only the first time sends entries to the log.
|
2021-01-09 20:39:05 +00:00
|
|
|
func LogMiddleware(c *gin.Context) {
|
2021-01-18 23:18:08 +00:00
|
|
|
if _, exists := c.Get("xid"); exists {
|
|
|
|
return
|
2021-01-09 20:39:05 +00:00
|
|
|
}
|
2022-11-05 07:29:17 +00:00
|
|
|
id := xid.New()
|
|
|
|
c.Set("xid", id)
|
2021-01-09 20:39:05 +00:00
|
|
|
defer func() {
|
2022-11-05 07:29:17 +00:00
|
|
|
var entry *zerolog.Event
|
2021-11-07 20:40:44 +00:00
|
|
|
interrupt := recover()
|
|
|
|
if interrupt != nil {
|
|
|
|
err := interrupt.(error)
|
2022-11-05 07:29:17 +00:00
|
|
|
c.Header("requestid", id.String())
|
2021-01-09 20:39:05 +00:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
entry = log.Error().Err(err).Int("statuscode", 500)
|
2021-11-10 18:06:23 +00:00
|
|
|
entry.Stack()
|
2021-01-09 20:39:05 +00:00
|
|
|
} else {
|
2021-01-10 14:15:29 +00:00
|
|
|
statuscode := c.Writer.Status()
|
2021-11-07 20:40:44 +00:00
|
|
|
switch {
|
|
|
|
case statuscode >= 400 && statuscode < 500:
|
|
|
|
entry = log.Warn()
|
|
|
|
case statuscode >= 500:
|
2021-01-09 20:39:05 +00:00
|
|
|
entry = log.Error()
|
2021-11-07 20:40:44 +00:00
|
|
|
default:
|
|
|
|
entry = log.Info()
|
2021-01-09 20:39:05 +00:00
|
|
|
}
|
2021-11-07 20:40:44 +00:00
|
|
|
entry.Int("statuscode", statuscode)
|
2021-01-09 20:39:05 +00:00
|
|
|
}
|
2022-11-19 18:04:36 +00:00
|
|
|
entry.Str("xid", id.String()).Msg("Request")
|
2021-01-09 20:39:05 +00:00
|
|
|
}()
|
|
|
|
c.Next()
|
|
|
|
}
|
2021-01-18 23:13:35 +00:00
|
|
|
|
|
|
|
// RequireUser is an middleware that looks if the user is an Anonymous user and redircts it to the login if so.
|
|
|
|
func RequireUser(c *gin.Context) {
|
2021-11-11 22:05:07 +00:00
|
|
|
var acc = c.MustGet(constants.Accounts).(auth.Account)
|
2021-01-18 23:13:35 +00:00
|
|
|
if acc.Anonymous() {
|
|
|
|
acc.Redirect(c)
|
2021-01-24 11:10:29 +00:00
|
|
|
c.Abort()
|
2021-01-18 23:13:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 23:36:11 +00:00
|
|
|
|
2021-11-11 22:06:48 +00:00
|
|
|
// Config is the type For Configuration of the Middleware
|
2022-11-06 09:52:24 +00:00
|
|
|
type Config map[string]any
|
2021-11-11 22:06:48 +00:00
|
|
|
|
2021-02-26 23:36:11 +00:00
|
|
|
// Middleware is an type to Save data between executions and to provide help at the teardown.
|
|
|
|
type Middleware interface {
|
|
|
|
Gin(*gin.Context)
|
2021-11-11 22:06:48 +00:00
|
|
|
Defaults() Config
|
|
|
|
Setup(Config)
|
2021-02-26 23:36:11 +00:00
|
|
|
Teardown()
|
2022-11-07 17:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PreSetupMiddleware is for middleware that only needs the sites after their initialization
|
|
|
|
type PreSetupMiddleware interface {
|
|
|
|
Middleware
|
|
|
|
PreSetup([]any) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostSetupMiddleware is for middleware that requires sites to be already configured
|
|
|
|
type PostSetupMiddleware interface {
|
|
|
|
Middleware
|
|
|
|
PostSetup([]any) error
|
2021-02-26 23:36:11 +00:00
|
|
|
}
|