BlueGit

timfa bluegit

Files

bluegit / internal / server / server.go

server.go

feat: pipelines-tab en jobconsole
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 5d0db17c History
server.go 16.9 KiB Download Raw
1// Package server is de webinterface, de git-HTTP-server en de API.
2package server
3
4import (
5 "context"
6 "crypto/rand"
7 "embed"
8 "encoding/hex"
9 "html/template"
10 "io/fs"
11 "log/slog"
12 "net/http"
13 "net/url"
14 "strings"
15 "sync"
16 "time"
17 "unicode/utf8"
18
19 "gitlab.com/Timfa/bluegit/internal/access"
20 "gitlab.com/Timfa/bluegit/internal/activity"
21 "gitlab.com/Timfa/bluegit/internal/auth"
22 "gitlab.com/Timfa/bluegit/internal/config"
23 "gitlab.com/Timfa/bluegit/internal/i18n"
24 "gitlab.com/Timfa/bluegit/internal/realip"
25 "gitlab.com/Timfa/bluegit/internal/runner"
26 "gitlab.com/Timfa/bluegit/internal/store"
27)
28
29//go:embed templates/*.html static/*
30var webFS embed.FS
31
32// Server bedient BlueGit.
33type Server struct {
34 Cfg config.Config
35 Store *store.Store
36 Auth auth.Provider
37 Dev bool
38 Git string
39 HookBin string
40 Runner *runner.Runner
41 Log *slog.Logger
42 // StateDir is de map met database en het bestand storage_path.
43 StateDir string
44 // AfterStorage krijgt het nieuwe datadisk-pad, voor runner en SSH.
45 AfterStorage func(path string)
46 tmpl *template.Template
47 byLang map[string]*template.Template
48
49 storageMu sync.RWMutex
50 storage string
51 storageSet bool
52}
53
54type principal struct {
55 User store.User
56 Session store.Session
57 OK bool
58}
59
60type ctxKey struct{}
61
62// Handler is de HTTP-handler met sessies en koppen.
63func (s *Server) Handler() http.Handler {
64 s.mustTemplates()
65 mux := http.NewServeMux()
66 mux.HandleFunc("GET /assets/{file}", s.asset)
67 mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
68 w.WriteHeader(http.StatusOK)
69 _, _ = w.Write([]byte("ok\n"))
70 })
71 mux.HandleFunc("GET /login", s.login)
72 mux.HandleFunc("GET /auth/callback", s.callback)
73 mux.HandleFunc("POST /logout", s.logout)
74 mux.HandleFunc("GET /{$}", s.home)
75 mux.HandleFunc("GET /new", s.newPage)
76 mux.HandleFunc("GET /new/slug", s.slugPreview)
77 mux.HandleFunc("POST /new/repo", s.createRepo)
78 mux.HandleFunc("POST /new/group", s.createGroup)
79 mux.HandleFunc("GET /setup", s.setupPage)
80 mux.HandleFunc("POST /setup", s.setupSave)
81 mux.HandleFunc("GET /settings", s.account)
82 mux.HandleFunc("POST /settings/language", s.saveLanguage)
83 mux.HandleFunc("POST /settings/tokens", s.createToken)
84 mux.HandleFunc("POST /settings/tokens/delete", s.deleteToken)
85 mux.HandleFunc("POST /settings/ssh", s.addSSH)
86 mux.HandleFunc("POST /settings/ssh/delete", s.deleteSSH)
87 mux.HandleFunc("GET /settings/export", s.export)
88 mux.HandleFunc("GET /users/{username}", s.profile)
89 mux.HandleFunc("GET /groups/{path}", s.groupPage)
90 mux.HandleFunc("GET /groups/{path}/logo", s.groupLogo)
91 mux.HandleFunc("POST /groups/{path}/settings", s.saveGroup)
92 mux.HandleFunc("POST /groups/{path}/logo", s.uploadGroupLogo)
93 mux.HandleFunc("POST /groups/{path}/members", s.groupMember)
94 mux.HandleFunc("POST /groups/{path}/members/remove", s.groupMemberRemove)
95 mux.HandleFunc("POST /groups/{path}/secrets", s.groupSecret)
96 mux.HandleFunc("POST /groups/{path}/secrets/delete", s.groupSecretDelete)
97 mux.HandleFunc("POST /groups/{path}/webhooks", s.groupWebhook)
98 mux.HandleFunc("POST /groups/{path}/webhooks/delete", s.groupWebhookDelete)
99
100 mux.HandleFunc("GET /{owner}/{repo}/info/refs", s.gitHTTP)
101 mux.HandleFunc("POST /{owner}/{repo}/git-upload-pack", s.gitHTTP)
102 mux.HandleFunc("POST /{owner}/{repo}/git-receive-pack", s.gitHTTP)
103 mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/releases", s.createRelease)
104
105 mux.HandleFunc("GET /{owner}/{repo}", s.repoHome)
106 mux.HandleFunc("GET /{owner}/{repo}/-/tree", s.treeRoot)
107 mux.HandleFunc("GET /{owner}/{repo}/-/tree/{path...}", s.tree)
108 mux.HandleFunc("GET /{owner}/{repo}/-/blob/{path...}", s.blob)
109 mux.HandleFunc("GET /{owner}/{repo}/-/commits", s.commits)
110 mux.HandleFunc("GET /{owner}/{repo}/-/tags", s.tags)
111 mux.HandleFunc("GET /{owner}/{repo}/-/commit/{sha}", s.commit)
112 mux.HandleFunc("GET /{owner}/{repo}/-/branches", s.branches)
113 mux.HandleFunc("GET /{owner}/{repo}/-/merge-requests", s.mergeRequests)
114 mux.HandleFunc("POST /{owner}/{repo}/-/merge-requests", s.createMR)
115 mux.HandleFunc("GET /{owner}/{repo}/-/merge-requests/{number}", s.showMR)
116 mux.HandleFunc("POST /{owner}/{repo}/-/merge-requests/{number}/merge", s.mergeMR)
117 mux.HandleFunc("POST /{owner}/{repo}/-/merge-requests/{number}/close", s.closeMR)
118 mux.HandleFunc("POST /{owner}/{repo}/-/merge-requests/{number}/notes", s.createNote)
119 mux.HandleFunc("POST /{owner}/{repo}/-/merge-requests/{number}/suggestions", s.applySuggestions)
120 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines", s.pipelines)
121 mux.HandleFunc("POST /{owner}/{repo}/-/pipelines", s.runPipeline)
122 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}", s.pipeline)
123 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}", s.showJob)
124 mux.HandleFunc("GET /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/log", s.jobLog)
125 mux.HandleFunc("POST /{owner}/{repo}/-/pipelines/{id}/jobs/{job}/play", s.playJob)
126 mux.HandleFunc("GET /{owner}/{repo}/-/releases", s.releases)
127 mux.HandleFunc("GET /{owner}/{repo}/-/releases/{tag}/assets/{name}", s.releaseAsset)
128 mux.HandleFunc("GET /{owner}/{repo}/-/logo", s.repoLogo)
129 mux.HandleFunc("POST /{owner}/{repo}/-/logo", s.uploadRepoLogo)
130 mux.HandleFunc("POST /{owner}/{repo}/-/star", s.toggleStar)
131 mux.HandleFunc("GET /{owner}/{repo}/-/settings", s.settings)
132 mux.HandleFunc("POST /{owner}/{repo}/-/settings", s.updateRepo)
133 mux.HandleFunc("POST /{owner}/{repo}/-/members", s.repoMember)
134 mux.HandleFunc("POST /{owner}/{repo}/-/members/remove", s.repoMemberRemove)
135 mux.HandleFunc("POST /{owner}/{repo}/-/protected", s.addProtected)
136 mux.HandleFunc("POST /{owner}/{repo}/-/protected/remove", s.removeProtected)
137 mux.HandleFunc("POST /{owner}/{repo}/-/secrets", s.repoSecret)
138 mux.HandleFunc("POST /{owner}/{repo}/-/secrets/delete", s.repoSecretDelete)
139 mux.HandleFunc("POST /{owner}/{repo}/-/webhooks", s.repoWebhook)
140 mux.HandleFunc("POST /{owner}/{repo}/-/webhooks/delete", s.repoWebhookDelete)
141 mux.HandleFunc("POST /{owner}/{repo}/-/delete", s.deleteRepo)
142
143 return s.realIP(s.withSession(s.secure(s.gate(mux))))
144}
145
146func (s *Server) realIP(next http.Handler) http.Handler {
147 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
148 realip.Apply(r, s.Cfg.ProxySet())
149 next.ServeHTTP(w, r)
150 })
151}
152
153func (s *Server) mustTemplates() {
154 if s.byLang != nil {
155 return
156 }
157 s.byLang = map[string]*template.Template{}
158 for _, lang := range i18n.All() {
159 l := lang
160 s.byLang[string(l)] = template.Must(template.New("web").Funcs(template.FuncMap{
161 "short": func(v string) string {
162 if len(v) > 10 {
163 return v[:10]
164 }
165 return v
166 },
167 "join": strings.Join,
168 "initial": func(s string) string {
169 s = strings.TrimSpace(s)
170 if s == "" {
171 return "?"
172 }
173 r, _ := utf8.DecodeRuneInString(s)
174 return strings.ToUpper(string(r))
175 },
176 "splitPath": func(p string) []string {
177 if p == "" {
178 return nil
179 }
180 return strings.Split(p, "/")
181 },
182 "iconOf": func(name string) string { return fileIcon(name, "blob") },
183 "t": func(key string, args ...any) string { return i18n.T(l, key, args...) },
184 "tn": func(n int, one, many string) string {
185 key := many
186 if n == 1 {
187 key = one
188 }
189 return i18n.T(l, key, n)
190 },
191 "roleName": func(r access.Role) string { return i18n.T(l, "role."+string(r)) },
192 "visName": func(v string) string { return i18n.T(l, "vis."+v) },
193 "presetName": func(id string) string {
194 label := i18n.T(l, "set.preset."+id)
195 if label == "set.preset."+id {
196 return id
197 }
198 return label
199 },
200 }).ParseFS(webFS, "templates/*.html"))
201 }
202 s.tmpl = s.byLang[string(i18n.EN)]
203}
204
205func (s *Server) asset(w http.ResponseWriter, r *http.Request) {
206 name := r.PathValue("file")
207 if name == "" || strings.Contains(name, "/") || strings.Contains(name, "..") {
208 http.NotFound(w, r)
209 return
210 }
211 sub, err := fs.Sub(webFS, "static")
212 if err != nil {
213 http.Error(w, "assets", http.StatusInternalServerError)
214 return
215 }
216 http.ServeFileFS(w, r, sub, name)
217}
218
219func (s *Server) secure(next http.Handler) http.Handler {
220 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
221 w.Header().Set("X-Content-Type-Options", "nosniff")
222 w.Header().Set("X-Frame-Options", "DENY")
223 w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
224 w.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self' https://auth.timfalken.com data:; style-src 'self'; form-action 'self'; base-uri 'self'; frame-ancestors 'none'")
225 next.ServeHTTP(w, r)
226 })
227}
228
229func (s *Server) withSession(next http.Handler) http.Handler {
230 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
231 p := principal{}
232 if c, err := r.Cookie("bluegit_session"); err == nil && c.Value != "" {
233 sess, err := s.Store.Session(r.Context(), c.Value)
234 if err == nil && sess.ExpiresAt.After(time.Now()) {
235 p.Session = sess
236 p.User = sess.User
237 p.OK = true
238 s.maybeExtend(r.Context(), sess)
239 }
240 }
241 next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxKey{}, p)))
242 })
243}
244
245func (s *Server) maybeExtend(ctx context.Context, sess store.Session) {
246 if time.Until(sess.ExpiresAt) > 6*time.Hour {
247 return
248 }
249 sec, err := s.Auth.Extend(ctx, sess.AccessToken)
250 if err != nil || sec <= 0 {
251 return
252 }
253 _ = s.Store.UpdateSessionExpiry(ctx, sess.ID, time.Now().Add(time.Duration(sec)*time.Second))
254}
255
256func (s *Server) who(r *http.Request) principal {
257 p, _ := r.Context().Value(ctxKey{}).(principal)
258 return p
259}
260
261func (s *Server) csrfOK(w http.ResponseWriter, r *http.Request) bool {
262 if err := r.ParseForm(); err != nil {
263 http.Error(w, s.tr(r, "err.badForm"), http.StatusBadRequest)
264 return false
265 }
266 p := s.who(r)
267 if !p.OK || r.FormValue("csrf") == "" || r.FormValue("csrf") != p.Session.CSRF {
268 http.Error(w, s.tr(r, "err.badCSRF"), http.StatusBadRequest)
269 return false
270 }
271 return true
272}
273
274func (s *Server) loginRequired(w http.ResponseWriter, r *http.Request) (store.User, bool) {
275 p := s.who(r)
276 if !p.OK {
277 http.Redirect(w, r, "/login", http.StatusFound)
278 return store.User{}, false
279 }
280 return p.User, true
281}
282
283func (s *Server) setSessionCookie(w http.ResponseWriter, id string, exp time.Time) {
284 http.SetCookie(w, &http.Cookie{
285 Name: "bluegit_session",
286 Value: id,
287 Path: "/",
288 HttpOnly: true,
289 SameSite: http.SameSiteLaxMode,
290 Secure: strings.HasPrefix(s.Cfg.BaseURL, "https://"),
291 Expires: exp,
292 })
293}
294
295func (s *Server) flash(w http.ResponseWriter, notice, problem string) {
296 secure := strings.HasPrefix(s.Cfg.BaseURL, "https://")
297 if notice != "" {
298 http.SetCookie(w, &http.Cookie{Name: "bluegit_notice", Value: url.QueryEscape(notice), Path: "/", MaxAge: 120, HttpOnly: true, Secure: secure, SameSite: http.SameSiteLaxMode})
299 }
300 if problem != "" {
301 http.SetCookie(w, &http.Cookie{Name: "bluegit_error", Value: url.QueryEscape(problem), Path: "/", MaxAge: 120, HttpOnly: true, Secure: secure, SameSite: http.SameSiteLaxMode})
302 }
303}
304
305func (s *Server) takeFlash(w http.ResponseWriter, r *http.Request) (string, string) {
306 notice, problem := flashVal(r, "bluegit_notice"), flashVal(r, "bluegit_error")
307 if notice != "" {
308 http.SetCookie(w, &http.Cookie{Name: "bluegit_notice", Value: "", Path: "/", MaxAge: -1})
309 }
310 if problem != "" {
311 http.SetCookie(w, &http.Cookie{Name: "bluegit_error", Value: "", Path: "/", MaxAge: -1})
312 }
313 return notice, problem
314}
315
316func cookieVal(r *http.Request, name string) string {
317 c, err := r.Cookie(name)
318 if err != nil {
319 return ""
320 }
321 return c.Value
322}
323
324func flashVal(r *http.Request, name string) string {
325 raw := cookieVal(r, name)
326 if raw == "" {
327 return ""
328 }
329 v, err := url.QueryUnescape(raw)
330 if err != nil {
331 return raw
332 }
333 return v
334}
335
336func (s *Server) redirect(w http.ResponseWriter, r *http.Request, path, notice, problem string) {
337 s.flash(w, notice, problem)
338 http.Redirect(w, r, path, http.StatusSeeOther)
339}
340
341func (s *Server) langOf(r *http.Request) i18n.Lang {
342 if p := s.who(r); p.OK {
343 return i18n.Parse(p.User.Language)
344 }
345 return i18n.EN
346}
347
348func (s *Server) tr(r *http.Request, key string, args ...any) string {
349 return i18n.T(s.langOf(r), key, args...)
350}
351
352func (s *Server) trErr(r *http.Request, err error) string {
353 return i18n.Text(s.langOf(r), err)
354}
355
356func (s *Server) view(w http.ResponseWriter, r *http.Request, title string) View {
357 notice, problem := s.takeFlash(w, r)
358 lang := s.langOf(r)
359 if strings.HasPrefix(title, "title.") {
360 title = i18n.T(lang, title)
361 }
362 v := View{
363 Title: title, BaseURL: s.Cfg.BaseURL, Notice: notice, Err: problem,
364 StorageUnset: s.StoragePath() == "", Lang: string(lang), HTMLLang: htmlLang(lang),
365 Cunei: lang == i18n.Cuneiform,
366 }
367 for _, l := range i18n.All() {
368 v.Langs = append(v.Langs, LangChoice{Code: string(l), Label: l.Name()})
369 }
370 if p := s.who(r); p.OK {
371 u := p.User
372 v.User = &u
373 v.CSRF = p.Session.CSRF
374 }
375 return v
376}
377
378func htmlLang(l i18n.Lang) string {
379 switch l {
380 case i18n.NL:
381 return "nl"
382 case i18n.DE:
383 return "de"
384 case i18n.FR:
385 return "fr"
386 default:
387 return "en"
388 }
389}
390
391func (s *Server) render(w http.ResponseWriter, name string, v View) {
392 w.Header().Set("Content-Type", "text/html; charset=utf-8")
393 tmpl := s.byLang[v.Lang]
394 if tmpl == nil {
395 tmpl = s.tmpl
396 }
397 if err := tmpl.ExecuteTemplate(w, name, v); err != nil && s.Log != nil {
398 s.Log.Error("sjabloon", "name", name, "err", err)
399 }
400}
401
402func randomState() (string, error) {
403 b := make([]byte, 32)
404 if _, err := rand.Read(b); err != nil {
405 return "", err
406 }
407 return hex.EncodeToString(b), nil
408}
409
410// View is de gegevensset voor elk scherm.
411type View struct {
412 Title string
413 BaseURL string
414 User *store.User
415 CSRF string
416 Notice string
417 Err string
418 Repos []store.Repo
419 Groups []store.Group
420 Repo *store.Repo
421 Role access.Role
422 CanWrite bool
423 CanAdmin bool
424 CanMerge bool
425 CanComment bool
426 CanApply bool
427 Commits []CommitRow
428 Commit *CommitRow
429 Entries []EntryRow
430 TreePath string
431 Blob string
432 BlobPath string
433 Binary bool
434 Branches []BranchRow
435 MRs []store.MergeRequest
436 MR *store.MergeRequest
437 Diff string
438 DiffFiles []DiffFileView
439 Pipes []store.Pipeline
440 PipeRows []PipeRow
441 Pipe *store.Pipeline
442 Jobs []store.Job
443 JobPage *JobPage
444 Releases []store.Release
445 Members []store.Member
446 Secrets []store.SecretMeta
447 Hooks []HookView
448 Patterns []string
449 Tokens []store.TokenInfo
450 Keys []store.SSHKey
451 CloneHTTP string
452 CloneSSH string
453 Group *store.Group
454 Tab string
455 Rev string
456 NewToken string
457 StorageUnset bool
458 StoragePath string
459 StorageFree string
460 StorageTotal string
461 MinFree string
462 Wide bool
463 Page string
464 Lang string
465 HTMLLang string
466 Cunei bool
467 Langs []LangChoice
468 Query string
469 SortDir string
470 FlipDir string
471 Cards []RepoCard
472 Head *CommitRow
473 Files []FileRow
474 Tree []FileRow
475 Readme template.HTML
476 ReadmeName string
477 LicensePath string
478 NCommits int
479 NBranches int
480 NTags int
481 NReleases int
482 Created string
483 Lines []CodeLine
484 BlobSize string
485 FileName string
486 RevProtected bool
487 TagNames []string
488 Heat *activity.Calendar
489 Notes []NoteRow
490 Profile *store.User
491}
492
493// LangChoice is één taal in de instellingen.
494type LangChoice struct {
495 Code, Label string
496}
497
498// RepoCard is een repository in een groepslijst.
499type RepoCard struct {
500 Repo store.Repo
501 Stars int
502 Starred bool
503 When string
504}
505
506// NoteRow is een reactie in de interface.
507type NoteRow struct {
508 ID int64
509 Author, Body, When, Path string
510 Start, End int
511 HasSuggestion bool
512}
513
514// DiffFileView is één bestand in de merge-request-diff.
515type DiffFileView struct {
516 Path string
517 Lines []DiffLineView
518}
519
520// DiffLineView is één diffregel, met de reacties die op het nieuwe regelnummer eindigen.
521type DiffLineView struct {
522 Kind string
523 Text string
524 Old int
525 New int
526 Notes []NoteRow
527}
528
529// FileRow is een rij in de bestandstabel of de boom.
530type FileRow struct {
531 Name, Path, Icon, Subject, When, SHA string
532 Dir bool
533 Depth int
534 Active bool
535}
536
537// CodeLine is één regel broncode.
538type CodeLine struct {
539 N int
540 HTML template.HTML
541}
542
543// CommitRow is een commit in de interface.
544// Author is de gekoppelde gebruiker, of anders de ruwe git-naam.
545// RawName en PushedBy zijn gezet als de pusher de fallback is en de git-naam afwijkt.
546type CommitRow struct {
547 SHA, Short, Author, Date, Subject, Body, When string
548 UserURL, Avatar, Initial, RawName, PushedBy string
549}
550
551// EntryRow is een bestand of map.
552type EntryRow struct {
553 Name, Type, Path string
554}
555
556// BranchRow is een branch, met of zonder bescherming.
557type BranchRow struct {
558 Name string
559 Protected bool
560}
561
562// HookView is een webhook plus de laatste pogingen.
563type HookView struct {
564 ID int64
565 Name string
566 Preset string
567 URL string
568 Method string
569 Filter string
570 Events string
571 Deliveries []store.Delivery
572}
573
574func repoURL(repo store.Repo) string { return "/" + repo.FullPath() }