this repo has no description
1package knotserver
2
3import (
4 "fmt"
5 "net/http"
6
7 "github.com/go-chi/chi/v5"
8 "github.com/icyphox/bild/knotserver/config"
9 "github.com/icyphox/bild/knotserver/db"
10)
11
12func Setup(c *config.Config, db *db.DB) (http.Handler, error) {
13 r := chi.NewRouter()
14
15 h := Handle{
16 c: c,
17 db: db,
18 }
19
20 r.Get("/", h.Index)
21 r.Route("/{did}", func(r chi.Router) {
22 // Repo routes
23 r.Route("/{name}", func(r chi.Router) {
24 r.Get("/", h.RepoIndex)
25 r.Get("/info/refs", h.InfoRefs)
26 r.Post("/git-upload-pack", h.UploadPack)
27
28 r.Route("/tree/{ref}", func(r chi.Router) {
29 r.Get("/*", h.RepoTree)
30 })
31
32 r.Route("/blob/{ref}", func(r chi.Router) {
33 r.Get("/*", h.FileContent)
34 })
35
36 r.Get("/log/{ref}", h.Log)
37 r.Get("/archive/{file}", h.Archive)
38 r.Get("/commit/{ref}", h.Diff)
39 r.Get("/refs/", h.Refs)
40 })
41 })
42
43 r.Route("/repo", func(r chi.Router) {
44 r.Put("/new", h.NewRepo)
45 })
46
47 r.With(h.VerifySignature).Get("/health", h.Health)
48
49 r.Group(func(r chi.Router) {
50 r.Use(h.VerifySignature)
51 r.Get("/keys", h.Keys)
52 r.Put("/keys", h.Keys)
53 })
54
55 return r, nil
56}
57
58type Handle struct {
59 c *config.Config
60 db *db.DB
61}
62
63func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) {
64 path := chi.URLParam(r, "*")
65
66 if r.URL.RawQuery == "service=git-receive-pack" {
67 w.WriteHeader(http.StatusBadRequest)
68 w.Write([]byte("no pushing allowed!"))
69 return
70 }
71
72 fmt.Println(r.URL.RawQuery)
73 fmt.Println(r.Method)
74
75 if path == "info/refs" &&
76 r.URL.RawQuery == "service=git-upload-pack" &&
77 r.Method == "GET" {
78 h.InfoRefs(w, r)
79 } else if path == "git-upload-pack" && r.Method == "POST" {
80 h.UploadPack(w, r)
81 } else if r.Method == "GET" {
82 h.RepoIndex(w, r)
83 }
84}