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
64 if len(dids) > 0 {
65 h.knotInitialized = true
66 close(h.init)
67 for _, d := range dids {
68 h.jc.AddDid(d)
69 }
70 }
71
72 r.Get("/", h.Index)
73 r.Get("/capabilities", h.Capabilities)
74 r.Get("/version", h.Version)
75 r.Route("/{did}", func(r chi.Router) {
76 // Repo routes
77 r.Route("/{name}", func(r chi.Router) {
78 r.Route("/collaborator", func(r chi.Router) {
79 r.Use(h.VerifySignature)
80 r.Post("/add", h.AddRepoCollaborator)
81 })
82
83 r.Get("/", h.RepoIndex)
84 r.Get("/info/refs", h.InfoRefs)
85 r.Post("/git-upload-pack", h.UploadPack)
86 r.Post("/git-receive-pack", h.ReceivePack)
87 r.Get("/compare/{rev1}/{rev2}", h.Compare) // git diff-tree compare of two objects
88
89 r.With(h.VerifySignature).Post("/hidden-ref/{forkRef}/{remoteRef}", h.NewHiddenRef)
90
91 r.Route("/merge", func(r chi.Router) {
92 r.With(h.VerifySignature)
93 r.Post("/", h.Merge)
94 r.Post("/check", h.MergeCheck)
95 })
96
97 r.Route("/tree/{ref}", func(r chi.Router) {
98 r.Get("/", h.RepoIndex)
99 r.Get("/*", h.RepoTree)
100 })
101
102 r.Route("/blob/{ref}", func(r chi.Router) {
103 r.Get("/*", h.Blob)
104 })
105
106 r.Route("/raw/{ref}", func(r chi.Router) {
107 r.Get("/*", h.BlobRaw)
108 })
109
110 r.Get("/log/{ref}", h.Log)
111 r.Get("/archive/{file}", h.Archive)
112 r.Get("/commit/{ref}", h.Diff)
113 r.Get("/tags", h.Tags)
114 r.Route("/branches", func(r chi.Router) {
115 r.Get("/", h.Branches)
116 r.Get("/{branch}", h.Branch)
117 r.Route("/default", func(r chi.Router) {
118 r.Get("/", h.DefaultBranch)
119 r.With(h.VerifySignature).Put("/", h.SetDefaultBranch)
120 })
121 })
122 })
123 })
124
125 // Create a new repository.
126 r.Route("/repo", func(r chi.Router) {
127 r.Use(h.VerifySignature)
128 r.Put("/new", h.NewRepo)
129 r.Delete("/", h.RemoveRepo)
130 r.Route("/fork", func(r chi.Router) {
131 r.Post("/", h.RepoFork)
132 r.Post("/sync/{branch}", h.RepoForkSync)
133 r.Get("/sync/{branch}", h.RepoForkAheadBehind)
134 })
135 })
136
137 r.Route("/member", func(r chi.Router) {
138 r.Use(h.VerifySignature)
139 r.Put("/add", h.AddMember)
140 })
141
142 // Initialize the knot with an owner and public key.
143 r.With(h.VerifySignature).Post("/init", h.Init)
144
145 // Health check. Used for two-way verification with appview.
146 r.With(h.VerifySignature).Get("/health", h.Health)
147
148 // All public keys on the knot.
149 r.Get("/keys", h.Keys)
150
151 return r, nil
152}
153
154// version is set during build time.
155var version string
156
157func (h *Handle) Version(w http.ResponseWriter, r *http.Request) {
158 if version == "" {
159 info, ok := debug.ReadBuildInfo()
160 if !ok {
161 http.Error(w, "failed to read build info", http.StatusInternalServerError)
162 return
163 }
164
165 var modVer string
166 for _, mod := range info.Deps {
167 if mod.Path == "tangled.sh/tangled.sh/knotserver" {
168 version = mod.Version
169 break
170 }
171 }
172
173 if modVer == "" {
174 version = "unknown"
175 }
176 }
177
178 w.Header().Set("Content-Type", "text/plain")
179 fmt.Fprintf(w, "knotserver/%s", version)
180}