this repo has no description
1package routes 2 3import ( 4 "fmt" 5 "html/template" 6 "net/http" 7 "path/filepath" 8 9 "github.com/go-chi/chi/v5" 10 "github.com/icyphox/bild/legit/config" 11 "github.com/icyphox/bild/legit/db" 12) 13 14// Checks for gitprotocol-http(5) specific smells; if found, passes 15// the request on to the git http service, else render the web frontend. 16func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) { 17 path := chi.URLParam(r, "*") 18 19 if r.URL.RawQuery == "service=git-receive-pack" { 20 w.WriteHeader(http.StatusBadRequest) 21 w.Write([]byte("no pushing allowed!")) 22 return 23 } 24 25 if path == "info/refs" && 26 r.URL.RawQuery == "service=git-upload-pack" && 27 r.Method == "GET" { 28 h.InfoRefs(w, r) 29 } else if path == "git-upload-pack" && r.Method == "POST" { 30 h.UploadPack(w, r) 31 } else if r.Method == "GET" { 32 h.RepoIndex(w, r) 33 } 34} 35 36func Setup(c *config.Config) (http.Handler, error) { 37 r := chi.NewRouter() 38 t := template.Must(template.ParseGlob(filepath.Join(c.Dirs.Templates, "*"))) 39 db, err := db.Setup(c.Server.DBPath) 40 41 if err != nil { 42 return nil, fmt.Errorf("failed to setup db: %w", err) 43 } 44 45 h := Handle{ 46 c: c, 47 t: t, 48 db: db, 49 } 50 51 r.Get("/login", h.Login) 52 r.Get("/static/{file}", h.ServeStatic) 53 54 r.Route("/repo", func(r chi.Router) { 55 r.Get("/new", h.NewRepo) 56 r.Put("/new", h.NewRepo) 57 }) 58 59 r.Route("/settings", func(r chi.Router) { 60 r.Get("/keys", h.Keys) 61 r.Put("/keys", h.Keys) 62 }) 63 64 r.Route("/@{user}", func(r chi.Router) { 65 r.Get("/", h.Index) 66 67 // Repo routes 68 r.Route("/{name}", func(r chi.Router) { 69 r.Get("/", h.Multiplex) 70 r.Post("/", h.Multiplex) 71 72 r.Route("/tree/{ref}", func(r chi.Router) { 73 r.Get("/*", h.RepoTree) 74 }) 75 76 r.Route("/blob/{ref}", func(r chi.Router) { 77 r.Get("/*", h.FileContent) 78 }) 79 80 r.Get("/log/{ref}", h.Log) 81 r.Get("/archive/{file}", h.Archive) 82 r.Get("/commit/{ref}", h.Diff) 83 r.Get("/refs/", h.Refs) 84 85 // Catch-all routes 86 r.Get("/*", h.Multiplex) 87 r.Post("/*", h.Multiplex) 88 }) 89 }) 90 91 return r, nil 92}