all repos — elgit @ 7bea4a8f49a9f3aa9386b7e7d24a2a78beb2f7e9

fork of legit: web frontend for git, written in go

embed static files

Alan Pearce
commit

7bea4a8f49a9f3aa9386b7e7d24a2a78beb2f7e9

parent

5ff42d78653ea018f8855de4768cf872ce41e41e

5 files changed, 11 insertions(+), 34 deletions(-)

changed files
M config.yamlconfig.yaml
@@ -8,8 +8,6 @@ - README.md
mainBranch: - master - main -dirs: - static: ./static meta: title: Alin's Git Repositories description: rendered with elgit
M config/config.goconfig/config.go
@@ -3,7 +3,6 @@
import ( "fmt" "os" - "path/filepath" "gopkg.in/yaml.v3" )
@@ -13,10 +12,6 @@ Root string `yaml:"root"`
Readme []string `yaml:"readme"` MainBranch []string `yaml:"mainBranch"` Unlisted []string `yaml:"unlisted,omitempty"` -} - -type Dirs struct { - Static string `yaml:"static"` } type Meta struct {
@@ -33,7 +28,6 @@ }
type Config struct { Repo Repo `yaml:"repo"` - Dirs Dirs `yaml:"dirs"` Meta Meta `yaml:"meta"` Server Server `yaml:"server"` }
@@ -47,10 +41,6 @@
c := Config{} if err := yaml.Unmarshal(b, &c); err != nil { return nil, fmt.Errorf("parsing config: %w", err) - } - - if c.Dirs.Static, err = filepath.Abs(c.Dirs.Static); err != nil { - return nil, err } return &c, nil
M main.gomain.go
@@ -1,6 +1,7 @@
package main import ( + "embed" "flag" "log" "net"
@@ -10,6 +11,9 @@
"alin.ovh/elgit/config" "alin.ovh/elgit/routes" ) + +//go:embed static/* +var staticFiles embed.FS func main() { var cfg string
@@ -23,15 +27,11 @@ if err != nil {
log.Fatal(err) } - if err := UnveilPaths([]string{ - c.Dirs.Static, - c.Repo.Root, - }, - "r"); err != nil { + if err := UnveilPaths([]string{c.Repo.Root}, "r"); err != nil { log.Fatalf("unveil: %s", err) } - mux := routes.Handlers(c) + mux := routes.Handlers(c, staticFiles) addr := net.JoinHostPort(c.Server.Host, strconv.FormatInt(int64(c.Server.Port), 10)) log.Println("starting server on", addr) log.Fatal(http.ListenAndServe(addr, mux))
M routes/handler.goroutes/handler.go
@@ -1,6 +1,7 @@
package routes import ( + "io/fs" "log" "net/http" "path"
@@ -58,7 +59,7 @@ }
} } -func Handlers(c *config.Config) *httptreemux.TreeMux { +func Handlers(c *config.Config, staticFiles fs.FS) *httptreemux.TreeMux { mux := httptreemux.New() plist := filepath.Join(c.Repo.Root, "projects.list")
@@ -111,7 +112,9 @@
mux.RedirectTrailingSlash = false mux.GET("/", d.Index) - mux.GET("/static/:file", d.ServeStatic) + mux.NewGroup("/static/"). + UsingContext(). + Handler("GET", "/*", http.FileServerFS(staticFiles)) mux.GET("/:name/tree/:ref/*rest", d.RepoTree) mux.GET("/:name/blob/:ref/*rest", d.FileContent)
M routes/routes.goroutes/routes.go
@@ -13,8 +13,6 @@
"alin.ovh/elgit/config" "alin.ovh/elgit/git" "alin.ovh/elgit/templates" - securejoin "github.com/cyphar/filepath-securejoin" - "github.com/dimfeld/httptreemux/v5" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday/v2" )
@@ -435,15 +433,3 @@ log.Println(err)
return } } - -func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request, params map[string]string) { - f := params["file"] - f = httptreemux.Clean(f) - f, err := securejoin.SecureJoin(d.c.Dirs.Static, f) - if err != nil { - d.Write404(w) - return - } - - http.ServeFile(w, r, f) -}