2023-09-18 23:57:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-01-27 19:56:01 +00:00
|
|
|
"context"
|
2023-11-29 03:01:49 +00:00
|
|
|
"fmt"
|
2023-09-18 23:57:18 +00:00
|
|
|
"html/template"
|
2023-11-29 03:01:49 +00:00
|
|
|
"io/fs"
|
2023-09-18 23:57:18 +00:00
|
|
|
"net/http"
|
2023-11-29 01:05:50 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-01-10 02:08:40 +00:00
|
|
|
"time"
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
"github.com/gin-contrib/multitemplate"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-contrib/sessions/cookie"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-09-23 02:12:36 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/config"
|
|
|
|
"reichard.io/antholume/database"
|
2024-01-28 02:02:08 +00:00
|
|
|
"reichard.io/antholume/utils"
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2024-01-28 02:02:08 +00:00
|
|
|
db *database.DBManager
|
|
|
|
cfg *config.Config
|
2024-02-25 19:54:50 +00:00
|
|
|
assets fs.FS
|
2024-01-28 02:02:08 +00:00
|
|
|
httpServer *http.Server
|
|
|
|
templates map[string]*template.Template
|
|
|
|
userAuthCache map[string]string
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
var htmlPolicy = bluemonday.StrictPolicy()
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
func NewApi(db *database.DBManager, c *config.Config, assets fs.FS) *API {
|
2023-09-18 23:57:18 +00:00
|
|
|
api := &API{
|
2024-01-28 02:02:08 +00:00
|
|
|
db: db,
|
|
|
|
cfg: c,
|
|
|
|
assets: assets,
|
|
|
|
userAuthCache: make(map[string]string),
|
2024-01-27 19:56:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Create router
|
2024-01-27 19:56:01 +00:00
|
|
|
router := gin.New()
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Add server
|
2024-01-27 19:56:01 +00:00
|
|
|
api.httpServer = &http.Server{
|
|
|
|
Handler: router,
|
|
|
|
Addr: (":" + c.ListenPort),
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Add global logging middleware
|
|
|
|
router.Use(loggingMiddleware)
|
2024-01-10 02:08:40 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Add global template loader middleware (develop)
|
|
|
|
if c.Version == "develop" {
|
|
|
|
log.Info("utilizing debug template loader")
|
|
|
|
router.Use(api.templateMiddleware(router))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assets & web app templates
|
2023-11-29 03:01:49 +00:00
|
|
|
assetsDir, _ := fs.Sub(assets, "assets")
|
2024-01-27 19:56:01 +00:00
|
|
|
router.StaticFS("/assets", http.FS(assetsDir))
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Generate auth token
|
2023-09-23 02:12:36 +00:00
|
|
|
var newToken []byte
|
|
|
|
var err error
|
2024-01-27 01:45:07 +00:00
|
|
|
if c.CookieAuthKey != "" {
|
2024-02-25 19:54:50 +00:00
|
|
|
log.Info("utilizing environment cookie auth key")
|
2024-01-27 01:45:07 +00:00
|
|
|
newToken = []byte(c.CookieAuthKey)
|
2023-09-23 02:12:36 +00:00
|
|
|
} else {
|
2024-02-25 19:54:50 +00:00
|
|
|
log.Info("generating cookie auth key")
|
2024-01-28 02:02:08 +00:00
|
|
|
newToken, err = utils.GenerateToken(64)
|
2023-09-23 02:12:36 +00:00
|
|
|
if err != nil {
|
2024-02-25 19:54:50 +00:00
|
|
|
log.Panic("unable to generate cookie auth key")
|
2023-09-23 02:12:36 +00:00
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Set enc token
|
2023-09-18 23:57:18 +00:00
|
|
|
store := cookie.NewStore(newToken)
|
2024-01-27 01:45:07 +00:00
|
|
|
if c.CookieEncKey != "" {
|
|
|
|
if len(c.CookieEncKey) == 16 || len(c.CookieEncKey) == 32 {
|
2024-02-25 19:54:50 +00:00
|
|
|
log.Info("utilizing environment cookie encryption key")
|
2024-01-27 01:45:07 +00:00
|
|
|
store = cookie.NewStore(newToken, []byte(c.CookieEncKey))
|
|
|
|
} else {
|
2024-02-25 19:54:50 +00:00
|
|
|
log.Panic("invalid cookie encryption key (must be 16 or 32 bytes)")
|
2024-01-27 01:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Configure cookie session store
|
2023-09-18 23:57:18 +00:00
|
|
|
store.Options(sessions.Options{
|
2023-10-14 01:06:49 +00:00
|
|
|
MaxAge: 60 * 60 * 24 * 7,
|
2023-10-24 22:41:25 +00:00
|
|
|
Secure: c.CookieSecure,
|
|
|
|
HttpOnly: c.CookieHTTPOnly,
|
2023-09-18 23:57:18 +00:00
|
|
|
SameSite: http.SameSiteStrictMode,
|
|
|
|
})
|
2024-01-27 19:56:01 +00:00
|
|
|
router.Use(sessions.Sessions("token", store))
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Register web app route
|
2024-01-27 19:56:01 +00:00
|
|
|
api.registerWebAppRoutes(router)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Register API routes
|
2024-01-27 19:56:01 +00:00
|
|
|
apiGroup := router.Group("/api")
|
2023-09-18 23:57:18 +00:00
|
|
|
api.registerKOAPIRoutes(apiGroup)
|
2023-10-05 23:56:19 +00:00
|
|
|
api.registerOPDSRoutes(apiGroup)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
func (api *API) Start() error {
|
|
|
|
return api.httpServer.ListenAndServe()
|
|
|
|
}
|
2024-01-28 02:02:08 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
func (api *API) Stop() error {
|
2024-02-25 19:54:50 +00:00
|
|
|
// Stop server
|
2024-01-27 19:56:01 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := api.httpServer.Shutdown(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close DB
|
|
|
|
return api.db.DB.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) registerWebAppRoutes(router *gin.Engine) {
|
2024-02-25 19:54:50 +00:00
|
|
|
// Generate templates
|
2024-01-27 19:56:01 +00:00
|
|
|
router.HTMLRender = *api.generateTemplates()
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Static assets (required @ root)
|
2024-01-27 19:56:01 +00:00
|
|
|
router.GET("/manifest.json", api.appWebManifest)
|
|
|
|
router.GET("/favicon.ico", api.appFaviconIcon)
|
|
|
|
router.GET("/sw.js", api.appServiceWorker)
|
2023-10-29 00:07:24 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Local / offline static pages (no template, no auth)
|
2024-01-27 19:56:01 +00:00
|
|
|
router.GET("/local", api.appLocalDocuments)
|
2023-11-27 02:41:17 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Reader (reader page, document progress, devices)
|
2024-01-27 19:56:01 +00:00
|
|
|
router.GET("/reader", api.appDocumentReader)
|
|
|
|
router.GET("/reader/devices", api.authWebAppMiddleware, api.appGetDevices)
|
|
|
|
router.GET("/reader/progress/:document", api.authWebAppMiddleware, api.appGetDocumentProgress)
|
2023-10-29 00:07:24 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Web app
|
2024-01-27 19:56:01 +00:00
|
|
|
router.GET("/", api.authWebAppMiddleware, api.appGetHome)
|
|
|
|
router.GET("/activity", api.authWebAppMiddleware, api.appGetActivity)
|
|
|
|
router.GET("/progress", api.authWebAppMiddleware, api.appGetProgress)
|
|
|
|
router.GET("/documents", api.authWebAppMiddleware, api.appGetDocuments)
|
|
|
|
router.GET("/documents/:document", api.authWebAppMiddleware, api.appGetDocument)
|
|
|
|
router.GET("/documents/:document/cover", api.authWebAppMiddleware, api.createGetCoverHandler(appErrorPage))
|
|
|
|
router.GET("/documents/:document/file", api.authWebAppMiddleware, api.createDownloadDocumentHandler(appErrorPage))
|
|
|
|
router.GET("/login", api.appGetLogin)
|
|
|
|
router.GET("/logout", api.authWebAppMiddleware, api.appAuthLogout)
|
|
|
|
router.GET("/register", api.appGetRegister)
|
|
|
|
router.GET("/settings", api.authWebAppMiddleware, api.appGetSettings)
|
|
|
|
router.GET("/admin/logs", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appGetAdminLogs)
|
2024-01-29 03:11:36 +00:00
|
|
|
router.GET("/admin/import", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appGetAdminImport)
|
|
|
|
router.POST("/admin/import", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appPerformAdminImport)
|
2024-01-27 19:56:01 +00:00
|
|
|
router.GET("/admin/users", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appGetAdminUsers)
|
2024-03-12 05:20:41 +00:00
|
|
|
router.POST("/admin/users", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appUpdateAdminUsers)
|
2024-01-27 19:56:01 +00:00
|
|
|
router.GET("/admin", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appGetAdmin)
|
|
|
|
router.POST("/admin", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appPerformAdminAction)
|
2024-01-29 03:11:36 +00:00
|
|
|
router.POST("/login", api.appAuthLogin)
|
|
|
|
router.POST("/register", api.appAuthRegister)
|
2023-10-31 10:28:22 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Demo mode enabled configuration
|
2024-01-27 19:56:01 +00:00
|
|
|
if api.cfg.DemoMode {
|
|
|
|
router.POST("/documents", api.authWebAppMiddleware, api.appDemoModeError)
|
|
|
|
router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.appDemoModeError)
|
|
|
|
router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.appDemoModeError)
|
|
|
|
router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.appDemoModeError)
|
|
|
|
router.POST("/settings", api.authWebAppMiddleware, api.appDemoModeError)
|
2023-10-31 10:28:22 +00:00
|
|
|
} else {
|
2024-01-27 19:56:01 +00:00
|
|
|
router.POST("/documents", api.authWebAppMiddleware, api.appUploadNewDocument)
|
|
|
|
router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.appDeleteDocument)
|
|
|
|
router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.appEditDocument)
|
|
|
|
router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.appIdentifyDocument)
|
|
|
|
router.POST("/settings", api.authWebAppMiddleware, api.appEditSettings)
|
2023-10-31 10:28:22 +00:00
|
|
|
}
|
2023-10-07 01:25:56 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Search enabled configuration
|
2024-01-27 19:56:01 +00:00
|
|
|
if api.cfg.SearchEnabled {
|
|
|
|
router.GET("/search", api.authWebAppMiddleware, api.appGetSearch)
|
|
|
|
router.POST("/search", api.authWebAppMiddleware, api.appSaveNewDocument)
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) registerKOAPIRoutes(apiGroup *gin.RouterGroup) {
|
|
|
|
koGroup := apiGroup.Group("/ko")
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// KO sync routes (webapp uses - progress & activity)
|
2024-01-27 03:07:30 +00:00
|
|
|
koGroup.GET("/documents/:document/file", api.authKOMiddleware, api.createDownloadDocumentHandler(apiErrorPage))
|
2024-01-01 04:12:46 +00:00
|
|
|
koGroup.GET("/syncs/progress/:document", api.authKOMiddleware, api.koGetProgress)
|
|
|
|
koGroup.GET("/users/auth", api.authKOMiddleware, api.koAuthorizeUser)
|
|
|
|
koGroup.POST("/activity", api.authKOMiddleware, api.koAddActivities)
|
|
|
|
koGroup.POST("/syncs/activity", api.authKOMiddleware, api.koCheckActivitySync)
|
2024-01-29 03:11:36 +00:00
|
|
|
koGroup.POST("/users/create", api.koAuthRegister)
|
2024-01-01 04:12:46 +00:00
|
|
|
koGroup.PUT("/syncs/progress", api.authKOMiddleware, api.koSetProgress)
|
2023-10-31 10:28:22 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Demo mode enabled configuration
|
2024-01-27 19:56:01 +00:00
|
|
|
if api.cfg.DemoMode {
|
2024-01-01 04:12:46 +00:00
|
|
|
koGroup.POST("/documents", api.authKOMiddleware, api.koDemoModeJSONError)
|
|
|
|
koGroup.POST("/syncs/documents", api.authKOMiddleware, api.koDemoModeJSONError)
|
|
|
|
koGroup.PUT("/documents/:document/file", api.authKOMiddleware, api.koDemoModeJSONError)
|
2023-10-31 10:28:22 +00:00
|
|
|
} else {
|
2024-01-01 04:12:46 +00:00
|
|
|
koGroup.POST("/documents", api.authKOMiddleware, api.koAddDocuments)
|
|
|
|
koGroup.POST("/syncs/documents", api.authKOMiddleware, api.koCheckDocumentsSync)
|
|
|
|
koGroup.PUT("/documents/:document/file", api.authKOMiddleware, api.koUploadExistingDocument)
|
2023-10-31 10:28:22 +00:00
|
|
|
}
|
2023-10-05 23:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) registerOPDSRoutes(apiGroup *gin.RouterGroup) {
|
|
|
|
opdsGroup := apiGroup.Group("/opds")
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// OPDS routes
|
2023-11-25 23:38:18 +00:00
|
|
|
opdsGroup.GET("", api.authOPDSMiddleware, api.opdsEntry)
|
|
|
|
opdsGroup.GET("/", api.authOPDSMiddleware, api.opdsEntry)
|
|
|
|
opdsGroup.GET("/search.xml", api.authOPDSMiddleware, api.opdsSearchDescription)
|
|
|
|
opdsGroup.GET("/documents", api.authOPDSMiddleware, api.opdsDocuments)
|
2024-01-27 03:07:30 +00:00
|
|
|
opdsGroup.GET("/documents/:document/cover", api.authOPDSMiddleware, api.createGetCoverHandler(apiErrorPage))
|
|
|
|
opdsGroup.GET("/documents/:document/file", api.authOPDSMiddleware, api.createDownloadDocumentHandler(apiErrorPage))
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:05:50 +00:00
|
|
|
func (api *API) generateTemplates() *multitemplate.Renderer {
|
2024-02-25 19:54:50 +00:00
|
|
|
// Define templates & helper functions
|
2023-12-01 12:35:51 +00:00
|
|
|
templates := make(map[string]*template.Template)
|
2023-11-29 01:05:50 +00:00
|
|
|
render := multitemplate.NewRenderer()
|
|
|
|
helperFuncs := template.FuncMap{
|
|
|
|
"dict": dict,
|
2024-01-24 04:00:51 +00:00
|
|
|
"fields": fields,
|
|
|
|
"getSVGGraphData": getSVGGraphData,
|
2024-03-11 17:13:26 +00:00
|
|
|
"getTimeZones": getTimeZones,
|
2024-01-20 19:26:26 +00:00
|
|
|
"hasPrefix": strings.HasPrefix,
|
2024-01-24 04:00:51 +00:00
|
|
|
"niceNumbers": niceNumbers,
|
|
|
|
"niceSeconds": niceSeconds,
|
2023-11-29 01:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Load base
|
|
|
|
b, _ := fs.ReadFile(api.assets, "templates/base.tmpl")
|
2023-11-29 01:05:50 +00:00
|
|
|
baseTemplate := template.Must(template.New("base").Funcs(helperFuncs).Parse(string(b)))
|
|
|
|
|
|
|
|
// Load SVGs
|
2024-02-25 19:54:50 +00:00
|
|
|
svgs, _ := fs.ReadDir(api.assets, "templates/svgs")
|
2023-11-29 03:01:49 +00:00
|
|
|
for _, item := range svgs {
|
|
|
|
basename := item.Name()
|
|
|
|
path := fmt.Sprintf("templates/svgs/%s", basename)
|
2023-11-29 01:05:50 +00:00
|
|
|
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
b, _ := fs.ReadFile(api.assets, path)
|
2023-11-29 01:05:50 +00:00
|
|
|
baseTemplate = template.Must(baseTemplate.New("svg/" + name).Parse(string(b)))
|
2023-12-01 12:35:51 +00:00
|
|
|
templates["svg/"+name] = baseTemplate
|
2023-11-29 01:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Load components
|
|
|
|
components, _ := fs.ReadDir(api.assets, "templates/components")
|
2023-11-29 03:01:49 +00:00
|
|
|
for _, item := range components {
|
|
|
|
basename := item.Name()
|
|
|
|
path := fmt.Sprintf("templates/components/%s", basename)
|
2023-11-29 01:05:50 +00:00
|
|
|
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Clone Base Template
|
2024-02-25 19:54:50 +00:00
|
|
|
b, _ := fs.ReadFile(api.assets, path)
|
2023-11-29 01:05:50 +00:00
|
|
|
baseTemplate = template.Must(baseTemplate.New("component/" + name).Parse(string(b)))
|
2023-12-01 12:35:51 +00:00
|
|
|
render.Add("component/"+name, baseTemplate)
|
|
|
|
templates["component/"+name] = baseTemplate
|
2023-11-29 01:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Load pages
|
|
|
|
pages, _ := fs.ReadDir(api.assets, "templates/pages")
|
2023-11-29 03:01:49 +00:00
|
|
|
for _, item := range pages {
|
|
|
|
basename := item.Name()
|
|
|
|
path := fmt.Sprintf("templates/pages/%s", basename)
|
2023-11-29 01:05:50 +00:00
|
|
|
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
|
|
|
|
|
|
|
// Clone Base Template
|
2024-02-25 19:54:50 +00:00
|
|
|
b, _ := fs.ReadFile(api.assets, path)
|
2023-11-29 01:05:50 +00:00
|
|
|
pageTemplate, _ := template.Must(baseTemplate.Clone()).New("page/" + name).Parse(string(b))
|
|
|
|
render.Add("page/"+name, pageTemplate)
|
2023-12-01 12:35:51 +00:00
|
|
|
templates["page/"+name] = pageTemplate
|
2023-11-29 01:05:50 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
api.templates = templates
|
2023-12-01 12:35:51 +00:00
|
|
|
|
2023-11-29 01:05:50 +00:00
|
|
|
return &render
|
|
|
|
}
|
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
func loggingMiddleware(c *gin.Context) {
|
|
|
|
// Start timer
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
// Process request
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
// End timer
|
|
|
|
endTime := time.Now()
|
|
|
|
latency := endTime.Sub(startTime).Round(time.Microsecond)
|
|
|
|
|
|
|
|
// Log data
|
|
|
|
logData := log.Fields{
|
|
|
|
"type": "access",
|
|
|
|
"ip": c.ClientIP(),
|
|
|
|
"latency": fmt.Sprintf("%s", latency),
|
|
|
|
"status": c.Writer.Status(),
|
|
|
|
"method": c.Request.Method,
|
|
|
|
"path": c.Request.URL.Path,
|
|
|
|
}
|
2024-01-10 02:08:40 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Get username
|
|
|
|
var auth authData
|
|
|
|
if data, _ := c.Get("Authorization"); data != nil {
|
|
|
|
auth = data.(authData)
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Log user
|
|
|
|
if auth.UserName != "" {
|
|
|
|
logData["user"] = auth.UserName
|
|
|
|
}
|
2024-01-10 02:36:36 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
// Log result
|
|
|
|
log.WithFields(logData).Info(fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path))
|
|
|
|
}
|
2024-01-10 02:36:36 +00:00
|
|
|
|
2024-02-25 19:54:50 +00:00
|
|
|
func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
router.HTMLRender = *api.generateTemplates()
|
|
|
|
c.Next()
|
2024-01-10 02:08:40 +00:00
|
|
|
}
|
|
|
|
}
|