package web import ( "log" "net/http" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "github.com/spf13/viper" "git.jmbit.de/filegate/filegate/db" "git.jmbit.de/filegate/filegate/web/static" ) func setupRouter(production bool) *gin.Engine { log.Printf("Production mode: %v", production) if production { gin.SetMode(gin.ReleaseMode) } else { gin.SetMode(gin.DebugMode) } gin.ForceConsoleColor() router := gin.New() router.Use(gin.Recovery()) router.Use(sessions.Sessions("session", db.CreateStore())) router.Use(urlLog()) err := router.SetTrustedProxies(viper.GetStringSlice("web.trustedProxies")) router.HTMLRender = &TemplRender{} if err != nil { log.Printf("Could not set trusted Proxies: %v", err) } if !production { router.Static("/static", "./web/static") } else { router.StaticFS("/static", http.FS(static.StaticFS)) } return router } func urlLog() gin.HandlerFunc { return func(c *gin.Context) { session := sessions.Default(c) username := session.Get("username") if username == nil { username = "N/A" } log.Printf( "[INFO] %s: %s User: %s Source: %s", c.Request.Method, c.Request.URL, username, c.Request.RemoteAddr, ) } }