this repo has no description
1package routes 2 3import ( 4 "fmt" 5 "net/http" 6 7 _ "github.com/bluesky-social/indigo/atproto/identity" 8 _ "github.com/bluesky-social/indigo/atproto/syntax" 9 _ "github.com/bluesky-social/indigo/xrpc" 10 "github.com/go-chi/chi/v5" 11 "github.com/gorilla/sessions" 12 "github.com/icyphox/bild/config" 13 "github.com/icyphox/bild/db" 14 "github.com/icyphox/bild/routes/auth" 15 "github.com/icyphox/bild/routes/tmpl" 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, db *db.DB) (http.Handler, error) { 41 r := chi.NewRouter() 42 s := sessions.NewCookieStore([]byte("TODO_CHANGE_ME")) 43 t, err := tmpl.Load(c.Dirs.Templates) 44 if err != nil { 45 return nil, fmt.Errorf("failed to load templates: %w", err) 46 } 47 48 auth := auth.NewAuth(s) 49 50 h := Handle{ 51 c: c, 52 t: t, 53 s: s, 54 db: db, 55 auth: auth, 56 } 57 58 r.Group(func(r chi.Router) { 59 r.Use(h.AuthMiddleware) 60 r.Get("/login", h.Login) 61 r.Post("/login", h.Login) 62 }) 63 r.Get("/static/{file}", h.ServeStatic) 64 65 r.Route("/repo", func(r chi.Router) { 66 r.Use(h.AuthMiddleware) 67 r.Get("/new", h.NewRepo) 68 r.Put("/new", h.NewRepo) 69 }) 70 71 r.Group(func(r chi.Router) { 72 r.Use(h.AuthMiddleware) 73 r.Route("/settings", func(r chi.Router) { 74 r.Get("/keys", h.Keys) 75 r.Put("/keys", h.Keys) 76 }) 77 }) 78 79 r.Route("/@{user}", func(r chi.Router) { 80 r.Get("/", h.Index) 81 82 // Repo routes 83 r.Route("/{name}", func(r chi.Router) { 84 r.Get("/", h.Multiplex) 85 r.Post("/", h.Multiplex) 86 87 r.Route("/tree/{ref}", func(r chi.Router) { 88 r.Get("/*", h.RepoTree) 89 }) 90 91 r.Route("/blob/{ref}", func(r chi.Router) { 92 r.Get("/*", h.FileContent) 93 }) 94 95 r.Get("/log/{ref}", h.Log) 96 r.Get("/archive/{file}", h.Archive) 97 r.Get("/commit/{ref}", h.Diff) 98 r.Get("/refs/", h.Refs) 99 100 // Catch-all routes 101 r.Get("/*", h.Multiplex) 102 r.Post("/*", h.Multiplex) 103 }) 104 }) 105 106 return r, nil 107}