web: NotFound route to root + support own http.FileSystem

This commit is contained in:
genofire 2021-10-25 12:56:47 +02:00
parent 023df961c2
commit 2937b93dff
2 changed files with 17 additions and 1 deletions

View File

@ -10,6 +10,8 @@ maintains a list of modules. When it runs, it executes all of its modules.
package web package web
import ( import (
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap" "go.uber.org/zap"
@ -27,7 +29,9 @@ type Service struct {
// config // config
Listen string `toml:"listen"` Listen string `toml:"listen"`
AccessLog bool `toml:"access_log"` AccessLog bool `toml:"access_log"`
WebrootIndexDisable bool `toml:"webroot_index_disable"`
Webroot string `toml:"webroot"` Webroot string `toml:"webroot"`
WebrootFS http.FileSystem `toml:"-"`
ACME struct { ACME struct {
Enable bool `toml:"enable"` Enable bool `toml:"enable"`
Domains []string `toml:"domains"` Domains []string `toml:"domains"`

View File

@ -21,5 +21,17 @@ func (ws *Service) Bind(r *gin.Engine) {
} }
ws.log.Info("bind modules", zap.Int("count", len(ws.modules))) ws.log.Info("bind modules", zap.Int("count", len(ws.modules)))
r.Use(static.Serve("/", static.LocalFile(ws.Webroot, false))) if ws.Webroot != "" {
ws.WebrootFS = static.LocalFile(ws.Webroot, false)
}
r.Use(func(c *gin.Context) {
if !ws.WebrootIndexDisable {
_, err := ws.WebrootFS.Open(c.Request.URL.Path)
if err != nil {
c.FileFromFS("/", ws.WebrootFS)
return
}
}
c.FileFromFS(c.Request.URL.Path, ws.WebrootFS)
})
} }