From 23af74a05e4cd6d22ecdf358b798faa5ff2c51a0 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Sun, 13 Jun 2021 20:45:57 +0800 Subject: [PATCH] support requesting subdirs of repos --- main.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index f86bc65..5158632 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "os" + "strings" ) type Repo struct { @@ -43,16 +44,27 @@ type Server struct { func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) { log.Print("request: ", req.URL.Path) id := req.URL.Path[1:] - if repo, ok := s.config.Repos[id]; ok { - err := s.template.Execute(resp, repo) - if err != nil { - resp.WriteHeader(500) - 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))) + for len(id) > 0 { + log.Print(" trying id=", id) + if repo, ok := s.config.Repos[id]; ok { + err := s.template.Execute(resp, repo) + if err != nil { + resp.WriteHeader(500) + 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))) + return + } + return } - } else { - resp.WriteHeader(404) + + i := strings.LastIndexByte(id, '/') + if i == -1 { + break + } + id = id[:i] } + log.Print(id, ": Error 404") + resp.WriteHeader(404) } func main() {