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) (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 db, err := db.Setup(c.Server.DBPath) 51 if err != nil { 52 return nil, fmt.Errorf("failed to setup db: %w", err) 53 } 54 55 h := Handle{ 56 c: c, 57 t: t, 58 s: s, 59 db: db, 60 auth: auth, 61 } 62 63 r.Group(func(r chi.Router) { 64 r.Use(h.AuthMiddleware) 65 r.Get("/login", h.Login) 66 r.Post("/login", h.Login) 67 }) 68 r.Get("/static/{file}", h.ServeStatic) 69 70 r.Route("/repo", func(r chi.Router) { 71 r.Use(h.AuthMiddleware) 72 r.Get("/new", h.NewRepo) 73 r.Put("/new", h.NewRepo) 74 }) 75 76 r.Group(func(r chi.Router) { 77 r.Use(h.AuthMiddleware) 78 r.Route("/settings", func(r chi.Router) { 79 r.Get("/keys", h.Keys) 80 r.Put("/keys", h.Keys) 81 }) 82 }) 83 84 r.Route("/@{user}", func(r chi.Router) { 85 r.Get("/", h.Index) 86 87 // Repo routes 88 r.Route("/{name}", func(r chi.Router) { 89 r.Get("/", h.Multiplex) 90 r.Post("/", h.Multiplex) 91 92 r.Route("/tree/{ref}", func(r chi.Router) { 93 r.Get("/*", h.RepoTree) 94 }) 95 96 r.Route("/blob/{ref}", func(r chi.Router) { 97 r.Get("/*", h.FileContent) 98 }) 99 100 r.Get("/log/{ref}", h.Log) 101 r.Get("/archive/{file}", h.Archive) 102 r.Get("/commit/{ref}", h.Diff) 103 r.Get("/refs/", h.Refs) 104 105 // Catch-all routes 106 r.Get("/*", h.Multiplex) 107 r.Post("/*", h.Multiplex) 108 }) 109 }) 110 111 return r, nil 112}