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