Repository navigation
Expand file tree
/
Copy pathweb.go
More file actions
658 lines (585 loc) · 16.2 KB
/
Copy pathweb.go
File metadata and controls
658 lines (585 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package patchbin
import (
"context"
"embed"
"fmt"
"html/template"
"io"
"io/fs"
"log/slog"
"mime"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/alecthomas/chroma/v2"
formatterHtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
"github.com/gorilla/feeds"
)
//go:embed static/*
var embedStaticFS embed.FS
var (
//go:embed tmpl/*
tmplFS embed.FS
indexTmpl = getTemplate("index.html")
prTmpl = getTemplate("pr.html")
prsListTmpl = getTemplate("prs.html")
repoTmpl = getTemplate("repo.html")
)
type BasicData struct {
MetaData
}
type MetaData struct {
URL string
Desc template.HTML
Tab TabStatus
}
type PrListItem struct {
ID int64
Slug string
Name string
RepoName string
FormattedDate string
NumPatchsets int
}
type PrListData struct {
PRs []PrListItem
MetaData
}
type WebCtx struct {
Pr *PrCmd
Backend *Backend
Formatter *formatterHtml.Formatter
Logger *slog.Logger
Theme *chroma.Style
}
type ctxWeb struct{}
func getTemplate(page string) *template.Template {
tmpl, err := template.New("").Funcs(template.FuncMap{
"sha": shaFn,
}).ParseFS(
tmplFS,
filepath.Join("tmpl", "pages", page),
filepath.Join("tmpl", "components", "*.html"),
filepath.Join("tmpl", "base.html"),
)
if err != nil {
panic(err)
}
return tmpl.Lookup(page)
}
func getWebCtx(r *http.Request) (*WebCtx, error) {
data, ok := r.Context().Value(ctxWeb{}).(*WebCtx)
if data == nil || !ok {
return data, fmt.Errorf("webCtx not set on `r.Context()` for connection")
}
return data, nil
}
func setWebCtx(ctx context.Context, web *WebCtx) context.Context {
return context.WithValue(ctx, ctxWeb{}, web)
}
func ctxMdw(ctx context.Context, handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handler(w, r.WithContext(ctx))
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("content-type", "text/html")
err = indexTmpl.Execute(w, BasicData{
MetaData: MetaData{
URL: web.Backend.Cfg.Url,
Desc: template.HTML(web.Backend.Cfg.Desc),
},
})
if err != nil {
web.Backend.Logger.Error("cannot execute template", "err", err)
}
}
type TabStatus string
const (
TabStatusActive TabStatus = "active"
TabStatusInactive TabStatus = "inactive"
)
func createPrListHandler(tab TabStatus) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
var prs []*PatchRequest
switch TabStatus(tab) {
case TabStatusInactive:
prs, err = web.Pr.GetPatchRequestsInactive()
case TabStatusActive:
fallthrough
default:
prs, err = web.Pr.GetPatchRequestsActive()
}
if err != nil {
web.Backend.Logger.Error("cannot get patch requests", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
prItems := []PrListItem{}
for _, pr := range prs {
patchsets, err := web.Pr.GetPatchsetsByPrID(pr.ID)
if err != nil {
patchsets = nil
}
prItems = append(prItems, PrListItem{
ID: pr.ID,
Slug: pr.Slug,
Name: pr.Name,
RepoName: pr.RepoName,
FormattedDate: pr.CreatedAt.Format(web.Backend.Cfg.TimeFormat),
NumPatchsets: len(patchsets),
})
}
w.Header().Set("content-type", "text/html")
err = prsListTmpl.Execute(w, PrListData{
PRs: prItems,
MetaData: MetaData{
URL: web.Backend.Cfg.Url,
Desc: template.HTML(web.Backend.Cfg.Desc),
Tab: TabStatus(tab),
},
})
if err != nil {
web.Backend.Logger.Error("cannot execute template", "err", err)
}
}
}
func shaFn(sha string) string {
if sha == "" {
return "(none)"
}
return truncateSha(sha)
}
func createRepoPrListHandler(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
repoName := r.PathValue("repo")
if repoName == "prs" {
http.Redirect(w, r, "/active", http.StatusMovedPermanently)
return
}
prs, err := web.Pr.GetPatchRequestsByRepoName(repoName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
prItems := []PrListItem{}
for _, pr := range prs {
patchsets, _ := web.Pr.GetPatchsetsByPrID(pr.ID)
prItems = append(prItems, PrListItem{
ID: pr.ID,
Slug: pr.Slug,
Name: pr.Name,
RepoName: pr.RepoName,
FormattedDate: pr.CreatedAt.Format(web.Backend.Cfg.TimeFormat),
NumPatchsets: len(patchsets),
})
}
w.Header().Set("content-type", "text/html")
err = repoTmpl.Execute(w, struct {
Name string
Branch string
PRs []PrListItem
MetaData
}{
Name: repoName,
Branch: "main",
PRs: prItems,
MetaData: MetaData{
URL: web.Backend.Cfg.Url,
Desc: template.HTML(web.Backend.Cfg.Desc),
},
})
if err != nil {
web.Backend.Logger.Error("cannot execute template", "err", err)
}
}
func repoRssHandler(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
repoName := r.PathValue("repo")
if repoName == "prs" {
http.Redirect(w, r, "/rss", http.StatusMovedPermanently)
return
}
prs, err := web.Pr.GetPatchRequestsByRepoName(repoName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
desc := fmt.Sprintf("Events related to repo %s on %s", repoName, web.Backend.Cfg.Url)
feed := &feeds.Feed{
Title: fmt.Sprintf("%s repo events", repoName),
Link: &feeds.Link{Href: fmt.Sprintf("https://%s/%s", web.Backend.Cfg.Url, repoName)},
Description: desc,
Author: &feeds.Author{Name: "git collaboration server"},
Created: time.Now(),
}
var feedItems []*feeds.Item
for _, pr := range prs {
eventLogs, err := web.Pr.GetEventLogsByPrID(pr.ID)
if err != nil {
continue
}
for _, eventLog := range eventLogs {
user, err := web.Pr.GetUserByID(eventLog.UserID)
if err != nil {
continue
}
displayName := web.Backend.ComputeUserName(user.Pubkey)
realUrl := fmt.Sprintf("https://%s/%s/%s", web.Backend.Cfg.Url, pr.RepoName, pr.Slug)
content := fmt.Sprintf(
"<div><div>Repo: %s</div><div>Slug: %s</div><div>Event: %s</div><div>Created: %s</div><div>Data: %s</div></div>",
pr.RepoName, pr.Slug, eventLog.Event, eventLog.CreatedAt.Format(time.RFC3339Nano), eventLog.Data,
)
title := fmt.Sprintf(`%s in %s for PR "%s" (%s:%s)`, eventLog.Event, pr.RepoName, pr.Name, pr.RepoName, pr.Slug)
item := &feeds.Item{
Id: fmt.Sprintf("%d", eventLog.ID),
Title: title,
Link: &feeds.Link{Href: realUrl},
Content: content,
Created: eventLog.CreatedAt,
Description: title,
Author: &feeds.Author{Name: displayName},
}
feedItems = append(feedItems, item)
}
}
feed.Items = feedItems
rss, err := feed.ToAtom()
if err != nil {
http.Error(w, "Could not generate atom rss feed", http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/atom+xml; charset=utf-8")
_, _ = w.Write([]byte(rss))
}
func renderPrRss(w http.ResponseWriter, r *http.Request, web *WebCtx, pr *PatchRequest) {
desc := fmt.Sprintf(
"Events related to PR %s:%s on %s",
pr.RepoName, pr.Slug, web.Backend.Cfg.Url,
)
feed := &feeds.Feed{
Title: fmt.Sprintf("%s:%s events", pr.RepoName, pr.Slug),
Link: &feeds.Link{Href: fmt.Sprintf("https://%s/%s/%s", web.Backend.Cfg.Url, pr.RepoName, pr.Slug)},
Description: desc,
Author: &feeds.Author{Name: "git collaboration server"},
Created: time.Now(),
}
eventLogs, err := web.Pr.GetEventLogsByPrID(pr.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
var feedItems []*feeds.Item
for _, eventLog := range eventLogs {
user, err := web.Pr.GetUserByID(eventLog.UserID)
if err != nil {
continue
}
displayName := web.Backend.ComputeUserName(user.Pubkey)
realUrl := fmt.Sprintf("https://%s/%s/%s", web.Backend.Cfg.Url, pr.RepoName, pr.Slug)
content := fmt.Sprintf(
"<div><div>Repo: %s</div><div>Slug: %s</div><div>Event: %s</div><div>Created: %s</div><div>Data: %s</div></div>",
pr.RepoName, pr.Slug, eventLog.Event, eventLog.CreatedAt.Format(time.RFC3339Nano), eventLog.Data,
)
title := fmt.Sprintf(`%s in %s for PR "%s" (%s:%s)`, eventLog.Event, pr.RepoName, pr.Name, pr.RepoName, pr.Slug)
item := &feeds.Item{
Id: fmt.Sprintf("%d", eventLog.ID),
Title: title,
Link: &feeds.Link{Href: realUrl},
Content: content,
Created: eventLog.CreatedAt,
Description: title,
Author: &feeds.Author{Name: displayName},
}
feedItems = append(feedItems, item)
}
feed.Items = feedItems
rss, err := feed.ToAtom()
if err != nil {
http.Error(w, "Could not generate atom rss feed", http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/atom+xml; charset=utf-8")
_, _ = w.Write([]byte(rss))
}
func redirectLegacyPr(w http.ResponseWriter, r *http.Request) {
idPath := r.PathValue("id")
if idPath == "" {
idPath = r.PathValue("slug")
}
if idPath == "" {
http.Redirect(w, r, "/", http.StatusMovedPermanently)
return
}
if idPath == "active" {
http.Redirect(w, r, "/active", http.StatusMovedPermanently)
return
}
if idPath == "inactive" {
http.Redirect(w, r, "/inactive", http.StatusMovedPermanently)
return
}
parts := strings.Split(idPath, "/")
prIDStr := parts[0]
revPart := ""
if dot := strings.LastIndex(prIDStr, "."); dot != -1 {
revPart = prIDStr[dot:]
prIDStr = prIDStr[:dot]
}
prID, err := strconv.ParseInt(prIDStr, 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
web, err := getWebCtx(r)
if err != nil {
http.Error(w, "server error", 500)
return
}
pr, err := web.Pr.GetPatchRequestByID(prID)
if err != nil {
http.NotFound(w, r)
return
}
remainder := ""
if len(parts) > 1 {
remainder = "/" + strings.Join(parts[1:], "/")
}
newURL := fmt.Sprintf("/%s/%s%s%s", pr.RepoName, pr.Slug, revPart, remainder)
http.Redirect(w, r, newURL, http.StatusMovedPermanently)
}
func rssHandler(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
desc := fmt.Sprintf(
"Events related to git collaboration server %s",
web.Backend.Cfg.Url,
)
feed := &feeds.Feed{
Title: fmt.Sprintf("%s events", web.Backend.Cfg.Url),
Link: &feeds.Link{Href: web.Backend.Cfg.Url},
Description: desc,
Author: &feeds.Author{Name: "git collaboration server"},
Created: time.Now(),
}
var eventLogs []*EventLog
id := r.PathValue("id")
pubkey := r.URL.Query().Get("pubkey")
if id != "" {
var prID int64
prID, err = getPrID(id)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
eventLogs, err = web.Pr.GetEventLogsByPrID(prID)
} else if pubkey != "" {
user, perr := web.Pr.GetUserByPubkey(pubkey)
if perr != nil {
w.WriteHeader(http.StatusNotFound)
return
}
eventLogs, err = web.Pr.GetEventLogsByUserID(user.ID)
} else {
eventLogs, err = web.Pr.GetEventLogs()
}
if err != nil {
web.Logger.Error("rss could not get eventLogs", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var feedItems []*feeds.Item
for _, eventLog := range eventLogs {
user, err := web.Pr.GetUserByID(eventLog.UserID)
if err != nil {
web.Logger.Error("user not found for event log", "id", eventLog.ID, "err", err)
continue
}
pr, err := web.Pr.GetPatchRequestByID(eventLog.PatchRequestID.Int64)
if err != nil {
continue
}
displayName := web.Backend.ComputeUserName(user.Pubkey)
realUrl := fmt.Sprintf("https://%s/%s/%s", web.Backend.Cfg.Url, pr.RepoName, pr.Slug)
content := fmt.Sprintf(
"<div><div>Repo: %s</div><div>Slug: %s</div><div>Event: %s</div><div>Created: %s</div><div>Data: %s</div></div>",
pr.RepoName,
pr.Slug,
eventLog.Event,
eventLog.CreatedAt.Format(time.RFC3339Nano),
eventLog.Data,
)
title := fmt.Sprintf(
`%s in %s for PR "%s" (%s:%s)`,
eventLog.Event,
pr.RepoName,
pr.Name,
pr.RepoName,
pr.Slug,
)
item := &feeds.Item{
Id: fmt.Sprintf("%d", eventLog.ID),
Title: title,
Link: &feeds.Link{Href: realUrl},
Content: content,
Created: eventLog.CreatedAt,
Description: title,
Author: &feeds.Author{Name: displayName},
}
feedItems = append(feedItems, item)
}
feed.Items = feedItems
rss, err := feed.ToAtom()
if err != nil {
web.Logger.Error("could not generate atom rss feed", "err", err)
http.Error(w, "Could not generate atom rss feed", http.StatusInternalServerError)
}
w.Header().Add("Content-Type", "application/atom+xml; charset=utf-8")
_, err = w.Write([]byte(rss))
if err != nil {
web.Logger.Error("write error atom rss feed", "err", err)
}
}
func chromaStyleHandler(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
w.Header().Add("content-type", "text/css")
err = web.Formatter.WriteCSS(w, web.Theme)
if err != nil {
web.Backend.Logger.Error("cannot write css file", "err", err)
}
}
func serveFile(userfs fs.FS, embedfs fs.FS) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
web, err := getWebCtx(r)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
logger := web.Logger
file := r.PathValue("file")
logger.Info("serving file", "file", file)
// merging both embedded fs and whatever user provides
var reader fs.File
if userfs == nil {
reader, err = embedfs.Open(file)
} else {
reader, err = userfs.Open(file)
if err != nil {
// serve embeded static folder
reader, err = embedfs.Open(file)
}
}
if err != nil {
logger.Error(err.Error())
http.Error(w, "file not found", 404)
return
}
contents, err := io.ReadAll(reader)
if err != nil {
logger.Error(err.Error())
http.Error(w, "file not found", 404)
return
}
contentType := mime.TypeByExtension(filepath.Ext(file))
if contentType == "" {
contentType = http.DetectContentType(contents)
}
w.Header().Add("Content-Type", contentType)
_, err = w.Write(contents)
if err != nil {
logger.Error(err.Error())
http.Error(w, "server error", 500)
return
}
}
}
func getUserDefinedFS(datadir, dirName string) fs.FS {
dir := filepath.Join(datadir, dirName)
_, err := os.Stat(dir)
if err != nil {
return nil
}
return os.DirFS(dir)
}
func getEmbedFS(ffs embed.FS, dirName string) (fs.FS, error) {
fsys, err := fs.Sub(ffs, dirName)
if err != nil {
return nil, err
}
return fsys, nil
}
func GitWebServer(cfg *GitCfg) http.Handler {
dbpath := filepath.Join(cfg.DataDir, "pr.db?_fk=on")
dbh, err := SqliteOpen("file:"+dbpath, cfg.Logger)
if err != nil {
panic(fmt.Sprintf("cannot find database file, check folder and perms: %s: %s", dbpath, err))
}
be := &Backend{
DB: dbh,
Logger: cfg.Logger,
Cfg: cfg,
}
prCmd := &PrCmd{
Backend: be,
}
formatter := formatterHtml.New(
formatterHtml.WithClasses(true),
)
web := &WebCtx{
Pr: prCmd,
Backend: be,
Logger: cfg.Logger,
Formatter: formatter,
Theme: styles.Get(cfg.Theme),
}
ctx := context.Background()
ctx = setWebCtx(ctx, web)
// ensure legacy router is disabled
// GODEBUG=httpmuxgo121=0
mux := http.NewServeMux()
mux.HandleFunc("GET /", ctxMdw(ctx, indexHandler))
mux.HandleFunc("GET /active", ctxMdw(ctx, createPrListHandler("active")))
mux.HandleFunc("GET /inactive", ctxMdw(ctx, createPrListHandler("inactive")))
mux.HandleFunc("GET /rss", ctxMdw(ctx, rssHandler))
mux.HandleFunc("GET /syntax.css", ctxMdw(ctx, chromaStyleHandler))
// Repo routes
mux.HandleFunc("GET /{repo}", ctxMdw(ctx, createRepoPrListHandler))
// PR detail routes (supports branches with slashes, .patch, /rss, /patches/{patchID})
mux.HandleFunc("GET /{repo}/{slug...}", ctxMdw(ctx, createPrDetail))
embedFS, err := getEmbedFS(embedStaticFS, "static")
if err != nil {
panic(err)
}
userFS := getUserDefinedFS(cfg.DataDir, "static")
mux.HandleFunc("GET /static/{file}", ctxMdw(ctx, serveFile(userFS, embedFS)))
return mux
}