mirror of
1
Fork 0

[bugfix] Fix bug where admin panel could not be accessed at `/admin` (#427)

* clarify comments

* tidy up static serving + add /admin redirect
This commit is contained in:
tobi 2022-03-13 18:35:26 +01:00 committed by GitHub
parent e306233166
commit 4b4c935e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 12 deletions

View File

@ -47,7 +47,7 @@ type Router interface {
AttachMiddleware(handler gin.HandlerFunc) AttachMiddleware(handler gin.HandlerFunc)
// Attach 404 NoRoute handler // Attach 404 NoRoute handler
AttachNoRouteHandler(handler gin.HandlerFunc) AttachNoRouteHandler(handler gin.HandlerFunc)
// Add Gin StaticFile handler // Add Gin StaticFS handler
AttachStaticFS(relativePath string, fs http.FileSystem) AttachStaticFS(relativePath string, fs http.FileSystem)
// Start the router // Start the router
Start() Start()
@ -62,7 +62,7 @@ type router struct {
certManager *autocert.Manager certManager *autocert.Manager
} }
// Add Gin StaticFile handler // Add Gin StaticFS handler
func (r *router) AttachStaticFS(relativePath string, fs http.FileSystem) { func (r *router) AttachStaticFS(relativePath string, fs http.FileSystem) {
r.engine.StaticFS(relativePath, fs) r.engine.StaticFS(relativePath, fs)
} }

View File

@ -21,7 +21,6 @@ package web
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -88,18 +87,24 @@ func (m *Module) NotFoundHandler(c *gin.Context) {
// Route satisfies the RESTAPIModule interface // Route satisfies the RESTAPIModule interface
func (m *Module) Route(s router.Router) error { func (m *Module) Route(s router.Router) error {
// serve static files from /assets // serve static files from assets dir at /assets
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current working directory: %s", err)
}
assetBaseDir := viper.GetString(config.Keys.WebAssetBaseDir) assetBaseDir := viper.GetString(config.Keys.WebAssetBaseDir)
assetPath := filepath.Join(cwd, assetBaseDir) if assetBaseDir == "" {
return fmt.Errorf("%s cannot be empty and must be a relative or absolute path", config.Keys.WebAssetBaseDir)
}
assetPath, err := filepath.Abs(assetBaseDir)
if err != nil {
return fmt.Errorf("error getting absolute path of %s: %s", assetBaseDir, err)
}
s.AttachStaticFS("/assets", fileSystem{http.Dir(assetPath)}) s.AttachStaticFS("/assets", fileSystem{http.Dir(assetPath)})
// Admin panel route, if it exists // serve admin panel from within assets dir at /admin/
adminPath := filepath.Join(cwd, assetBaseDir, "/admin") // and redirect /admin to /admin/
s.AttachStaticFS("/admin", fileSystem{http.Dir(adminPath)}) adminPath := filepath.Join(assetPath, "admin")
s.AttachStaticFS("/admin/", fileSystem{http.Dir(adminPath)})
s.AttachHandler(http.MethodGet, "/admin", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/admin/")
})
// serve front-page // serve front-page
s.AttachHandler(http.MethodGet, "/", m.baseHandler) s.AttachHandler(http.MethodGet, "/", m.baseHandler)