shiori/internal/http/routes/frontend.go
Felipe Martin a60dbf3dc5
Run legacy API and new API at the same time. (#648)
* config: backwards comptabile dir

* remove duplicated frontend

* frontend: move assets to assets folder

* legacy routes handler in gin

* templates and asset in different embed

* new routes

* frontend routes serve old views

* added DTO for account object

* api auth calls legacy handler

* frontend: handle new error messages

* frontend: update urls

* frontend: login using new api

* updated frontend tests

* chore: remove debug route

* create shiori/gopher user if no owner is present

* server as default command

* serve -> server

* refactored database logic, allow database url

* removed unused configuration

* storage docs

* refactor cli to use cfg and deps

* check errors only in server

* log fatal instead of os exit

* dont default data directory to current dir

* fixed sqlite path

* trigger build on prs

* avoid releasing if lint/test fails

* pull request condition

* event -> event_name

* Get correct pull request number

* added workflow to delete dangling tags

* fix: nil error checking

* set gin mode first

* set gin mode before initialization

* fix logger

* allow version bump from custom ref

* Updated matrix link to workspace
2023-07-19 18:25:41 +02:00

83 lines
2 KiB
Go

package routes
import (
"embed"
"html/template"
"net/http"
"path/filepath"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/go-shiori/shiori/internal/config"
views "github.com/go-shiori/shiori/internal/view"
"github.com/sirupsen/logrus"
)
type assetsFS struct {
http.FileSystem
logger *logrus.Logger
}
func (fs assetsFS) Exists(prefix string, path string) bool {
_, err := fs.Open(path)
if err != nil {
logrus.WithError(err).WithField("path", path).WithField("prefix", prefix).Error("requested frontend file not found")
}
return err == nil
}
func (fs assetsFS) Open(name string) (http.File, error) {
f, err := fs.FileSystem.Open(filepath.Join("assets", name))
if err != nil {
logrus.WithError(err).WithField("path", name).Error("requested frontend file not found")
}
return f, err
}
func newAssetsFS(logger *logrus.Logger, fs embed.FS) static.ServeFileSystem {
return assetsFS{
logger: logger,
FileSystem: http.FS(fs),
}
}
type FrontendRoutes struct {
logger *logrus.Logger
cfg *config.Config
}
func (r *FrontendRoutes) loadTemplates(e *gin.Engine) {
tmpl, err := template.New("html").Delims("$$", "$$").ParseFS(views.Templates, "*.html")
if err != nil {
r.logger.WithError(err).Error("Failed to parse templates")
return
}
e.SetHTMLTemplate(tmpl)
}
func (r *FrontendRoutes) Setup(e *gin.Engine) {
group := e.Group("/")
e.Delims("$$", "$$")
r.loadTemplates(e)
// e.LoadHTMLGlob("internal/view/*.html")
group.Use(gzip.Gzip(gzip.DefaultCompression))
group.GET("/login", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "login.html", gin.H{
"RootPath": r.cfg.Http.RootPath,
})
})
group.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.html", gin.H{
"RootPath": r.cfg.Http.RootPath,
})
})
e.StaticFS("/assets", newAssetsFS(r.logger, views.Assets))
}
func NewFrontendRoutes(logger *logrus.Logger, cfg *config.Config) *FrontendRoutes {
return &FrontendRoutes{
logger: logger,
cfg: cfg,
}
}