git-pr
created pr with
3.1
added 3.2
1: 3b99dc0 < -: ------- feat: static assets
2: 8919af5 ! 1: 66cafc6 feat: static assets folder
3: 7346122 ! 2: 5e76ed3 fix(cli): access control for removing patchsets
4: d8792d5 < -: ------- feat: static folder
pr_reviewed
changed status to
reviewed
added 3.4
1: cc56ea1 = 1: 0467f9e feat: static assets folder
2: ef749a4 = 2: da1730f review: typo and future enhancement comment
-: ------- > 3: c038404 refactor: per-file override for static folder
changed status to
accepted
cmds
checkout latest patchset:
ssh pr.pico.sh print 3 | git am -3checkout any patchset in a patch request:
ssh pr.pico.sh print 3.[rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 3set PR to open (enables RSS notifications):
ssh pr.pico.sh pr open 3set PR to draft (stops RSS notifications):
ssh pr.pico.sh pr draft 3
Patchset
3.1
feat: static folder
Eric Bower
2024-07-19T14:52:39ZSemantic diff summary
1 added,
4 modified,
0 signature changed,
2 removed
across 2 analyzed files
(2 files skipped: unsupported file type)
+1
-1
ssh.go
#
| ... | ... | @@ -35,7 +35,7 @@ func GitSshServer(cfg *GitCfg) { | |
| 35 | 35 | dbpath := filepath.Join(cfg.DataDir, "pr.db") | |
| 36 | 36 | dbh, err := Open(dbpath, cfg.Logger) | |
| 37 | 37 | if err != nil { | |
| 38 | - | panic(fmt.Sprintf("cannot find database file, check folder and perms: %s", dbpath)) | |
| 38 | + | panic(fmt.Sprintf("cannot find database file, check folder and perms: %s: %s", dbpath, err)) | |
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | 41 | be := &Backend{ |
+18
-0
static/vars.css
#
| ... | ... | @@ -0,0 +1,18 @@ | |
| 1 | + | :root { | |
| 2 | + | --main-hue: 250; | |
| 3 | + | --white: #f2f2f2; | |
| 4 | + | --white-light: #f2f2f2; | |
| 5 | + | --white-dark: #e8e8e8; | |
| 6 | + | --code: #414558; | |
| 7 | + | --pre: #252525; | |
| 8 | + | --bg-color: #282a36; | |
| 9 | + | --text-color: #f2f2f2; | |
| 10 | + | --link-color: #8be9fd; | |
| 11 | + | --visited: #bd93f9; | |
| 12 | + | --blockquote: #bd93f9; | |
| 13 | + | --blockquote-bg: #353548; | |
| 14 | + | --hover: #ff80bf; | |
| 15 | + | --grey: #414558; | |
| 16 | + | --grey-light: #6a708e; | |
| 17 | + | --shadow: #252525; | |
| 18 | + | } |
+3
-2
tmpl/base.html
#
| ... | ... | @@ -9,8 +9,9 @@ | |
| 9 | 9 | <meta name="keywords" content="git, collaboration, patch, requests" /> | |
| 10 | 10 | {{template "meta" .}} | |
| 11 | 11 | ||
| 12 | - | <link rel="stylesheet" href="https://pico.sh/smol.css" /> | |
| 13 | - | <link rel="stylesheet" href="https://pico.sh/syntax.css" /> | |
| 12 | + | <link rel="stylesheet" href="/static/smol.css" /> | |
| 13 | + | <link rel="stylesheet" href="/static/vars.css" /> | |
| 14 | + | <link rel="stylesheet" href="/syntax.css" /> | |
| 14 | 15 | </head> | |
| 15 | 16 | <body class="container">{{template "body" .}}</body> | |
| 16 | 17 | </html> |
+25
-15
web.go
#
| ... | ... | @@ -634,6 +635,7 @@ func serveFile(staticfs fs.FS) func(w http.ResponseWriter, r *http.Request) { | |
| 634 | 635 | logger := web.Logger | |
| 635 | 636 | ||
| 636 | 637 | file := r.PathValue("file") | |
| 638 | + | logger.Info("serving file", "file", file, "fs", staticfs) | |
| 637 | 639 | reader, err := staticfs.Open(file) | |
| 638 | 640 | if err != nil { | |
| 639 | 641 | logger.Error(err.Error()) |
| ... | ... | @@ -646,7 +648,10 @@ func serveFile(staticfs fs.FS) func(w http.ResponseWriter, r *http.Request) { | |
| 646 | 648 | http.Error(w, "file not found", 404) | |
| 647 | 649 | return | |
| 648 | 650 | } | |
| 649 | - | contentType := http.DetectContentType(contents) | |
| 651 | + | contentType := mime.TypeByExtension(filepath.Ext(file)) | |
| 652 | + | if contentType == "" { | |
| 653 | + | contentType = http.DetectContentType(contents) | |
| 654 | + | } | |
| 650 | 655 | w.Header().Add("Content-Type", contentType) | |
| 651 | 656 | ||
| 652 | 657 | _, err = w.Write(contents) |
| ... | ... | @@ -658,13 +663,21 @@ func serveFile(staticfs fs.FS) func(w http.ResponseWriter, r *http.Request) { | |
| 658 | 663 | } | |
| 659 | 664 | } | |
| 660 | 665 | ||
| 661 | - | type StaticFs struct { | |
| 662 | - | Dir string | |
| 663 | - | } | |
| 666 | + | func getFileSystem(logger *slog.Logger, ffs embed.FS, datadir string, dirName string) (fs.FS, error) { | |
| 667 | + | dir := filepath.Join(datadir, dirName) | |
| 668 | + | _, err := os.Stat(dir) | |
| 669 | + | if err == nil { | |
| 670 | + | logger.Info("found folder in data_dir", "dir", dir) | |
| 671 | + | return os.DirFS(dir), nil | |
| 672 | + | } | |
| 664 | 673 | ||
| 665 | - | func (fs *StaticFs) Open(name string) (os.File, error) { | |
| 666 | - | fp, err := os.Open(filepath.Join(fs.Dir, name)) | |
| 667 | - | return *fp, err | |
| 674 | + | logger.Info("using embeded folder", "dir", dir) | |
| 675 | + | fsys, err := fs.Sub(ffs, dirName) | |
| 676 | + | if err != nil { | |
| 677 | + | return nil, err | |
| 678 | + | } | |
| 679 | + | ||
| 680 | + | return fsys, nil | |
| 668 | 681 | } | |
| 669 | 682 | ||
| 670 | 683 | func StartWebServer(cfg *GitCfg) { |
| ... | ... | @@ -673,7 +686,7 @@ func StartWebServer(cfg *GitCfg) { | |
| 673 | 686 | dbpath := filepath.Join(cfg.DataDir, "pr.db") | |
| 674 | 687 | dbh, err := Open(dbpath, cfg.Logger) | |
| 675 | 688 | if err != nil { | |
| 676 | - | panic(fmt.Sprintf("cannot find database file, check folder and perms: %s", dbpath)) | |
| 689 | + | panic(fmt.Sprintf("cannot find database file, check folder and perms: %s: %s", dbpath, err)) | |
| 677 | 690 | } | |
| 678 | 691 | ||
| 679 | 692 | be := &Backend{ |
| ... | ... | @@ -708,14 +721,11 @@ func StartWebServer(cfg *GitCfg) { | |
| 708 | 721 | http.HandleFunc("GET /", ctxMdw(ctx, repoListHandler)) | |
| 709 | 722 | http.HandleFunc("GET /syntax.css", ctxMdw(ctx, chromaStyleHandler)) | |
| 710 | 723 | http.HandleFunc("GET /rss", ctxMdw(ctx, rssHandler)) | |
| 711 | - | dir := filepath.Join(cfg.DataDir, "static") | |
| 712 | - | var filesys fs.FS = staticFS | |
| 713 | - | _, err = os.Stat(dir) | |
| 714 | - | if err == nil { | |
| 715 | - | cfg.Logger.Info("detected static folder, using instead of the default one") | |
| 716 | - | filesys = os.DirFS(dir) | |
| 724 | + | filesys, err := getFileSystem(cfg.Logger, staticFS, cfg.DataDir, "static") | |
| 725 | + | if err != nil { | |
| 726 | + | panic(err) | |
| 717 | 727 | } | |
| 718 | - | http.HandleFunc("GET /static/{file}", serveFile(filesys)) | |
| 728 | + | http.HandleFunc("GET /static/{file}", ctxMdw(ctx, serveFile(filesys))) | |
| 719 | 729 | ||
| 720 | 730 | cfg.Logger.Info("starting web server", "addr", addr) | |
| 721 | 731 | err = http.ListenAndServe(addr, nil) |