forked from tangled.org/core
this repo has no description
at fix-tw-dark 3.6 kB view raw
1package knotserver 2 3import ( 4 "context" 5 "fmt" 6 "log/slog" 7 "net/http" 8 "runtime/debug" 9 10 "github.com/go-chi/chi/v5" 11 "tangled.sh/tangled.sh/core/jetstream" 12 "tangled.sh/tangled.sh/core/knotserver/config" 13 "tangled.sh/tangled.sh/core/knotserver/db" 14 "tangled.sh/tangled.sh/core/rbac" 15) 16 17const ( 18 ThisServer = "thisserver" // resource identifier for rbac enforcement 19) 20 21type Handle struct { 22 c *config.Config 23 db *db.DB 24 jc *jetstream.JetstreamClient 25 e *rbac.Enforcer 26 l *slog.Logger 27 28 // init is a channel that is closed when the knot has been initailized 29 // i.e. when the first user (knot owner) has been added. 30 init chan struct{} 31 knotInitialized bool 32} 33 34func Setup(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, jc *jetstream.JetstreamClient, l *slog.Logger) (http.Handler, error) { 35 r := chi.NewRouter() 36 37 h := Handle{ 38 c: c, 39 db: db, 40 e: e, 41 l: l, 42 jc: jc, 43 init: make(chan struct{}), 44 } 45 46 err := e.AddDomain(ThisServer) 47 if err != nil { 48 return nil, fmt.Errorf("failed to setup enforcer: %w", err) 49 } 50 51 err = h.jc.StartJetstream(ctx, h.processMessages) 52 if err != nil { 53 return nil, fmt.Errorf("failed to start jetstream: %w", err) 54 } 55 56 // Check if the knot knows about any Dids; 57 // if it does, it is already initialized and we can repopulate the 58 // Jetstream subscriptions. 59 dids, err := db.GetAllDids() 60 if err != nil { 61 return nil, fmt.Errorf("failed to get all Dids: %w", err) 62 } 63 if len(dids) > 0 { 64 h.knotInitialized = true 65 close(h.init) 66 // h.jc.UpdateDids(dids) 67 } 68 69 r.Get("/", h.Index) 70 r.Get("/version", h.Version) 71 r.Route("/{did}", func(r chi.Router) { 72 // Repo routes 73 r.Route("/{name}", func(r chi.Router) { 74 r.Route("/collaborator", func(r chi.Router) { 75 r.Use(h.VerifySignature) 76 r.Post("/add", h.AddRepoCollaborator) 77 }) 78 79 r.Get("/", h.RepoIndex) 80 r.Get("/info/refs", h.InfoRefs) 81 r.Post("/git-upload-pack", h.UploadPack) 82 83 r.Route("/merge", func(r chi.Router) { 84 r.With(h.VerifySignature) 85 r.Post("/", h.Merge) 86 r.Post("/check", h.MergeCheck) 87 }) 88 89 r.Route("/tree/{ref}", func(r chi.Router) { 90 r.Get("/", h.RepoIndex) 91 r.Get("/*", h.RepoTree) 92 }) 93 94 r.Route("/blob/{ref}", func(r chi.Router) { 95 r.Get("/*", h.Blob) 96 }) 97 98 r.Get("/log/{ref}", h.Log) 99 r.Get("/archive/{file}", h.Archive) 100 r.Get("/commit/{ref}", h.Diff) 101 r.Get("/tags", h.Tags) 102 r.Get("/branches", h.Branches) 103 }) 104 }) 105 106 // Create a new repository. 107 r.Route("/repo", func(r chi.Router) { 108 r.Use(h.VerifySignature) 109 r.Put("/new", h.NewRepo) 110 r.Delete("/", h.RemoveRepo) 111 }) 112 113 r.Route("/member", func(r chi.Router) { 114 r.Use(h.VerifySignature) 115 r.Put("/add", h.AddMember) 116 }) 117 118 // Initialize the knot with an owner and public key. 119 r.With(h.VerifySignature).Post("/init", h.Init) 120 121 // Health check. Used for two-way verification with appview. 122 r.With(h.VerifySignature).Get("/health", h.Health) 123 124 // All public keys on the knot. 125 r.Get("/keys", h.Keys) 126 127 return r, nil 128} 129 130// version is set during build time. 131var version string 132 133func (h *Handle) Version(w http.ResponseWriter, r *http.Request) { 134 if version == "" { 135 info, ok := debug.ReadBuildInfo() 136 if !ok { 137 http.Error(w, "failed to read build info", http.StatusInternalServerError) 138 return 139 } 140 141 var modVer string 142 for _, mod := range info.Deps { 143 if mod.Path == "tangled.sh/tangled.sh/knotserver" { 144 version = mod.Version 145 break 146 } 147 } 148 149 if modVer == "" { 150 version = "unknown" 151 } 152 } 153 154 w.Header().Set("Content-Type", "text/plain") 155 fmt.Fprintf(w, "knotserver/%s", version) 156}