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/auth" 13 "github.com/icyphox/bild/config" 14 "github.com/icyphox/bild/db" 15 "github.com/icyphox/bild/routes/middleware" 16 "github.com/icyphox/bild/routes/tmpl" 17) 18 19// Checks for gitprotocol-http(5) specific smells; if found, passes 20// the request on to the git http service, else render the web frontend. 21func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) { 22 path := chi.URLParam(r, "*") 23 24 if r.URL.RawQuery == "service=git-receive-pack" { 25 w.WriteHeader(http.StatusBadRequest) 26 w.Write([]byte("no pushing allowed!")) 27 return 28 } 29 30 if path == "info/refs" && 31 r.URL.RawQuery == "service=git-upload-pack" && 32 r.Method == "GET" { 33 h.InfoRefs(w, r) 34 } else if path == "git-upload-pack" && r.Method == "POST" { 35 h.UploadPack(w, r) 36 } else if r.Method == "GET" { 37 h.RepoIndex(w, r) 38 } 39} 40 41func Setup(c *config.Config, db *db.DB) (http.Handler, error) { 42 r := chi.NewRouter() 43 s := sessions.NewCookieStore([]byte("TODO_CHANGE_ME")) 44 t, err := tmpl.Load(c.Dirs.Templates) 45 if err != nil { 46 return nil, fmt.Errorf("failed to load templates: %w", err) 47 } 48 49 auth := auth.NewAuth(s) 50 51 h := Handle{ 52 c: c, 53 t: t, 54 s: s, 55 db: db, 56 auth: auth, 57 } 58 59 r.Group(func(r chi.Router) { 60 r.Use(h.AuthMiddleware) 61 r.Get("/login", h.Login) 62 r.Post("/login", h.Login) 63 }) 64 r.Get("/static/{file}", h.ServeStatic) 65 66 r.Route("/repo", func(r chi.Router) { 67 r.Use(h.AuthMiddleware) 68 r.Get("/new", h.NewRepo) 69 r.Put("/new", h.NewRepo) 70 }) 71 72 r.Group(func(r chi.Router) { 73 r.Use(h.AuthMiddleware) 74 r.Route("/settings", func(r chi.Router) { 75 r.Get("/keys", h.Keys) 76 r.Put("/keys", h.Keys) 77 }) 78 }) 79 80 r.Route("/@{user}", func(r chi.Router) { 81 r.Use(middleware.AddDID) 82 r.Get("/", h.Index) 83 84 // Repo routes 85 r.Route("/{name}", func(r chi.Router) { 86 r.Get("/", h.Multiplex) 87 r.Post("/", h.Multiplex) 88 89 r.Route("/tree/{ref}", func(r chi.Router) { 90 r.Get("/*", h.RepoTree) 91 }) 92 93 r.Route("/blob/{ref}", func(r chi.Router) { 94 r.Get("/*", h.FileContent) 95 }) 96 97 r.Get("/log/{ref}", h.Log) 98 r.Get("/archive/{file}", h.Archive) 99 r.Get("/commit/{ref}", h.Diff) 100 r.Get("/refs/", h.Refs) 101 102 // Catch-all routes 103 r.Get("/*", h.Multiplex) 104 r.Post("/*", h.Multiplex) 105 }) 106 }) 107 108 return r, nil 109}