BlueGit

timfa bluegit

Files

bluegit / internal / server / server.go

server.go

feat: pipeline-editor en mail bij een mislukte run
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
b3d1a25f 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 Diff string
442 DiffFiles []DiffFileView
443 Pipes []store.Pipeline
444 PipeRows []PipeRow
445 Pipe *store.Pipeline
446 Jobs []store.Job
447 JobPage *JobPage
448 Editor *CIEditor
449 Releases []store.Release
450 Members []store.Member
451 Secrets []store.SecretMeta
452 Hooks []HookView
453 Patterns []string
454 Tokens []store.TokenInfo
455 Keys []store.SSHKey
456 CloneHTTP string
457 CloneSSH string
458 Group *store.Group
459 Tab string
460 Rev string
461 NewToken string
462 StorageUnset bool
463 StoragePath string
464 StorageFree string
465 StorageTotal string
466 MinFree string
467 Wide bool
468 Page string
469 Lang string
470 HTMLLang string
471 Cunei bool
472 Langs []LangChoice
473 Query string
474 SortDir string
475 FlipDir string
476 Cards []RepoCard
477 Head *CommitRow
478 Files []FileRow
479 Tree []FileRow
480 Readme template.HTML
481 ReadmeName string
482 LicensePath string
483 NCommits int
484 NBranches int
485 NTags int
486 NReleases int
487 Created string
488 Lines []CodeLine
489 BlobSize string
490 FileName string
491 RevProtected bool
492 TagNames []string
493 Heat *activity.Calendar
494 Notes []NoteRow
495 Profile *store.User
496}
497
498// LangChoice is één taal in de instellingen.
499type LangChoice struct {
500 Code, Label string
501}
502
503// RepoCard is een repository in een groepslijst.
504type RepoCard struct {
505 Repo store.Repo
506 Stars int
507 Starred bool
508 When string
509}
510
511// NoteRow is een reactie in de interface.
512type NoteRow struct {
513 ID int64
514 Author, Body, When, Path string
515 Start, End int
516 HasSuggestion bool
517}
518
519// DiffFileView is één bestand in de merge-request-diff.
520type DiffFileView struct {
521 Path string
522 Lines []DiffLineView
523}
524
525// DiffLineView is één diffregel, met de reacties die op het nieuwe regelnummer eindigen.
526type DiffLineView struct {
527 Kind string
528 Text string
529 Old int
530 New int
531 Notes []NoteRow
532}
533
534// FileRow is een rij in de bestandstabel of de boom.
535type FileRow struct {
536 Name, Path, Icon, Subject, When, SHA string
537 Dir bool
538 Depth int
539 Active bool
540}
541
542// CodeLine is één regel broncode.
543type CodeLine struct {
544 N int
545 HTML template.HTML
546}
547
548// CommitRow is een commit in de interface.
549// Author is de gekoppelde gebruiker, of anders de ruwe git-naam.
550// RawName en PushedBy zijn gezet als de pusher de fallback is en de git-naam afwijkt.
551type CommitRow struct {
552 SHA, Short, Author, Date, Subject, Body, When string
553 UserURL, Avatar, Initial, RawName, PushedBy string
554}
555
556// EntryRow is een bestand of map.
557type EntryRow struct {
558 Name, Type, Path string
559}
560
561// BranchRow is een branch, met of zonder bescherming.
562type BranchRow struct {
563 Name string
564 Protected bool
565}
566
567// HookView is een webhook plus de laatste pogingen.
568type HookView struct {
569 ID int64
570 Name string
571 Preset string
572 URL string
573 Method string
574 Filter string
575 Events string
576 Deliveries []store.Delivery
577}
578
579func repoURL(repo store.Repo) string { return "/" + repo.FullPath() }