mirror of
https://github.com/veops/oneterm.git
synced 2025-10-05 15:27:01 +08:00
50 lines
934 B
Go
50 lines
934 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/veops/oneterm/acl"
|
|
"github.com/veops/oneterm/logger"
|
|
)
|
|
|
|
func ginLogger() gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
start := time.Now()
|
|
|
|
ctx.Next()
|
|
|
|
cost := time.Since(start)
|
|
logger.L().Info(ctx.Request.URL.String(),
|
|
zap.String("method", ctx.Request.Method),
|
|
zap.Int("status", ctx.Writer.Status()),
|
|
zap.String("ip", ctx.ClientIP()),
|
|
zap.Duration("cost", cost),
|
|
)
|
|
|
|
}
|
|
}
|
|
|
|
func auth() gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
cookie, err := ctx.Cookie("session")
|
|
if err != nil || cookie == "" {
|
|
logger.L().Error("cannot get cookie.session", zap.Error(err))
|
|
ctx.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
sess, err := acl.ParseCookie(cookie)
|
|
if err != nil {
|
|
ctx.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
ctx.Set("session", sess)
|
|
|
|
ctx.Next()
|
|
}
|
|
}
|