git-pr

created pr with 3.1 on 2024-07-19T14:53:23Z · by c8ef7d19
added 3.2 on 2024-07-19T14:55:48Z · by c8ef7d19
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 on 2024-07-19T15:42:29Z · by 964fa508
changed status to reviewed on 2024-07-19T15:42:29Z · by 964fa508
added 3.4 on 2024-07-19T17:43:47Z · by c8ef7d19
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 on 2024-07-19T18:27:53Z · by 964fa508
cmds
checkout latest patchset:
ssh pr.pico.sh print 3 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print 3.[rev] | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 3
set PR to open (enables RSS notifications):
ssh pr.pico.sh pr open 3
set PR to draft (stops RSS notifications):
ssh pr.pico.sh pr draft 3
+1 -1 ssh.go #
......@@ -35,7 +35,7 @@ func GitSshServer(cfg *GitCfg) {
3535 dbpath := filepath.Join(cfg.DataDir, "pr.db")
3636 dbh, err := Open(dbpath, cfg.Logger)
3737 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))
3939 }
4040
4141 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 @@
99 <meta name="keywords" content="git, collaboration, patch, requests" />
1010 {{template "meta" .}}
1111
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" />
1415 </head>
1516 <body class="container">{{template "body" .}}</body>
1617 </html>
+25 -15 web.go #
......@@ -9,6 +9,7 @@ import (
99 "io"
1010 "io/fs"
1111 "log/slog"
12+ "mime"
1213 "net/http"
1314 "os"
1415 "path/filepath"
......@@ -634,6 +635,7 @@ func serveFile(staticfs fs.FS) func(w http.ResponseWriter, r *http.Request) {
634635 logger := web.Logger
635636
636637 file := r.PathValue("file")
638+ logger.Info("serving file", "file", file, "fs", staticfs)
637639 reader, err := staticfs.Open(file)
638640 if err != nil {
639641 logger.Error(err.Error())
......@@ -646,7 +648,10 @@ func serveFile(staticfs fs.FS) func(w http.ResponseWriter, r *http.Request) {
646648 http.Error(w, "file not found", 404)
647649 return
648650 }
649- contentType := http.DetectContentType(contents)
651+ contentType := mime.TypeByExtension(filepath.Ext(file))
652+ if contentType == "" {
653+ contentType = http.DetectContentType(contents)
654+ }
650655 w.Header().Add("Content-Type", contentType)
651656
652657 _, err = w.Write(contents)
......@@ -658,13 +663,21 @@ func serveFile(staticfs fs.FS) func(w http.ResponseWriter, r *http.Request) {
658663 }
659664 }
660665
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+ }
664673
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
668681 }
669682
670683 func StartWebServer(cfg *GitCfg) {
......@@ -673,7 +686,7 @@ func StartWebServer(cfg *GitCfg) {
673686 dbpath := filepath.Join(cfg.DataDir, "pr.db")
674687 dbh, err := Open(dbpath, cfg.Logger)
675688 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))
677690 }
678691
679692 be := &Backend{
......@@ -708,14 +721,11 @@ func StartWebServer(cfg *GitCfg) {
708721 http.HandleFunc("GET /", ctxMdw(ctx, repoListHandler))
709722 http.HandleFunc("GET /syntax.css", ctxMdw(ctx, chromaStyleHandler))
710723 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)
717727 }
718- http.HandleFunc("GET /static/{file}", serveFile(filesys))
728+ http.HandleFunc("GET /static/{file}", ctxMdw(ctx, serveFile(filesys)))
719729
720730 cfg.Logger.Info("starting web server", "addr", addr)
721731 err = http.ListenAndServe(addr, nil)
Back to top