Monorepo for Tangled tangled.org

knotserver: fix a bunch of things

anirudh.fi f4b8dc96 27703946

verified
Changed files
+27 -26
cmd
knotserver
knotserver
+3 -3
cmd/knotserver/main.go
··· 10 10 11 11 "github.com/icyphox/bild/config" 12 12 "github.com/icyphox/bild/db" 13 - "github.com/icyphox/bild/routes" 13 + "github.com/icyphox/bild/knotserver" 14 14 ) 15 15 16 16 func main() { ··· 29 29 log.Fatalf("failed to setup db: %s", err) 30 30 } 31 31 32 - mux, err := routes.Setup(c, db) 32 + mux, err := knotserver.Setup(c, db) 33 33 if err != nil { 34 34 log.Fatal(err) 35 35 } ··· 37 37 addr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) 38 38 39 39 log.Println("starting main server on", addr) 40 - go http.ListenAndServe(addr, mux) 40 + log.Fatal(http.ListenAndServe(addr, mux)) 41 41 }
-1
go.mod
··· 9 9 github.com/bluekeyes/go-gitdiff v0.8.0 10 10 github.com/bluesky-social/indigo v0.0.0-20250123072624-9e3b84fdbb20 11 11 github.com/dustin/go-humanize v1.0.1 12 - github.com/go-chi/chi v1.5.5 13 12 github.com/go-chi/chi/v5 v5.2.0 14 13 github.com/go-git/go-git/v5 v5.12.0 15 14 github.com/google/uuid v1.6.0
-2
go.sum
··· 53 53 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= 54 54 github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= 55 55 github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= 56 - github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= 57 - github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= 58 56 github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0= 59 57 github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 60 58 github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
+8 -8
knotserver/git.go
··· 7 7 "net/http" 8 8 "path/filepath" 9 9 10 + "github.com/go-chi/chi/v5" 10 11 "github.com/icyphox/bild/knotserver/git/service" 11 12 ) 13 + 12 14 func (d *Handle) InfoRefs(w http.ResponseWriter, r *http.Request) { 13 - name := displayRepoName(r) 14 - name = filepath.Clean(name) 15 - 16 - repo := filepath.Join(d.c.Repo.ScanPath, name) 15 + did := chi.URLParam(r, "did") 16 + name := chi.URLParam(r, "name") 17 + repo := filepath.Join(d.c.Repo.ScanPath, did, name) 17 18 18 19 w.Header().Set("content-type", "application/x-git-upload-pack-advertisement") 19 20 w.WriteHeader(http.StatusOK) ··· 31 32 } 32 33 33 34 func (d *Handle) UploadPack(w http.ResponseWriter, r *http.Request) { 34 - name := displayRepoName(r) 35 - name = filepath.Clean(name) 36 - 37 - repo := filepath.Join(d.c.Repo.ScanPath, name) 35 + did := chi.URLParam(r, "did") 36 + name := chi.URLParam(r, "name") 37 + repo := filepath.Join(d.c.Repo.ScanPath, did, name) 38 38 39 39 w.Header().Set("content-type", "application/x-git-upload-pack-result") 40 40 w.Header().Set("Connection", "Keep-Alive")
+9 -9
knotserver/handler.go
··· 1 1 package knotserver 2 2 3 3 import ( 4 + "fmt" 4 5 "net/http" 5 6 6 - "github.com/go-chi/chi" 7 + "github.com/go-chi/chi/v5" 7 8 "github.com/icyphox/bild/config" 8 9 "github.com/icyphox/bild/db" 9 10 ) ··· 22 23 // r.Put("/new", h.NewRepo) 23 24 // }) 24 25 26 + r.Get("/", h.Index) 25 27 r.Route("/{did}", func(r chi.Router) { 26 - r.Get("/", h.Index) 27 - 28 28 // Repo routes 29 29 r.Route("/{name}", func(r chi.Router) { 30 - r.Get("/", h.Multiplex) 31 - r.Post("/", h.Multiplex) 30 + r.Get("/", h.RepoIndex) 31 + r.Get("/info/refs", h.InfoRefs) 32 + r.Post("/git-upload-pack", h.UploadPack) 32 33 33 34 r.Route("/tree/{ref}", func(r chi.Router) { 34 35 r.Get("/*", h.RepoTree) ··· 42 43 r.Get("/archive/{file}", h.Archive) 43 44 r.Get("/commit/{ref}", h.Diff) 44 45 r.Get("/refs/", h.Refs) 45 - 46 - // Catch-all routes 47 - r.Get("/*", h.Multiplex) 48 - r.Post("/*", h.Multiplex) 49 46 }) 50 47 }) 51 48 ··· 65 62 w.Write([]byte("no pushing allowed!")) 66 63 return 67 64 } 65 + 66 + fmt.Println(r.URL.RawQuery) 67 + fmt.Println(r.Method) 68 68 69 69 if path == "info/refs" && 70 70 r.URL.RawQuery == "service=git-upload-pack" &&
+1 -1
knotserver/http_util.go
··· 22 22 } 23 23 24 24 func writeMsg(w http.ResponseWriter, msg string) { 25 - writeJson(w, map[string]string{"msg": msg}) 25 + writeJSON(w, map[string]string{"msg": msg}) 26 26 }
+2
knotserver/routes.go
··· 30 30 writeMsg(w, "repo empty") 31 31 return 32 32 } else { 33 + log.Println(err) 33 34 notFound(w) 34 35 return 35 36 } ··· 201 202 } 202 203 203 204 func (h *Handle) Log(w http.ResponseWriter, r *http.Request) { 205 + fmt.Println(r.URL.Path) 204 206 ref := chi.URLParam(r, "ref") 205 207 206 208 path := filepath.Join(h.c.Repo.ScanPath, didPath(r))
+4 -2
knotserver/util.go
··· 32 32 return fmt.Sprintf("@%s/%s", handle.Handle.String(), name) 33 33 } 34 34 35 - func didPath(r *http.Request, did string) string { 36 - path := filepath.Join(did, chi.URLParam(r, "name")) 35 + func didPath(r *http.Request) string { 36 + did := chi.URLParam(r, "did") 37 + name := chi.URLParam(r, "name") 38 + path := filepath.Join(did, name) 37 39 filepath.Clean(path) 38 40 return path 39 41 }