mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00

* refactor: remove baseUrl from api calls and sseBaseUrl * refactor: set cookie session to '/'. Since that's where the api endpoint is that way we set it to the root domain, we can't set it to the subfolder since the api is called directly now and not using the baseUrl. * feat: add the baseUrl route. When user for example is in `/autobrr` and hit reload it should just return the index.html. * refactor: now it have to be `/autobrr` Remove the trailing `/`, now base url is set to /autobrr aligned with other arrs. * refactor: remove baseUrl stuff. * refactor: use separate router for the api endpoint and the baseUrl. I don't think we need separate router, but I didn't test it, so feel free to test it and see if it works without the separate router, the whole point was to make sure that it's not prefixed with baseUrl and I noticed that it was being called in the frontend `APIClients.ts`. So yea just check if it works without it then keep the old one. Also removed the index since it was zombie code not being used anywhere. * feat: Dynamic base url. * fix: auth handler deps * feat(http): mount web and api on baseurl * feat(http): web api client routes * feat(http): baseurl legacy mode * feat(http): baseurl legacy mode test * feat(http): add assetBaseUrl * feat(http): try separate web handlers * feat(http): improve file serving * feat(http): ignore .gitkeep * fix(assets): windows paths * fix(assets): windows paths trimprefix * fix(assets): windows paths join * fix(assets): cleanup * fix(assets): additional web route check * feat(http): add comments --------- Co-authored-by: ze0s <ze0s@riseup.net>
201 lines
4.9 KiB
Go
201 lines
4.9 KiB
Go
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
package http
|
|
|
|
import (
|
|
"html/template"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
filePath "path"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type webLegacyHandler struct {
|
|
log zerolog.Logger
|
|
embedFS fs.FS
|
|
baseUrl string
|
|
assetBaseURL string
|
|
version string
|
|
|
|
files map[string]string
|
|
}
|
|
|
|
func newWebLegacyHandler(log zerolog.Logger, embedFS fs.FS, version, baseURL, assetBaseURL string) *webLegacyHandler {
|
|
return &webLegacyHandler{
|
|
log: log.With().Str("module", "web-assets").Logger(),
|
|
embedFS: embedFS,
|
|
baseUrl: baseURL,
|
|
assetBaseURL: assetBaseURL,
|
|
version: version,
|
|
files: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
// registerAssets walks the FS Dist dir and registers each file as a route
|
|
func (h *webLegacyHandler) registerAssets(r *chi.Mux) {
|
|
err := fs.WalkDir(h.embedFS, ".", func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if d.IsDir() {
|
|
//h.log.Trace().Msgf("web assets: skip dir: %s", d.Name())
|
|
return nil
|
|
}
|
|
|
|
h.log.Trace().Msgf("web assets: found path: %s", path)
|
|
|
|
// ignore index.html, so we can render it as a template and inject variables
|
|
if path == "index.html" || path == "manifest.webmanifest" || path == ".gitkeep" {
|
|
return nil
|
|
}
|
|
|
|
// use old path.Join to not be os specific
|
|
FileFS(r, filePath.Join("/", path), path, h.embedFS)
|
|
|
|
h.files[path] = path
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
func (h *webLegacyHandler) RegisterRoutes(r *chi.Mux) {
|
|
h.registerAssets(r)
|
|
|
|
// Serve static files without a prefix
|
|
assets, err := fs.Sub(h.embedFS, "assets")
|
|
if err != nil {
|
|
h.log.Error().Err(err).Msg("could not load assets sub dir")
|
|
}
|
|
|
|
StaticFS(r, "/assets", assets)
|
|
|
|
p := IndexParams{
|
|
Title: "Dashboard",
|
|
Version: h.version,
|
|
BaseUrl: h.baseUrl,
|
|
AssetBaseUrl: h.assetBaseURL,
|
|
}
|
|
|
|
// serve on base route
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
if err := h.RenderIndex(w, p); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
// handle manifest
|
|
r.Get("/manifest.webmanifest", func(w http.ResponseWriter, r *http.Request) {
|
|
if err := h.RenderManifest(w, p); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
// handle all other routes and files
|
|
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
|
file := strings.TrimPrefix(r.RequestURI, h.baseUrl)
|
|
|
|
if strings.Contains(file, "/assets") {
|
|
if strings.HasPrefix(file, "/assets/") {
|
|
fsFile(w, r, file, h.embedFS)
|
|
return
|
|
}
|
|
|
|
parts := strings.SplitAfter(file, "/assets/")
|
|
if len(parts) > 1 {
|
|
fsFile(w, r, "assets/"+parts[1], h.embedFS)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
if strings.Contains(file, "favicon.ico") {
|
|
fsFile(w, r, "favicon.ico", h.embedFS)
|
|
return
|
|
}
|
|
|
|
if strings.Contains(file, "Inter-Variable.woff2") {
|
|
fsFile(w, r, "Inter-Variable.woff2", h.embedFS)
|
|
return
|
|
}
|
|
|
|
// if valid web route then serve html
|
|
if validWebRoute(file) || file == "index.html" {
|
|
if err := h.RenderIndex(w, p); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// if not valid web route then try and serve files
|
|
fsFile(w, r, file, h.embedFS)
|
|
})
|
|
}
|
|
|
|
func (h *webLegacyHandler) RenderIndex(w io.Writer, p IndexParams) error {
|
|
return h.parseIndex().Execute(w, p)
|
|
}
|
|
|
|
func (h *webLegacyHandler) parseIndex() *template.Template {
|
|
return template.Must(template.New("index.html").ParseFS(h.embedFS, "index.html"))
|
|
}
|
|
|
|
func (h *webLegacyHandler) RenderManifest(w io.Writer, p IndexParams) error {
|
|
return h.parseManifest().Execute(w, p)
|
|
}
|
|
|
|
func (h *webLegacyHandler) parseManifest() *template.Template {
|
|
return template.Must(template.New("manifest.webmanifest").ParseFS(h.embedFS, "manifest.webmanifest"))
|
|
}
|
|
|
|
func (h *webLegacyHandler) RenderFallbackIndex(w io.Writer) error {
|
|
p := IndexParams{
|
|
Title: "autobrr Dashboard",
|
|
Version: h.version,
|
|
BaseUrl: h.baseUrl,
|
|
AssetBaseUrl: h.assetBaseURL,
|
|
}
|
|
return h.parseFallbackIndex().Execute(w, p)
|
|
}
|
|
|
|
func (h *webLegacyHandler) parseFallbackIndex() *template.Template {
|
|
return template.Must(template.New("fallback-index").Parse(`<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>autobrr</title>
|
|
<style>
|
|
@media (prefers-color-scheme: dark) {
|
|
body {
|
|
color:#fff;
|
|
background:#333333
|
|
}
|
|
|
|
a {
|
|
color: #3d70ea
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<span>Must use base url: <a href="{{.BaseUrl}}">{{.BaseUrl}}</a></span>
|
|
</body>
|
|
</html>
|
|
`))
|
|
}
|
|
|
|
// StaticFS registers a new route with path prefix to serve static files from the provided file system.
|
|
func StaticFS(r *chi.Mux, pathPrefix string, filesystem fs.FS) {
|
|
r.Handle(pathPrefix+"*", http.StripPrefix(pathPrefix, http.FileServer(http.FS(filesystem))))
|
|
}
|