AnthoLume/api/api.go

281 lines
9.3 KiB
Go
Raw Normal View History

2023-09-18 23:57:18 +00:00
package api
import (
"crypto/rand"
2023-11-29 03:01:49 +00:00
"embed"
"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"
"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"
"github.com/microcosm-cc/bluemonday"
log "github.com/sirupsen/logrus"
"reichard.io/antholume/config"
"reichard.io/antholume/database"
2023-09-18 23:57:18 +00:00
)
type API struct {
Router *gin.Engine
Config *config.Config
DB *database.DBManager
HTMLPolicy *bluemonday.Policy
2023-11-29 03:01:49 +00:00
Assets *embed.FS
2023-12-01 12:35:51 +00:00
Templates map[string]*template.Template
2023-09-18 23:57:18 +00:00
}
2023-12-01 12:35:51 +00:00
func NewApi(db *database.DBManager, c *config.Config, assets *embed.FS) *API {
2023-09-18 23:57:18 +00:00
api := &API{
HTMLPolicy: bluemonday.StrictPolicy(),
Router: gin.New(),
Config: c,
DB: db,
2023-12-01 12:35:51 +00:00
Assets: assets,
2023-09-18 23:57:18 +00:00
}
// Add Logger
api.Router.Use(apiLogger())
2023-09-18 23:57:18 +00:00
// Assets & Web App Templates
2023-11-29 03:01:49 +00:00
assetsDir, _ := fs.Sub(assets, "assets")
api.Router.StaticFS("/assets", http.FS(assetsDir))
2023-09-18 23:57:18 +00:00
// Generate Secure Token
var newToken []byte
var err error
if c.CookieSessionKey != "" {
log.Info("[NewApi] Utilizing Environment Cookie Session Key")
newToken = []byte(c.CookieSessionKey)
} else {
log.Info("[NewApi] Generating Cookie Session Key")
newToken, err = generateToken(64)
if err != nil {
panic("Unable to generate secure token")
}
2023-09-18 23:57:18 +00:00
}
// Configure Cookie Session Store
store := cookie.NewStore(newToken)
store.Options(sessions.Options{
MaxAge: 60 * 60 * 24 * 7,
Secure: c.CookieSecure,
HttpOnly: c.CookieHTTPOnly,
2023-09-18 23:57:18 +00:00
SameSite: http.SameSiteStrictMode,
})
api.Router.Use(sessions.Sessions("token", store))
// Register Web App Route
api.registerWebAppRoutes()
// Register API Routes
apiGroup := api.Router.Group("/api")
api.registerKOAPIRoutes(apiGroup)
api.registerOPDSRoutes(apiGroup)
2023-09-18 23:57:18 +00:00
return api
}
func (api *API) registerWebAppRoutes() {
2023-11-29 01:05:50 +00:00
// Generate Templates
api.Router.HTMLRender = *api.generateTemplates()
2023-09-18 23:57:18 +00:00
2023-10-31 10:28:22 +00:00
// Static Assets (Required @ Root)
api.Router.GET("/manifest.json", api.appWebManifest)
api.Router.GET("/favicon.ico", api.appFaviconIcon)
api.Router.GET("/sw.js", api.appServiceWorker)
2023-10-29 00:07:24 +00:00
2023-10-31 10:28:22 +00:00
// Local / Offline Static Pages (No Template, No Auth)
api.Router.GET("/local", api.appLocalDocuments)
// Reader (Reader Page, Document Progress, Devices)
api.Router.GET("/reader", api.appDocumentReader)
api.Router.GET("/reader/devices", api.authWebAppMiddleware, api.appGetDevices)
api.Router.GET("/reader/progress/:document", api.authWebAppMiddleware, api.appGetDocumentProgress)
2023-10-29 00:07:24 +00:00
2023-10-31 10:28:22 +00:00
// Web App
api.Router.GET("/", api.authWebAppMiddleware, api.appGetHome)
api.Router.GET("/activity", api.authWebAppMiddleware, api.appGetActivity)
api.Router.GET("/progress", api.authWebAppMiddleware, api.appGetProgress)
api.Router.GET("/documents", api.authWebAppMiddleware, api.appGetDocuments)
api.Router.GET("/documents/:document", api.authWebAppMiddleware, api.appGetDocument)
2023-09-18 23:57:18 +00:00
api.Router.GET("/documents/:document/cover", api.authWebAppMiddleware, api.getDocumentCover)
2023-10-31 10:28:22 +00:00
api.Router.GET("/documents/:document/file", api.authWebAppMiddleware, api.downloadDocument)
api.Router.GET("/login", api.appGetLogin)
api.Router.GET("/logout", api.authWebAppMiddleware, api.appAuthLogout)
api.Router.GET("/register", api.appGetRegister)
api.Router.GET("/settings", api.authWebAppMiddleware, api.appGetSettings)
api.Router.GET("/admin/logs", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appGetAdminLogs)
api.Router.GET("/admin", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appGetAdmin)
api.Router.POST("/admin", api.authWebAppMiddleware, api.authAdminWebAppMiddleware, api.appPerformAdminAction)
api.Router.POST("/login", api.appAuthFormLogin)
api.Router.POST("/register", api.appAuthFormRegister)
2023-10-31 10:28:22 +00:00
// Demo Mode Enabled Configuration
if api.Config.DemoMode {
api.Router.POST("/documents", api.authWebAppMiddleware, api.appDemoModeError)
api.Router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.appDemoModeError)
api.Router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.appDemoModeError)
api.Router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.appDemoModeError)
api.Router.POST("/settings", api.authWebAppMiddleware, api.appDemoModeError)
2023-10-31 10:28:22 +00:00
} else {
api.Router.POST("/documents", api.authWebAppMiddleware, api.appUploadNewDocument)
api.Router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.appDeleteDocument)
api.Router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.appEditDocument)
api.Router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.appIdentifyDocument)
api.Router.POST("/settings", api.authWebAppMiddleware, api.appEditSettings)
2023-10-31 10:28:22 +00:00
}
2023-10-07 01:25:56 +00:00
2023-10-31 10:28:22 +00:00
// Search Enabled Configuration
2023-10-07 01:25:56 +00:00
if api.Config.SearchEnabled {
api.Router.GET("/search", api.authWebAppMiddleware, api.appGetSearch)
api.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")
2023-10-31 10:28:22 +00:00
// KO Sync Routes (WebApp Uses - Progress & Activity)
koGroup.GET("/documents/:document/file", api.authKOMiddleware, api.downloadDocument)
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)
koGroup.POST("/users/create", api.koCreateUser)
koGroup.PUT("/syncs/progress", api.authKOMiddleware, api.koSetProgress)
2023-10-31 10:28:22 +00:00
// Demo Mode Enabled Configuration
if api.Config.DemoMode {
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 {
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
}
}
func (api *API) registerOPDSRoutes(apiGroup *gin.RouterGroup) {
opdsGroup := apiGroup.Group("/opds")
2023-10-31 10:28:22 +00:00
// OPDS Routes
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)
opdsGroup.GET("/documents/:document/cover", api.authOPDSMiddleware, api.getDocumentCover)
2023-10-31 10:28:22 +00:00
opdsGroup.GET("/documents/:document/file", api.authOPDSMiddleware, api.downloadDocument)
2023-09-18 23:57:18 +00:00
}
2023-11-29 01:05:50 +00:00
func (api *API) generateTemplates() *multitemplate.Renderer {
// 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{
"GetSVGGraphData": getSVGGraphData,
"GetUTCOffsets": getUTCOffsets,
"NiceSeconds": niceSeconds,
"dict": dict,
}
// Load Base
2023-11-29 03:01:49 +00:00
b, _ := api.Assets.ReadFile("templates/base.html")
2023-11-29 01:05:50 +00:00
baseTemplate := template.Must(template.New("base").Funcs(helperFuncs).Parse(string(b)))
// Load SVGs
2023-11-29 03:01:49 +00:00
svgs, _ := api.Assets.ReadDir("templates/svgs")
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))
2023-11-29 03:01:49 +00:00
b, _ := api.Assets.ReadFile(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
}
// Load Components
2023-11-29 03:01:49 +00:00
components, _ := api.Assets.ReadDir("templates/components")
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
2023-11-29 03:01:49 +00:00
b, _ := api.Assets.ReadFile(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
}
// Load Pages
2023-11-29 03:01:49 +00:00
pages, _ := api.Assets.ReadDir("templates/pages")
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
2023-11-29 03:01:49 +00:00
b, _ := api.Assets.ReadFile(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
}
2023-12-01 12:35:51 +00:00
api.Templates = templates
2023-11-29 01:05:50 +00:00
return &render
}
func apiLogger() gin.HandlerFunc {
return func(c *gin.Context) {
// Start Timer
startTime := time.Now()
// Process Request
c.Next()
// End Timer
endTime := time.Now()
latency := endTime.Sub(startTime).Round(time.Microsecond)
2024-01-10 02:36:36 +00:00
// Get Username
var auth authData
if data, _ := c.Get("Authorization"); data != nil {
auth = data.(authData)
}
username := auth.UserName
if username != "" {
username = " (" + username + ")"
}
// Log Result
2024-01-10 02:36:36 +00:00
log.Infof("[HTTPRouter] %-15s (%10s) %d %7s %s%s",
c.ClientIP(),
latency,
c.Writer.Status(),
c.Request.Method,
c.Request.URL.Path,
username,
)
}
}
2023-09-18 23:57:18 +00:00
func generateToken(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}