BlueGit

timfa bluegit

Files

bluegit / internal / server / server.go

server.go

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