support requesting subdirs of repos

This commit is contained in:
Edgaru089 2021-06-13 20:45:57 +08:00
parent 76b657aa3f
commit 23af74a05e

16
main.go
View File

@ -8,6 +8,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"strings"
) )
type Repo struct { type Repo struct {
@ -43,17 +44,28 @@ type Server struct {
func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) { func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
log.Print("request: ", req.URL.Path) log.Print("request: ", req.URL.Path)
id := req.URL.Path[1:] id := req.URL.Path[1:]
for len(id) > 0 {
log.Print(" trying id=", id)
if repo, ok := s.config.Repos[id]; ok { if repo, ok := s.config.Repos[id]; ok {
err := s.template.Execute(resp, repo) err := s.template.Execute(resp, repo)
if err != nil { if err != nil {
resp.WriteHeader(500) resp.WriteHeader(500)
resp.Write([]byte("Internal Server Error\n\n")) resp.Write([]byte("Internal Server Error\n\n"))
resp.Write([]byte(fmt.Sprintf("Error: Executing Template: %s, id=%s\n\nRepo=%v", err.Error(), id, repo))) resp.Write([]byte(fmt.Sprintf("Error: Executing Template: %s, id=%s\n\nRepo=%v", err.Error(), id, repo)))
return
} }
} else { return
}
i := strings.LastIndexByte(id, '/')
if i == -1 {
break
}
id = id[:i]
}
log.Print(id, ": Error 404")
resp.WriteHeader(404) resp.WriteHeader(404)
} }
}
func main() { func main() {
var c Config var c Config