switch to atreugo
1 file changed, 46 insertions(+), 87 deletions(-)
changed files
M routes/handler.go → routes/handler.go
@@ -1,79 +1,24 @@ package routes import ( + "fmt" "io/fs" "log" - "net/http" - "path" + "net" "path/filepath" + "strconv" + "time" - "github.com/dimfeld/httptreemux/v5" "github.com/fsnotify/fsnotify" + "github.com/savsgio/atreugo/v11" "alin.ovh/elgit/config" - "alin.ovh/elgit/templates" ) -var ErrNotAllowed = templates.Error{ - Code: http.StatusMethodNotAllowed, - Message: "that's not possible.", -} - -// Checks for gitprotocol-http(5) specific smells; if found, passes -// the request on to the git http service, else render the web frontend. -func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request, params map[string]string) { - rest := params["rest"] - switch params["name"] { - case "info", "git-upload-pack": - rest = path.Join(params["name"], params["rest"]) - params["name"] = params["category"] - params["category"] = "" - } +var ErrMethodNotAllowed = fmt.Errorf("that's not possible") +var ErrNotFound = fmt.Errorf("nothing like that here") - if d.isNotAllowed(path.Join(params["category"], params["name"])) { - d.Write404(w) - - return - } - - if r.URL.RawQuery == "service=git-receive-pack" { - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("no pushing allowed!")) - if err != nil { - log.Println(err) - d.Write500(w) - - return - } - - return - } - - switch r.Method { - case http.MethodGet: - switch { - case rest == "info/refs" && - r.URL.RawQuery == "service=git-upload-pack": - d.InfoRefs(w, r, params) - default: - d.RepoIndex(w, r, params) - } - - case http.MethodPost: - switch rest { - case "git-upload-pack": - d.UploadPack(w, r, params) - default: - d.WriteError(w, ErrNotAllowed) - } - default: - d.WriteError(w, ErrNotAllowed) - } -} - -func Handlers(c *config.Config, staticFiles fs.FS) *httptreemux.TreeMux { - mux := httptreemux.New() - +func Handlers(c *config.Config, staticFiles fs.FS) *atreugo.Atreugo { plist := filepath.Join(c.Repo.Root, "projects.list") projects, err := ReadProjectsList(plist) if err != nil {@@ -84,6 +29,27 @@ d := deps{ c, projects, } + + addr := net.JoinHostPort(c.Server.Host, strconv.FormatInt(c.Server.Port, 10)) + srv := atreugo.New(atreugo.Config{ + Addr: addr, + Name: c.Server.Name, + Network: "tcp6", + Prefork: false, + Reuseport: false, + GracefulShutdown: true, + Compress: true, + Logger: log.Default(), + NotFoundView: d.NotFound, + MethodNotAllowedView: nil, + ErrorView: d.Error, + // PanicView: d.Panic, + ReadTimeout: 5 * time.Second, + DisablePreParseMultipartForm: true, + KeepHijackedConns: false, + CloseOnShutdown: true, + StreamRequestBody: true, + }) watcher, err := fsnotify.NewWatcher() if err != nil {@@ -117,32 +83,25 @@ } } }() - mux.NotFoundHandler = func(w http.ResponseWriter, _ *http.Request) { - d.Write404(w) - } - - mux.GET("/", d.Index) - mux.NewGroup("/static/"). - UsingContext(). - Handler("GET", "/*", http.FileServerFS(staticFiles)) + d.mountRepoPaths(srv.NewGroupPath("/{name}")) + d.mountRepoPaths(srv.NewGroupPath("/{category}/{name}")) - d.mountRepoPaths(mux.NewGroup("/:name")) - d.mountRepoPaths(mux.NewGroup("/:category/:name")) + srv.StaticFS("/static/", staticFiles) + srv.GET("/", d.Index) - return mux + return srv } -func (d deps) mountRepoPaths(group *httptreemux.Group) { - group.GET("/tree/:ref/*rest", d.RepoTree) - group.GET("/blob/:ref/*rest", d.FileContent) - group.GET("/tree/:ref/", d.RepoTree) - group.GET("/log/:ref/", d.Log) - group.GET("/archive/:file", d.Archive) - group.GET("/commit/:ref", d.Diff) - group.GET("/commit/:ref/*file", d.FileDiff) - group.GET("/refs/", d.Refs) - group.GET("", d.Multiplex) - group.POST("", d.Multiplex) - group.GET("/*rest", d.Multiplex) - group.POST("/*rest", d.Multiplex) +func (d deps) mountRepoPaths(router *atreugo.Router) { + router.GET("/tree/{ref}/{rest:*}", d.RepoTree) + router.GET("/blob/{ref}/{rest:*}", d.FileContent) + router.GET("/tree/{ref}/", d.RepoTree) + router.GET("/log/{ref}/", d.Log) + router.GET("/archive/{file}", d.Archive) + router.GET("/commit/{ref}", d.Diff) + router.GET("/commit/{ref}/{file:*}", d.FileDiff) + router.GET("/refs/", d.Refs) + router.GET("/info/refs", d.InfoRefs) + router.POST("/git-upload-pack", d.UploadPack) + router.GET("/", d.RepoIndex) }