diff options
| author | 2022-03-13 18:35:26 +0100 | |
|---|---|---|
| committer | 2022-03-13 18:35:26 +0100 | |
| commit | 4b4c935e02cee98d06b8824fc3751dfc88603e52 (patch) | |
| tree | 2b3fdf6d47593a360acf9cc5bb9581fea7a55030 | |
| parent | [bugfix] Fix html-escaped characters in content warnings (#426) (diff) | |
| download | gotosocial-4b4c935e02cee98d06b8824fc3751dfc88603e52.tar.xz | |
[bugfix] Fix bug where admin panel could not be accessed at `/admin` (#427)
* clarify comments
* tidy up static serving + add /admin redirect
| -rw-r--r-- | internal/router/router.go | 4 | ||||
| -rw-r--r-- | internal/web/base.go | 23 | 
2 files changed, 16 insertions, 11 deletions
diff --git a/internal/router/router.go b/internal/router/router.go index 4b00f0eb0..57e0d36b2 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -47,7 +47,7 @@ type Router interface {  	AttachMiddleware(handler gin.HandlerFunc)  	// Attach 404 NoRoute handler  	AttachNoRouteHandler(handler gin.HandlerFunc) -	// Add Gin StaticFile handler +	// Add Gin StaticFS handler  	AttachStaticFS(relativePath string, fs http.FileSystem)  	// Start the router  	Start() @@ -62,7 +62,7 @@ type router struct {  	certManager *autocert.Manager  } -// Add Gin StaticFile handler +// Add Gin StaticFS handler  func (r *router) AttachStaticFS(relativePath string, fs http.FileSystem) {  	r.engine.StaticFS(relativePath, fs)  } diff --git a/internal/web/base.go b/internal/web/base.go index 72b633580..58afd40a7 100644 --- a/internal/web/base.go +++ b/internal/web/base.go @@ -21,7 +21,6 @@ package web  import (  	"fmt"  	"net/http" -	"os"  	"path/filepath"  	"github.com/gin-gonic/gin" @@ -88,18 +87,24 @@ func (m *Module) NotFoundHandler(c *gin.Context) {  // Route satisfies the RESTAPIModule interface  func (m *Module) Route(s router.Router) error { -	// serve static files from /assets -	cwd, err := os.Getwd() +	// serve static files from assets dir at /assets +	assetBaseDir := viper.GetString(config.Keys.WebAssetBaseDir) +	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 current working directory: %s", err) +		return fmt.Errorf("error getting absolute path of %s: %s", assetBaseDir, err)  	} -	assetBaseDir := viper.GetString(config.Keys.WebAssetBaseDir) -	assetPath := filepath.Join(cwd, assetBaseDir)  	s.AttachStaticFS("/assets", fileSystem{http.Dir(assetPath)}) -	// Admin panel route, if it exists -	adminPath := filepath.Join(cwd, assetBaseDir, "/admin") -	s.AttachStaticFS("/admin", fileSystem{http.Dir(adminPath)}) +	// serve admin panel from within assets dir at /admin/ +	// and redirect /admin to /admin/ +	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  	s.AttachHandler(http.MethodGet, "/", m.baseHandler)  | 
