2023-09-18 23:57:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"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"
|
2023-09-18 23:57:18 +00:00
|
|
|
"reichard.io/bbank/config"
|
|
|
|
"reichard.io/bbank/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2023-09-23 02:12:36 +00:00
|
|
|
Router *gin.Engine
|
|
|
|
Config *config.Config
|
|
|
|
DB *database.DBManager
|
|
|
|
HTMLPolicy *bluemonday.Policy
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewApi(db *database.DBManager, c *config.Config) *API {
|
|
|
|
api := &API{
|
2023-09-23 18:14:57 +00:00
|
|
|
HTMLPolicy: bluemonday.StrictPolicy(),
|
2023-09-23 02:12:36 +00:00
|
|
|
Router: gin.Default(),
|
|
|
|
Config: c,
|
|
|
|
DB: db,
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assets & Web App Templates
|
|
|
|
api.Router.Static("/assets", "./assets")
|
|
|
|
|
|
|
|
// Generate Secure Token
|
2023-09-23 02:12:36 +00:00
|
|
|
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{
|
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,
|
|
|
|
})
|
|
|
|
api.Router.Use(sessions.Sessions("token", store))
|
|
|
|
|
|
|
|
// Register Web App Route
|
|
|
|
api.registerWebAppRoutes()
|
|
|
|
|
|
|
|
// Register API Routes
|
|
|
|
apiGroup := api.Router.Group("/api")
|
|
|
|
api.registerKOAPIRoutes(apiGroup)
|
2023-10-05 23:56:19 +00:00
|
|
|
api.registerOPDSRoutes(apiGroup)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) registerWebAppRoutes() {
|
|
|
|
// Define Templates & Helper Functions
|
|
|
|
render := multitemplate.NewRenderer()
|
|
|
|
helperFuncs := template.FuncMap{
|
2023-10-24 00:18:16 +00:00
|
|
|
"GetSVGGraphData": getSVGGraphData,
|
|
|
|
"GetUTCOffsets": getUTCOffsets,
|
|
|
|
"NiceSeconds": niceSeconds,
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 10:28:22 +00:00
|
|
|
// Templates
|
2023-10-25 23:52:01 +00:00
|
|
|
render.AddFromFiles("error", "templates/error.html")
|
2023-10-31 10:28:22 +00:00
|
|
|
render.AddFromFilesFuncs("activity", helperFuncs, "templates/base.html", "templates/activity.html")
|
|
|
|
render.AddFromFilesFuncs("document", helperFuncs, "templates/base.html", "templates/document.html")
|
|
|
|
render.AddFromFilesFuncs("documents", helperFuncs, "templates/base.html", "templates/documents.html")
|
2023-09-18 23:57:18 +00:00
|
|
|
render.AddFromFilesFuncs("home", helperFuncs, "templates/base.html", "templates/home.html")
|
2023-10-31 10:28:22 +00:00
|
|
|
render.AddFromFilesFuncs("login", helperFuncs, "templates/login.html")
|
2023-10-07 01:25:56 +00:00
|
|
|
render.AddFromFilesFuncs("search", helperFuncs, "templates/base.html", "templates/search.html")
|
2023-09-27 22:58:47 +00:00
|
|
|
render.AddFromFilesFuncs("settings", helperFuncs, "templates/base.html", "templates/settings.html")
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
api.Router.HTMLRender = render
|
|
|
|
|
2023-10-31 10:28:22 +00:00
|
|
|
// Static Assets (Required @ Root)
|
2023-09-18 23:57:18 +00:00
|
|
|
api.Router.GET("/manifest.json", api.webManifest)
|
2023-10-29 00:07:24 +00:00
|
|
|
api.Router.GET("/sw.js", api.serviceWorker)
|
|
|
|
|
2023-10-31 10:28:22 +00:00
|
|
|
// Local / Offline Static Pages (No Template, No Auth)
|
2023-10-30 23:23:38 +00:00
|
|
|
api.Router.GET("/local", api.localDocuments)
|
2023-10-29 00:07:24 +00:00
|
|
|
api.Router.GET("/reader", api.documentReader)
|
|
|
|
|
2023-10-31 10:28:22 +00:00
|
|
|
// Web App
|
2023-09-18 23:57:18 +00:00
|
|
|
api.Router.GET("/", api.authWebAppMiddleware, api.createAppResourcesRoute("home"))
|
2023-09-21 00:35:01 +00:00
|
|
|
api.Router.GET("/activity", api.authWebAppMiddleware, api.createAppResourcesRoute("activity"))
|
2023-09-23 02:12:36 +00:00
|
|
|
api.Router.GET("/documents", api.authWebAppMiddleware, api.createAppResourcesRoute("documents"))
|
|
|
|
api.Router.GET("/documents/:document", api.authWebAppMiddleware, api.createAppResourcesRoute("document"))
|
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)
|
2023-10-29 00:07:24 +00:00
|
|
|
api.Router.GET("/documents/:document/progress", api.authWebAppMiddleware, api.getDocumentProgress)
|
2023-10-31 10:28:22 +00:00
|
|
|
api.Router.GET("/login", api.createAppResourcesRoute("login"))
|
|
|
|
api.Router.GET("/logout", api.authWebAppMiddleware, api.authLogout)
|
|
|
|
api.Router.GET("/register", api.createAppResourcesRoute("login", gin.H{"Register": true}))
|
|
|
|
api.Router.GET("/settings", api.authWebAppMiddleware, api.createAppResourcesRoute("settings"))
|
|
|
|
api.Router.POST("/login", api.authFormLogin)
|
|
|
|
api.Router.POST("/register", api.authFormRegister)
|
|
|
|
|
|
|
|
// Demo Mode Enabled Configuration
|
|
|
|
if api.Config.DemoMode {
|
|
|
|
api.Router.POST("/documents", api.authWebAppMiddleware, api.demoModeAppError)
|
|
|
|
api.Router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.demoModeAppError)
|
|
|
|
api.Router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.demoModeAppError)
|
|
|
|
api.Router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.demoModeAppError)
|
|
|
|
api.Router.POST("/settings", api.authWebAppMiddleware, api.demoModeAppError)
|
|
|
|
} else {
|
|
|
|
api.Router.POST("/documents", api.authWebAppMiddleware, api.uploadNewDocument)
|
|
|
|
api.Router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.deleteDocument)
|
|
|
|
api.Router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.editDocument)
|
|
|
|
api.Router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.identifyDocument)
|
|
|
|
api.Router.POST("/settings", api.authWebAppMiddleware, api.editSettings)
|
|
|
|
}
|
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.createAppResourcesRoute("search"))
|
|
|
|
api.Router.POST("/search", api.authWebAppMiddleware, api.saveNewDocument)
|
|
|
|
}
|
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)
|
2023-10-25 23:52:01 +00:00
|
|
|
koGroup.GET("/documents/:document/file", api.authKOMiddleware, api.downloadDocument)
|
2023-10-31 10:28:22 +00:00
|
|
|
koGroup.GET("/syncs/progress/:document", api.authKOMiddleware, api.getProgress)
|
|
|
|
koGroup.GET("/users/auth", api.authKOMiddleware, api.authorizeUser)
|
2023-10-05 23:56:19 +00:00
|
|
|
koGroup.POST("/activity", api.authKOMiddleware, api.addActivities)
|
|
|
|
koGroup.POST("/syncs/activity", api.authKOMiddleware, api.checkActivitySync)
|
2023-10-31 10:28:22 +00:00
|
|
|
koGroup.POST("/users/create", api.createUser)
|
|
|
|
koGroup.PUT("/syncs/progress", api.authKOMiddleware, api.setProgress)
|
|
|
|
|
|
|
|
// Demo Mode Enabled Configuration
|
|
|
|
if api.Config.DemoMode {
|
|
|
|
koGroup.POST("/documents", api.authKOMiddleware, api.demoModeJSONError)
|
|
|
|
koGroup.POST("/syncs/documents", api.authKOMiddleware, api.demoModeJSONError)
|
|
|
|
koGroup.PUT("/documents/:document/file", api.authKOMiddleware, api.demoModeJSONError)
|
|
|
|
} else {
|
|
|
|
koGroup.POST("/documents", api.authKOMiddleware, api.addDocuments)
|
|
|
|
koGroup.POST("/syncs/documents", api.authKOMiddleware, api.checkDocumentsSync)
|
|
|
|
koGroup.PUT("/documents/:document/file", api.authKOMiddleware, api.uploadExistingDocument)
|
|
|
|
}
|
2023-10-05 23:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) registerOPDSRoutes(apiGroup *gin.RouterGroup) {
|
|
|
|
opdsGroup := apiGroup.Group("/opds")
|
|
|
|
|
2023-10-31 10:28:22 +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)
|
2023-10-05 23:56:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func generateToken(n int) ([]byte, error) {
|
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|