From 3da50c914acde2b2606385b80640d2ba527eacaf Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Mon, 27 Jan 2025 15:59:19 +0200 Subject: [PATCH] legit: setup db stuff and keys page --- cmd/legit/main.go | 12 +- config.yaml | 1 + go.mod | 1 + go.sum | 2 + legit/config/config.go | 7 +- legit/db/init.go | 32 ++++ legit/db/pubkeys.go | 23 +++ legit/routes/git.go | 4 +- legit/routes/handler.go | 61 ++++--- legit/routes/routes.go | 171 ++++++++++-------- legit/routes/template.go | 33 ++-- legit/routes/util.go | 12 +- legit/static/style.css | 356 +++++++++++++++++++------------------ legit/templates/head.html | 37 ++-- legit/templates/keys.html | 35 ++++ legit/templates/login.html | 4 +- 16 files changed, 479 insertions(+), 312 deletions(-) create mode 100644 legit/db/init.go create mode 100644 legit/db/pubkeys.go create mode 100644 legit/templates/keys.html diff --git a/cmd/legit/main.go b/cmd/legit/main.go index 4043640..b3b2e34 100644 --- a/cmd/legit/main.go +++ b/cmd/legit/main.go @@ -3,10 +3,10 @@ package main import ( "flag" "fmt" - "html/template" "log" + "log/slog" "net/http" - "path/filepath" + "os" "github.com/icyphox/bild/legit/config" "github.com/icyphox/bild/legit/routes" @@ -17,14 +17,18 @@ func main() { flag.StringVar(&cfg, "config", "./config.yaml", "path to config file") flag.Parse() + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil))) + c, err := config.Read(cfg) if err != nil { log.Fatal(err) } - t := template.Must(template.ParseGlob(filepath.Join(c.Dirs.Templates, "*"))) + mux, err := routes.Setup(c) + if err != nil { + log.Fatal(err) + } - mux := routes.Handlers(c, t) addr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) log.Println("starting server on", addr) log.Fatal(http.ListenAndServe(addr, mux)) diff --git a/config.yaml b/config.yaml index 2278b5e..a150c6a 100644 --- a/config.yaml +++ b/config.yaml @@ -18,3 +18,4 @@ server: name: github.com/icyphox/bild host: 0.0.0.0 port: 5555 + dbpath: bild.db diff --git a/go.mod b/go.mod index 7018017..a4be592 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/dustin/go-humanize v1.0.1 github.com/go-chi/chi/v5 v5.2.0 github.com/go-git/go-git/v5 v5.12.0 + github.com/mattn/go-sqlite3 v1.14.24 github.com/microcosm-cc/bluemonday v1.0.27 github.com/russross/blackfriday/v2 v2.1.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 7d79b8c..9acadc9 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA= github.com/mmcloughlin/avo v0.5.0/go.mod h1:ChHFdoV7ql95Wi7vuq2YT1bwCJqiWdZrQ1im3VujLYM= diff --git a/legit/config/config.go b/legit/config/config.go index 3bcf9de..7ff466f 100644 --- a/legit/config/config.go +++ b/legit/config/config.go @@ -26,9 +26,10 @@ type Config struct { SyntaxHighlight string `yaml:"syntaxHighlight"` } `yaml:"meta"` Server struct { - Name string `yaml:"name,omitempty"` - Host string `yaml:"host"` - Port int `yaml:"port"` + Name string `yaml:"name,omitempty"` + Host string `yaml:"host"` + Port int `yaml:"port"` + DBPath string `yaml:"dbpath"` } `yaml:"server"` } diff --git a/legit/db/init.go b/legit/db/init.go new file mode 100644 index 0000000..0798074 --- /dev/null +++ b/legit/db/init.go @@ -0,0 +1,32 @@ +package db + +import ( + "database/sql" + + _ "github.com/mattn/go-sqlite3" +) + +type DB struct { + db *sql.DB +} + +func Setup(dbPath string) (*DB, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + _, err = db.Exec(` + create table if not exists public_keys ( + id integer primary key autoincrement, + did text not null, + name text not null, + key text not null, + unique(did, name, key)) + `) + if err != nil { + return nil, err + } + + return &DB{db: db}, nil +} diff --git a/legit/db/pubkeys.go b/legit/db/pubkeys.go new file mode 100644 index 0000000..c0e390a --- /dev/null +++ b/legit/db/pubkeys.go @@ -0,0 +1,23 @@ +package db + +func (d *DB) AddPublicKey(did, name, key string) error { + query := `insert into public_keys (did, name, key) values (?, ?, ?)` + _, err := d.db.Exec(query, did, name, key) + return err +} + +func (d *DB) RemovePublicKey(did string) error { + query := `delete from public_keys where did = ?` + _, err := d.db.Exec(query, did) + return err +} + +func (d *DB) GetPublicKey(did string) (string, error) { + var key string + query := `select key from public_keys where did = ?` + err := d.db.QueryRow(query, did).Scan(&key) + if err != nil { + return "", err + } + return key, nil +} diff --git a/legit/routes/git.go b/legit/routes/git.go index 68a1a14..b380bed 100644 --- a/legit/routes/git.go +++ b/legit/routes/git.go @@ -10,7 +10,7 @@ import ( "github.com/icyphox/bild/legit/git/service" ) -func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) { +func (d *Handle) InfoRefs(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) name = filepath.Clean(name) @@ -31,7 +31,7 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) { } } -func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) { +func (d *Handle) UploadPack(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) name = filepath.Clean(name) diff --git a/legit/routes/handler.go b/legit/routes/handler.go index b55ad01..13a0a83 100644 --- a/legit/routes/handler.go +++ b/legit/routes/handler.go @@ -1,16 +1,19 @@ package routes import ( + "fmt" "html/template" "net/http" + "path/filepath" "github.com/go-chi/chi/v5" "github.com/icyphox/bild/legit/config" + "github.com/icyphox/bild/legit/db" ) // Checks for gitprotocol-http(5) specific smells; if found, passes // the request on to the git http service, else render the web frontend. -func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) { +func (h *Handle) Multiplex(w http.ResponseWriter, r *http.Request) { path := chi.URLParam(r, "*") if r.URL.RawQuery == "service=git-receive-pack" { @@ -22,45 +25,63 @@ func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) { if path == "info/refs" && r.URL.RawQuery == "service=git-upload-pack" && r.Method == "GET" { - d.InfoRefs(w, r) + h.InfoRefs(w, r) } else if path == "git-upload-pack" && r.Method == "POST" { - d.UploadPack(w, r) + h.UploadPack(w, r) } else if r.Method == "GET" { - d.RepoIndex(w, r) + h.RepoIndex(w, r) } } -func Handlers(c *config.Config, t *template.Template) http.Handler { +func Setup(c *config.Config) (http.Handler, error) { r := chi.NewRouter() - d := deps{c, t} + t := template.Must(template.ParseGlob(filepath.Join(c.Dirs.Templates, "*"))) + db, err := db.Setup(c.Server.DBPath) - r.Get("/login", d.Login) - r.Get("/static/{file}", d.ServeStatic) + if err != nil { + return nil, fmt.Errorf("failed to setup db: %w", err) + } + + h := Handle{ + c: c, + t: t, + db: db, + } + + r.Get("/login", h.Login) + r.Get("/static/{file}", h.ServeStatic) + + r.Route("/settings", func(r chi.Router) { + r.Get("/keys", h.Keys) + r.Put("/keys", h.Keys) + }) r.Route("/@{user}", func(r chi.Router) { - r.Get("/", d.Index) + r.Get("/", h.Index) + + // Repo routes r.Route("/{name}", func(r chi.Router) { - r.Get("/", d.Multiplex) - r.Post("/", d.Multiplex) + r.Get("/", h.Multiplex) + r.Post("/", h.Multiplex) r.Route("/tree/{ref}", func(r chi.Router) { - r.Get("/*", d.RepoTree) + r.Get("/*", h.RepoTree) }) r.Route("/blob/{ref}", func(r chi.Router) { - r.Get("/*", d.FileContent) + r.Get("/*", h.FileContent) }) - r.Get("/log/{ref}", d.Log) - r.Get("/archive/{file}", d.Archive) - r.Get("/commit/{ref}", d.Diff) - r.Get("/refs/", d.Refs) + r.Get("/log/{ref}", h.Log) + r.Get("/archive/{file}", h.Archive) + r.Get("/commit/{ref}", h.Diff) + r.Get("/refs/", h.Refs) // Catch-all routes - r.Get("/*", d.Multiplex) - r.Post("/*", d.Multiplex) + r.Get("/*", h.Multiplex) + r.Post("/*", h.Multiplex) }) }) - return r + return r, nil } diff --git a/legit/routes/routes.go b/legit/routes/routes.go index 36bb806..ac6ff2e 100644 --- a/legit/routes/routes.go +++ b/legit/routes/routes.go @@ -16,21 +16,23 @@ import ( "github.com/dustin/go-humanize" "github.com/go-chi/chi/v5" "github.com/icyphox/bild/legit/config" + "github.com/icyphox/bild/legit/db" "github.com/icyphox/bild/legit/git" "github.com/russross/blackfriday/v2" ) -type deps struct { - c *config.Config - t *template.Template +type Handle struct { + c *config.Config + t *template.Template + db *db.DB } -func (d *deps) Index(w http.ResponseWriter, r *http.Request) { +func (h *Handle) Index(w http.ResponseWriter, r *http.Request) { user := chi.URLParam(r, "user") - path := filepath.Join(d.c.Repo.ScanPath, user) + path := filepath.Join(h.c.Repo.ScanPath, user) dirs, err := os.ReadDir(path) if err != nil { - d.Write500(w) + h.Write500(w) log.Printf("reading scan path: %s", err) return } @@ -44,7 +46,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) { for _, dir := range dirs { name := dir.Name() - if !dir.IsDir() || d.isIgnored(name) || d.isUnlisted(name) { + if !dir.IsDir() || h.isIgnored(name) || h.isUnlisted(name) { continue } @@ -56,7 +58,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) { c, err := gr.LastCommit() if err != nil { - d.Write500(w) + h.Write500(w) log.Println(err) return } @@ -75,40 +77,40 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) { }) data := make(map[string]interface{}) - data["meta"] = d.c.Meta + data["meta"] = h.c.Meta data["info"] = infos - if err := d.t.ExecuteTemplate(w, "index", data); err != nil { + if err := h.t.ExecuteTemplate(w, "index", data); err != nil { log.Println(err) return } } -func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { +func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } name = filepath.Clean(name) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) fmt.Println(path) gr, err := git.Open(path, "") if err != nil { - d.Write404(w) + h.Write404(w) return } commits, err := gr.Commits() if err != nil { - d.Write500(w) + h.Write500(w) log.Println(err) return } var readmeContent template.HTML - for _, readme := range d.c.Repo.Readme { + for _, readme := range h.c.Repo.Readme { ext := filepath.Ext(readme) content, _ := gr.FileContent(readme) if len(content) > 0 { @@ -134,9 +136,9 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { log.Printf("no readme found for %s", name) } - mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch) + mainBranch, err := gr.FindMainBranch(h.c.Repo.MainBranch) if err != nil { - d.Write500(w) + h.Write500(w) log.Println(err) return } @@ -152,11 +154,11 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { data["readme"] = readmeContent data["commits"] = commits data["desc"] = getDescription(path) - data["servername"] = d.c.Server.Name - data["meta"] = d.c.Meta + data["servername"] = h.c.Server.Name + data["meta"] = h.c.Meta data["gomod"] = isGoModule(gr) - if err := d.t.ExecuteTemplate(w, "repo", data); err != nil { + if err := h.t.ExecuteTemplate(w, "repo", data); err != nil { log.Println(err) return } @@ -164,27 +166,27 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { return } -func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { +func (h *Handle) RepoTree(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } treePath := chi.URLParam(r, "*") ref := chi.URLParam(r, "ref") name = filepath.Clean(name) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) fmt.Println(path) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) + h.Write404(w) return } files, err := gr.FileTree(treePath) if err != nil { - d.Write500(w) + h.Write500(w) log.Println(err) return } @@ -197,11 +199,11 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { data["desc"] = getDescription(path) data["dotdot"] = filepath.Dir(treePath) - d.listFiles(files, data, w) + h.listFiles(files, data, w) return } -func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { +func (h *Handle) FileContent(w http.ResponseWriter, r *http.Request) { var raw bool if rawParam, err := strconv.ParseBool(r.URL.Query().Get("raw")); err == nil { raw = rawParam @@ -209,24 +211,24 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } treePath := chi.URLParam(r, "*") ref := chi.URLParam(r, "ref") name = filepath.Clean(name) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) + h.Write404(w) return } contents, err := gr.FileContent(treePath) if err != nil { - d.Write500(w) + h.Write500(w) return } data := make(map[string]any) @@ -239,20 +241,20 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { safe := sanitize([]byte(contents)) if raw { - d.showRaw(string(safe), w) + h.showRaw(string(safe), w) } else { - if d.c.Meta.SyntaxHighlight == "" { - d.showFile(string(safe), data, w) + if h.c.Meta.SyntaxHighlight == "" { + h.showFile(string(safe), data, w) } else { - d.showFileWithHighlight(treePath, string(safe), data, w) + h.showFileWithHighlight(treePath, string(safe), data, w) } } } -func (d *deps) Archive(w http.ResponseWriter, r *http.Request) { +func (h *Handle) Archive(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } @@ -260,7 +262,7 @@ func (d *deps) Archive(w http.ResponseWriter, r *http.Request) { // TODO: extend this to add more files compression (e.g.: xz) if !strings.HasSuffix(file, ".tar.gz") { - d.Write404(w) + h.Write404(w) return } @@ -272,10 +274,10 @@ func (d *deps) Archive(w http.ResponseWriter, r *http.Request) { setContentDisposition(w, filename) setGZipMIME(w) - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) + h.Write404(w) return } @@ -300,61 +302,61 @@ func (d *deps) Archive(w http.ResponseWriter, r *http.Request) { } } -func (d *deps) Log(w http.ResponseWriter, r *http.Request) { +func (h *Handle) Log(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } ref := chi.URLParam(r, "ref") - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) + h.Write404(w) return } commits, err := gr.Commits() if err != nil { - d.Write500(w) + h.Write500(w) log.Println(err) return } data := make(map[string]interface{}) data["commits"] = commits - data["meta"] = d.c.Meta + data["meta"] = h.c.Meta data["name"] = name data["displayname"] = getDisplayName(name) data["ref"] = ref data["desc"] = getDescription(path) data["log"] = true - if err := d.t.ExecuteTemplate(w, "log", data); err != nil { + if err := h.t.ExecuteTemplate(w, "log", data); err != nil { log.Println(err) return } } -func (d *deps) Diff(w http.ResponseWriter, r *http.Request) { +func (h *Handle) Diff(w http.ResponseWriter, r *http.Request) { name := uniqueName(r) - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } ref := chi.URLParam(r, "ref") - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) gr, err := git.Open(path, ref) if err != nil { - d.Write404(w) + h.Write404(w) return } diff, err := gr.Diff() if err != nil { - d.Write500(w) + h.Write500(w) log.Println(err) return } @@ -364,29 +366,29 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) { data["commit"] = diff.Commit data["stat"] = diff.Stat data["diff"] = diff.Diff - data["meta"] = d.c.Meta + data["meta"] = h.c.Meta data["name"] = name data["displayname"] = getDisplayName(name) data["ref"] = ref data["desc"] = getDescription(path) - if err := d.t.ExecuteTemplate(w, "commit", data); err != nil { + if err := h.t.ExecuteTemplate(w, "commit", data); err != nil { log.Println(err) return } } -func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { +func (h *Handle) Refs(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name") - if d.isIgnored(name) { - d.Write404(w) + if h.isIgnored(name) { + h.Write404(w) return } - path := filepath.Join(d.c.Repo.ScanPath, name) + path := filepath.Join(h.c.Repo.ScanPath, name) gr, err := git.Open(path, "") if err != nil { - d.Write404(w) + h.Write404(w) return } @@ -399,35 +401,58 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { branches, err := gr.Branches() if err != nil { log.Println(err) - d.Write500(w) + h.Write500(w) return } data := make(map[string]interface{}) - data["meta"] = d.c.Meta + data["meta"] = h.c.Meta data["name"] = name data["displayname"] = getDisplayName(name) data["branches"] = branches data["tags"] = tags data["desc"] = getDescription(path) - if err := d.t.ExecuteTemplate(w, "refs", data); err != nil { + if err := h.t.ExecuteTemplate(w, "refs", data); err != nil { log.Println(err) return } } -func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) { +func (h *Handle) ServeStatic(w http.ResponseWriter, r *http.Request) { f := chi.URLParam(r, "file") - f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f)) + f = filepath.Clean(filepath.Join(h.c.Dirs.Static, f)) http.ServeFile(w, r, f) } -func (d *deps) Login(w http.ResponseWriter, r *http.Request) { - if err := d.t.ExecuteTemplate(w, "login", nil); err != nil { +func (h *Handle) Login(w http.ResponseWriter, r *http.Request) { + if err := h.t.ExecuteTemplate(w, "login", nil); err != nil { log.Println(err) return } } + +func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + // TODO: fetch keys from db + if err := h.t.ExecuteTemplate(w, "keys", nil); err != nil { + log.Println(err) + return + } + case http.MethodPut: + key := r.FormValue("key") + name := r.FormValue("name") + // TODO: add did here + if err := h.db.AddPublicKey("did:ashtntnashtx", name, key); err != nil { + h.WriteOOBNotice(w, "keys", "Failed to add key") + log.Printf("adding public key: %s", err) + return + } + + h.WriteOOBNotice(w, "keys", "Key added!") + return + } +} diff --git a/legit/routes/template.go b/legit/routes/template.go index c78ce79..d7c96ae 100644 --- a/legit/routes/template.go +++ b/legit/routes/template.go @@ -2,6 +2,7 @@ package routes import ( "bytes" + "fmt" "html/template" "io" "log" @@ -15,30 +16,34 @@ import ( "github.com/icyphox/bild/legit/git" ) -func (d *deps) Write404(w http.ResponseWriter) { - tpath := filepath.Join(d.c.Dirs.Templates, "*") - t := template.Must(template.ParseGlob(tpath)) +func (h *Handle) Write404(w http.ResponseWriter) { w.WriteHeader(404) - if err := t.ExecuteTemplate(w, "404", nil); err != nil { + if err := h.t.ExecuteTemplate(w, "404", nil); err != nil { log.Printf("404 template: %s", err) } } -func (d *deps) Write500(w http.ResponseWriter) { - tpath := filepath.Join(d.c.Dirs.Templates, "*") - t := template.Must(template.ParseGlob(tpath)) +func (h *Handle) Write500(w http.ResponseWriter) { w.WriteHeader(500) - if err := t.ExecuteTemplate(w, "500", nil); err != nil { + if err := h.t.ExecuteTemplate(w, "500", nil); err != nil { log.Printf("500 template: %s", err) } } -func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) { - tpath := filepath.Join(d.c.Dirs.Templates, "*") +func (h *Handle) WriteOOBNotice(w http.ResponseWriter, id, msg string) { + html := fmt.Sprintf(`%s`, id, msg) + + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(http.StatusOK) + w.Write([]byte(html)) +} + +func (h *Handle) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) { + tpath := filepath.Join(h.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) data["files"] = files - data["meta"] = d.c.Meta + data["meta"] = h.c.Meta if err := t.ExecuteTemplate(w, "tree", data); err != nil { log.Println(err) @@ -72,7 +77,7 @@ func countLines(r io.Reader) (int, error) { } } -func (d *deps) showFileWithHighlight(name, content string, data map[string]any, w http.ResponseWriter) { +func (d *Handle) showFileWithHighlight(name, content string, data map[string]any, w http.ResponseWriter) { tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) @@ -114,7 +119,7 @@ func (d *deps) showFileWithHighlight(name, content string, data map[string]any, } } -func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) { +func (d *Handle) showFile(content string, data map[string]any, w http.ResponseWriter) { tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) @@ -142,7 +147,7 @@ func (d *deps) showFile(content string, data map[string]any, w http.ResponseWrit } } -func (d *deps) showRaw(content string, w http.ResponseWriter) { +func (d *Handle) showRaw(content string, w http.ResponseWriter) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "text/plain") w.Write([]byte(content)) diff --git a/legit/routes/util.go b/legit/routes/util.go index 235cef6..26ddcfa 100644 --- a/legit/routes/util.go +++ b/legit/routes/util.go @@ -43,8 +43,8 @@ func getDescription(path string) (desc string) { return } -func (d *deps) isUnlisted(name string) bool { - for _, i := range d.c.Repo.Unlisted { +func (h *Handle) isUnlisted(name string) bool { + for _, i := range h.c.Repo.Unlisted { if name == i { return true } @@ -53,8 +53,8 @@ func (d *deps) isUnlisted(name string) bool { return false } -func (d *deps) isIgnored(name string) bool { - for _, i := range d.c.Repo.Ignore { +func (h *Handle) isIgnored(name string) bool { + for _, i := range h.c.Repo.Ignore { if name == i { return true } @@ -69,7 +69,7 @@ type repoInfo struct { Category string } -func (d *deps) getAllRepos() ([]repoInfo, error) { +func (d *Handle) getAllRepos() ([]repoInfo, error) { repos := []repoInfo{} max := strings.Count(d.c.Repo.ScanPath, string(os.PathSeparator)) + 2 @@ -113,7 +113,7 @@ func (d *deps) getAllRepos() ([]repoInfo, error) { return repos, err } -func (d *deps) category(path string) string { +func (d *Handle) category(path string) string { return strings.TrimPrefix(filepath.Dir(strings.TrimPrefix(path, d.c.Repo.ScanPath)), string(os.PathSeparator)) } diff --git a/legit/static/style.css b/legit/static/style.css index 063b4c5..6467d11 100644 --- a/legit/static/style.css +++ b/legit/static/style.css @@ -1,331 +1,341 @@ :root { - --white: #fff; - --light: #f4f4f4; - --cyan: #509c93; - --light-gray: #eee; - --medium-gray: #ddd; - --gray: #6a6a6a; - --dark: #444; - --darker: #222; - - --sans-font: -apple-system, BlinkMacSystemFont, "Inter", "Roboto", "Segoe UI", sans-serif; - --display-font: -apple-system, BlinkMacSystemFont, "Inter", "Roboto", "Segoe UI", sans-serif; - --mono-font: 'SF Mono', SFMono-Regular, ui-monospace, 'DejaVu Sans Mono', 'Roboto Mono', Menlo, Consolas, monospace; + --white: #fff; + --light: #f4f4f4; + --cyan: #509c93; + --light-gray: #eee; + --medium-gray: #ddd; + --gray: #6a6a6a; + --dark: #444; + --darker: #222; + + --sans-font: -apple-system, BlinkMacSystemFont, "Inter", "Roboto", + "Segoe UI", sans-serif; + --display-font: -apple-system, BlinkMacSystemFont, "Inter", "Roboto", + "Segoe UI", sans-serif; + --mono-font: "SF Mono", SFMono-Regular, ui-monospace, "DejaVu Sans Mono", + "Roboto Mono", Menlo, Consolas, monospace; } @media (prefers-color-scheme: dark) { - :root { - color-scheme: dark light; - --light: #181818; - --cyan: #76c7c0; - --light-gray: #333; - --medium-gray: #444; - --gray: #aaa; - --dark: #ddd; - --darker: #f4f4f4; - } + :root { + color-scheme: dark light; + --white: #000; + --light: #181818; + --cyan: #76c7c0; + --light-gray: #333; + --medium-gray: #444; + --gray: #aaa; + --dark: #ddd; + --darker: #f4f4f4; + } } html { - background: var(--white); - -webkit-text-size-adjust: none; - font-family: var(--sans-font); - font-weight: 380; + background: var(--white); + -webkit-text-size-adjust: none; + font-family: var(--sans-font); + font-weight: 380; } pre { - font-family: var(--mono-font); + font-family: var(--mono-font); } ::selection { - background: var(--medium-gray); - opacity: 0.3; + background: var(--medium-gray); + opacity: 0.3; } * { - box-sizing: border-box; - padding: 0; - margin: 0; + box-sizing: border-box; + padding: 0; + margin: 0; } body { - max-width: 1000px; - padding: 0 13px; - margin: 40px auto; + max-width: 1000px; + padding: 0 13px; + margin: 40px auto; } -main, footer { - font-size: 1rem; - padding: 0; - line-height: 160%; +main, +footer { + font-size: 1rem; + padding: 0; + line-height: 160%; } -header h1, h2, h3 { - font-family: var(--display-font); +header h1, +h2, +h3 { + font-family: var(--display-font); } h2 { - font-weight: 400; + font-weight: 400; } strong { - font-weight: 500; + font-weight: 500; } main h1 { - padding: 10px 0 10px 0; + padding: 10px 0 10px 0; } main h2 { - font-size: 18px; + font-size: 18px; } -main h2, h3 { - padding: 20px 0 15px 0; +main h2, +h3 { + padding: 20px 0 15px 0; } nav { - padding: 0.4rem 0 1.5rem 0; + padding: 0.4rem 0 1.5rem 0; } nav ul { - padding: 0; - margin: 0; - list-style: none; - padding-bottom: 20px; + padding: 0; + margin: 0; + list-style: none; + padding-bottom: 20px; } nav ul li { - padding-right: 10px; - display: inline-block; + padding-right: 10px; + display: inline-block; } a { - margin: 0; - padding: 0; - box-sizing: border-box; - text-decoration: none; - word-wrap: break-word; + margin: 0; + padding: 0; + box-sizing: border-box; + text-decoration: none; + word-wrap: break-word; } a { - color: var(--darker); - border-bottom: 1.5px solid var(--medium-gray); + color: var(--darker); + border-bottom: 1.5px solid var(--medium-gray); } a:hover { - border-bottom: 1.5px solid var(--gray); + border-bottom: 1.5px solid var(--gray); } .index { - padding-top: 2em; - display: grid; - grid-template-columns: 6em 1fr minmax(0, 7em); - grid-row-gap: 0.5em; - min-width: 0; + padding-top: 2em; + display: grid; + grid-template-columns: 6em 1fr minmax(0, 7em); + grid-row-gap: 0.5em; + min-width: 0; } .clone-url { - padding-top: 2rem; + padding-top: 2rem; } .clone-url pre { - color: var(--dark); - white-space: pre-wrap; + color: var(--dark); + white-space: pre-wrap; } .desc { - font-weight: normal; - color: var(--gray); - font-style: italic; + font-weight: normal; + color: var(--gray); + font-style: italic; } .tree { - display: grid; - grid-template-columns: 10ch auto 1fr; - grid-row-gap: 0.5em; - grid-column-gap: 1em; - min-width: 0; + display: grid; + grid-template-columns: 10ch auto 1fr; + grid-row-gap: 0.5em; + grid-column-gap: 1em; + min-width: 0; } .log { - display: grid; - grid-template-columns: 20rem minmax(0, 1fr); - grid-row-gap: 0.8em; - grid-column-gap: 8rem; - margin-bottom: 2em; - padding-bottom: 1em; - border-bottom: 1.5px solid var(--medium-gray); + display: grid; + grid-template-columns: 20rem minmax(0, 1fr); + grid-row-gap: 0.8em; + grid-column-gap: 8rem; + margin-bottom: 2em; + padding-bottom: 1em; + border-bottom: 1.5px solid var(--medium-gray); } .log pre { - white-space: pre-wrap; + white-space: pre-wrap; } -.mode, .size { - font-family: var(--mono-font); +.mode, +.size { + font-family: var(--mono-font); } .size { - text-align: right; + text-align: right; } .readme pre { - white-space: pre-wrap; - overflow-x: auto; + white-space: pre-wrap; + overflow-x: auto; } .readme { - background: var(--light-gray); - padding: 0.5rem; + background: var(--light-gray); + padding: 0.5rem; } .readme ul { - padding: revert; + padding: revert; } .readme img { - max-width: 100%; + max-width: 100%; } .diff { - margin: 1rem 0 1rem 0; - padding: 1rem 0 1rem 0; - border-bottom: 1.5px solid var(--medium-gray); + margin: 1rem 0 1rem 0; + padding: 1rem 0 1rem 0; + border-bottom: 1.5px solid var(--medium-gray); } .diff pre { - overflow: scroll; + overflow: scroll; } .diff-stat { - padding: 1rem 0 1rem 0; + padding: 1rem 0 1rem 0; } -.commit-hash, .commit-email { - font-family: var(--mono-font); +.commit-hash, +.commit-email { + font-family: var(--mono-font); } .commit-email:before { - content: '<'; + content: "<"; } .commit-email:after { - content: '>'; + content: ">"; } .commit { - margin-bottom: 1rem; + margin-bottom: 1rem; } .commit pre { - padding-bottom: 1rem; - white-space: pre-wrap; + padding-bottom: 1rem; + white-space: pre-wrap; } .diff-stat ul li { - list-style: none; - padding-left: 0.5em; + list-style: none; + padding-left: 0.5em; } .diff-add { - color: green; + color: green; } .diff-del { - color: red; + color: red; } .diff-noop { - color: var(--gray); + color: var(--gray); } .ref { - font-family: var(--sans-font); - font-size: 14px; - color: var(--gray); - display: inline-block; - padding-top: 0.7em; + font-family: var(--sans-font); + font-size: 14px; + color: var(--gray); + display: inline-block; + padding-top: 0.7em; } .refs pre { - white-space: pre-wrap; - padding-bottom: 0.5rem; + white-space: pre-wrap; + padding-bottom: 0.5rem; } .refs strong { - padding-right: 1em; + padding-right: 1em; } .line-numbers { - white-space: pre-line; - -moz-user-select: -moz-none; - -khtml-user-select: none; - -webkit-user-select: none; - -o-user-select: none; - user-select: none; - display: flex; - float: left; - flex-direction: column; - margin-right: 1ch; + white-space: pre-line; + -moz-user-select: -moz-none; + -khtml-user-select: none; + -webkit-user-select: none; + -o-user-select: none; + user-select: none; + display: flex; + float: left; + flex-direction: column; + margin-right: 1ch; } .file-wrapper { - display: flex; - flex-direction: row; - grid-template-columns: 1rem minmax(0, 1fr); - gap: 1rem; - padding: 0.5rem; - background: var(--light-gray); - overflow-x: auto; + display: flex; + flex-direction: row; + grid-template-columns: 1rem minmax(0, 1fr); + gap: 1rem; + padding: 0.5rem; + background: var(--light-gray); + overflow-x: auto; } .chroma-file-wrapper { - display: flex; - flex-direction: row; - grid-template-columns: 1rem minmax(0, 1fr); - overflow-x: auto; + display: flex; + flex-direction: row; + grid-template-columns: 1rem minmax(0, 1fr); + overflow-x: auto; } .file-content { - background: var(--light-gray); - overflow-y: hidden; - overflow-x: auto; + background: var(--light-gray); + overflow-y: hidden; + overflow-x: auto; } .diff-type { - color: var(--gray); + color: var(--gray); } .commit-info { - color: var(--gray); - padding-bottom: 1.5rem; - font-size: 0.85rem; + color: var(--gray); + padding-bottom: 1.5rem; + font-size: 0.85rem; } @media (max-width: 600px) { - .index { - grid-row-gap: 0.8em; - } - - .log { - grid-template-columns: 1fr; - grid-row-gap: 0em; - } - - .index { - grid-template-columns: 1fr; - grid-row-gap: 0em; - } - - .index-name:not(:first-child) { - padding-top: 1.5rem; - } - - .commit-info:not(:last-child) { - padding-bottom: 1.5rem; - } - - pre { - font-size: 0.8rem; - } + .index { + grid-row-gap: 0.8em; + } + + .log { + grid-template-columns: 1fr; + grid-row-gap: 0em; + } + + .index { + grid-template-columns: 1fr; + grid-row-gap: 0em; + } + + .index-name:not(:first-child) { + padding-top: 1.5rem; + } + + .commit-info:not(:last-child) { + padding-bottom: 1.5rem; + } + + pre { + font-size: 0.8rem; + } } diff --git a/legit/templates/head.html b/legit/templates/head.html index b3f71c9..f79a4de 100644 --- a/legit/templates/head.html +++ b/legit/templates/head.html @@ -1,32 +1,39 @@ {{ define "head" }} - - - - - + + + + + + + + {{ if .parent }} - {{ .meta.Title }} — {{ .name }} ({{ .ref }}): {{ .parent }}/ + + {{ .meta.Title }} — {{ .name }} ({{ .ref }}): {{ .parent }}/ + {{ else if .path }} - {{ .meta.Title }} — {{ .name }} ({{ .ref }}): {{ .path }} + + {{ .meta.Title }} — {{ .name }} ({{ .ref }}): {{ .path }} + {{ else if .files }} {{ .meta.Title }} — {{ .name }} ({{ .ref }}) {{ else if .commit }} {{ .meta.Title }} — {{ .name }}: {{ .commit.This }} {{ else if .branches }} {{ .meta.Title }} — {{ .name }}: refs - {{ else if .commits }} - {{ if .log }} + {{ else if .commits }} {{ if .log }} {{ .meta.Title }} — {{ .name }}: log {{ else }} {{ .meta.Title }} — {{ .name }} - {{ end }} - {{ else }} + {{ end }} {{ else }} {{ .meta.Title }} - {{ end }} - {{ if and .servername .gomod }} - + {{ end }} {{ if and .servername .gomod }} + {{ end }} - + {{ end }} diff --git a/legit/templates/keys.html b/legit/templates/keys.html new file mode 100644 index 0000000..25796dd --- /dev/null +++ b/legit/templates/keys.html @@ -0,0 +1,35 @@ +{{ define "keys" }} + + {{ template "head" . }} + + +
+
+

+ Give your key a name and paste your + public key here. This is what you'll use to + push to your Git repository. +

+
+
+ + +
+ +
+
+ + +{{ end }} diff --git a/legit/templates/login.html b/legit/templates/login.html index 52c7636..1c7259d 100644 --- a/legit/templates/login.html +++ b/legit/templates/login.html @@ -10,8 +10,8 @@ action="http://localhost:3000/login" >

- You will be redirected to bsky.app (or your PDS) to complete - login. + You will be redirected to bsky.social (or your PDS) to + complete login.