diff --git a/.gitignore b/.gitignore index ed10d54f..5469a0f2 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,10 @@ go.work.sum # go generate /cmd/hakurei/LICENSE +/cmd/pkgserver/.sass-cache +/cmd/pkgserver/ui/static/*.js +/cmd/pkgserver/ui/static/*.css* +/cmd/pkgserver/ui/static/*.css.map /internal/pkg/testdata/testtool /internal/rosa/hakurei_current.tar.gz diff --git a/cmd/pkgserver/api.go b/cmd/pkgserver/api.go new file mode 100644 index 00000000..914479c3 --- /dev/null +++ b/cmd/pkgserver/api.go @@ -0,0 +1,176 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "net/url" + "path" + "strconv" + "sync" + + "hakurei.app/internal/info" + "hakurei.app/internal/rosa" +) + +// for lazy initialisation of serveInfo +var ( + infoPayload struct { + // Current package count. + Count int `json:"count"` + // Hakurei version, set at link time. + HakureiVersion string `json:"hakurei_version"` + } + infoPayloadOnce sync.Once +) + +// handleInfo writes constant system information. +func handleInfo(w http.ResponseWriter, _ *http.Request) { + infoPayloadOnce.Do(func() { + infoPayload.Count = int(rosa.PresetUnexportedStart) + infoPayload.HakureiVersion = info.Version() + }) + // TODO(mae): cache entire response if no additional fields are planned + writeAPIPayload(w, infoPayload) +} + +// newStatusHandler returns a [http.HandlerFunc] that offers status files for +// viewing or download, if available. +func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + m, ok := index.names[path.Base(r.URL.Path)] + if !ok || !m.HasReport { + http.NotFound(w, r) + return + } + + contentType := "text/plain; charset=utf-8" + if disposition { + contentType = "application/octet-stream" + + // quoting like this is unsound, but okay, because metadata is hardcoded + contentDisposition := `attachment; filename="` + contentDisposition += m.Name + "-" + if m.Version != "" { + contentDisposition += m.Version + "-" + } + contentDisposition += m.ids + `.log"` + w.Header().Set("Content-Disposition", contentDisposition) + } + w.Header().Set("Content-Type", contentType) + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + if err := func() (err error) { + defer index.handleAccess(&err)() + _, err = w.Write(m.status) + return + }(); err != nil { + log.Println(err) + http.Error( + w, "cannot deliver status, contact maintainers", + http.StatusInternalServerError, + ) + } + } +} + +// handleGet writes a slice of metadata with specified order. +func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + limit, err := strconv.Atoi(q.Get("limit")) + if err != nil || limit > 100 || limit < 1 { + http.Error( + w, "limit must be an integer between 1 and 100", + http.StatusBadRequest, + ) + return + } + i, err := strconv.Atoi(q.Get("index")) + if err != nil || i >= len(index.sorts[0]) || i < 0 { + http.Error( + w, "index must be an integer between 0 and "+ + strconv.Itoa(int(rosa.PresetUnexportedStart-1)), + http.StatusBadRequest, + ) + return + } + sort, err := strconv.Atoi(q.Get("sort")) + if err != nil || sort >= len(index.sorts) || sort < 0 { + http.Error( + w, "sort must be an integer between 0 and "+ + strconv.Itoa(sortOrderEnd), + http.StatusBadRequest, + ) + return + } + values := index.sorts[sort][i:min(i+limit, len(index.sorts[sort]))] + writeAPIPayload(w, &struct { + Values []*metadata `json:"values"` + }{values}) +} + +func (index *packageIndex) handleSearch(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + limit, err := strconv.Atoi(q.Get("limit")) + if err != nil || limit > 100 || limit < 1 { + http.Error( + w, "limit must be an integer between 1 and 100", + http.StatusBadRequest, + ) + return + } + i, err := strconv.Atoi(q.Get("index")) + if err != nil || i >= len(index.sorts[0]) || i < 0 { + http.Error( + w, "index must be an integer between 0 and "+ + strconv.Itoa(int(rosa.PresetUnexportedStart-1)), + http.StatusBadRequest, + ) + return + } + search, err := url.PathUnescape(q.Get("search")) + if len(search) > 100 || err != nil { + http.Error( + w, "search must be a string between 0 and 100 characters long", + http.StatusBadRequest, + ) + return + } + desc := q.Get("desc") == "true" + n, res, err := index.performSearchQuery(limit, i, search, desc) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + writeAPIPayload(w, &struct { + Count int `json:"count"` + Results []searchResult `json:"results"` + }{n, res}) +} + +// apiVersion is the name of the current API revision, as part of the pattern. +const apiVersion = "v1" + +// registerAPI registers API handler functions. +func (index *packageIndex) registerAPI(mux *http.ServeMux) { + mux.HandleFunc("GET /api/"+apiVersion+"/info", handleInfo) + mux.HandleFunc("GET /api/"+apiVersion+"/get", index.handleGet) + mux.HandleFunc("GET /api/"+apiVersion+"/search", index.handleSearch) + mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.newStatusHandler(false)) + mux.HandleFunc("GET /status/", index.newStatusHandler(true)) +} + +// writeAPIPayload sets headers common to API responses and encodes payload as +// JSON for the response body. +func writeAPIPayload(w http.ResponseWriter, payload any) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + w.Header().Set("Pragma", "no-cache") + w.Header().Set("Expires", "0") + + if err := json.NewEncoder(w).Encode(payload); err != nil { + log.Println(err) + http.Error( + w, "cannot encode payload, contact maintainers", + http.StatusInternalServerError, + ) + } +} diff --git a/cmd/pkgserver/api_test.go b/cmd/pkgserver/api_test.go new file mode 100644 index 00000000..2337c92a --- /dev/null +++ b/cmd/pkgserver/api_test.go @@ -0,0 +1,183 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "slices" + "strconv" + "testing" + + "hakurei.app/internal/info" + "hakurei.app/internal/rosa" +) + +// prefix is prepended to every API path. +const prefix = "/api/" + apiVersion + "/" + +func TestAPIInfo(t *testing.T) { + t.Parallel() + + w := httptest.NewRecorder() + handleInfo(w, httptest.NewRequestWithContext( + t.Context(), + http.MethodGet, + prefix+"info", + nil, + )) + + resp := w.Result() + checkStatus(t, resp, http.StatusOK) + checkAPIHeader(t, w.Header()) + + checkPayload(t, resp, struct { + Count int `json:"count"` + HakureiVersion string `json:"hakurei_version"` + }{int(rosa.PresetUnexportedStart), info.Version()}) +} + +func TestAPIGet(t *testing.T) { + t.Parallel() + const target = prefix + "get" + + index := newIndex(t) + newRequest := func(suffix string) *httptest.ResponseRecorder { + w := httptest.NewRecorder() + index.handleGet(w, httptest.NewRequestWithContext( + t.Context(), + http.MethodGet, + target+suffix, + nil, + )) + return w + } + + checkValidate := func(t *testing.T, suffix string, vmin, vmax int, wantErr string) { + t.Run("invalid", func(t *testing.T) { + t.Parallel() + + w := newRequest("?" + suffix + "=invalid") + resp := w.Result() + checkError(t, resp, wantErr, http.StatusBadRequest) + }) + + t.Run("min", func(t *testing.T) { + t.Parallel() + + w := newRequest("?" + suffix + "=" + strconv.Itoa(vmin-1)) + resp := w.Result() + checkError(t, resp, wantErr, http.StatusBadRequest) + + w = newRequest("?" + suffix + "=" + strconv.Itoa(vmin)) + resp = w.Result() + checkStatus(t, resp, http.StatusOK) + }) + + t.Run("max", func(t *testing.T) { + t.Parallel() + + w := newRequest("?" + suffix + "=" + strconv.Itoa(vmax+1)) + resp := w.Result() + checkError(t, resp, wantErr, http.StatusBadRequest) + + w = newRequest("?" + suffix + "=" + strconv.Itoa(vmax)) + resp = w.Result() + checkStatus(t, resp, http.StatusOK) + }) + } + + t.Run("limit", func(t *testing.T) { + t.Parallel() + checkValidate( + t, "index=0&sort=0&limit", 1, 100, + "limit must be an integer between 1 and 100", + ) + }) + + t.Run("index", func(t *testing.T) { + t.Parallel() + checkValidate( + t, "limit=1&sort=0&index", 0, int(rosa.PresetUnexportedStart-1), + "index must be an integer between 0 and "+strconv.Itoa(int(rosa.PresetUnexportedStart-1)), + ) + }) + + t.Run("sort", func(t *testing.T) { + t.Parallel() + checkValidate( + t, "index=0&limit=1&sort", 0, int(sortOrderEnd), + "sort must be an integer between 0 and "+strconv.Itoa(int(sortOrderEnd)), + ) + }) + + checkWithSuffix := func(name, suffix string, want []*metadata) { + t.Run(name, func(t *testing.T) { + t.Parallel() + + w := newRequest(suffix) + resp := w.Result() + checkStatus(t, resp, http.StatusOK) + checkAPIHeader(t, w.Header()) + checkPayloadFunc(t, resp, func(got *struct { + Count int `json:"count"` + Values []*metadata `json:"values"` + }) bool { + return got.Count == len(want) && + slices.EqualFunc(got.Values, want, func(a, b *metadata) bool { + return (a.Version == b.Version || + a.Version == rosa.Unversioned || + b.Version == rosa.Unversioned) && + a.HasReport == b.HasReport && + a.Name == b.Name && + a.Description == b.Description && + a.Website == b.Website + }) + }) + + }) + } + + checkWithSuffix("declarationAscending", "?limit=2&index=0&sort=0", []*metadata{ + { + Metadata: rosa.GetMetadata(0), + Version: rosa.Std.Version(0), + }, + { + Metadata: rosa.GetMetadata(1), + Version: rosa.Std.Version(1), + }, + }) + checkWithSuffix("declarationAscending offset", "?limit=3&index=5&sort=0", []*metadata{ + { + Metadata: rosa.GetMetadata(5), + Version: rosa.Std.Version(5), + }, + { + Metadata: rosa.GetMetadata(6), + Version: rosa.Std.Version(6), + }, + { + Metadata: rosa.GetMetadata(7), + Version: rosa.Std.Version(7), + }, + }) + checkWithSuffix("declarationDescending", "?limit=3&index=0&sort=1", []*metadata{ + { + Metadata: rosa.GetMetadata(rosa.PresetUnexportedStart - 1), + Version: rosa.Std.Version(rosa.PresetUnexportedStart - 1), + }, + { + Metadata: rosa.GetMetadata(rosa.PresetUnexportedStart - 2), + Version: rosa.Std.Version(rosa.PresetUnexportedStart - 2), + }, + { + Metadata: rosa.GetMetadata(rosa.PresetUnexportedStart - 3), + Version: rosa.Std.Version(rosa.PresetUnexportedStart - 3), + }, + }) + checkWithSuffix("declarationDescending offset", "?limit=1&index=37&sort=1", []*metadata{ + { + Metadata: rosa.GetMetadata(rosa.PresetUnexportedStart - 38), + Version: rosa.Std.Version(rosa.PresetUnexportedStart - 38), + }, + }) +} diff --git a/cmd/pkgserver/index.go b/cmd/pkgserver/index.go new file mode 100644 index 00000000..1dcdd27f --- /dev/null +++ b/cmd/pkgserver/index.go @@ -0,0 +1,105 @@ +package main + +import ( + "cmp" + "errors" + "slices" + "strings" + + "hakurei.app/internal/pkg" + "hakurei.app/internal/rosa" +) + +const ( + declarationAscending = iota + declarationDescending + nameAscending + nameDescending + sizeAscending + sizeDescending + + sortOrderEnd = iota - 1 +) + +// packageIndex refers to metadata by name and various sort orders. +type packageIndex struct { + sorts [sortOrderEnd + 1][rosa.PresetUnexportedStart]*metadata + names map[string]*metadata + search searchCache + // Taken from [rosa.Report] if available. + handleAccess func(*error) func() +} + +// metadata holds [rosa.Metadata] extended with additional information. +type metadata struct { + p rosa.PArtifact + *rosa.Metadata + + // Populated via [rosa.Toolchain.Version], [rosa.Unversioned] is equivalent + // to the zero value. Otherwise, the zero value is invalid. + Version string `json:"version,omitempty"` + // Output data size, available if present in report. + Size int64 `json:"size,omitempty"` + // Whether the underlying [pkg.Artifact] is present in the report. + HasReport bool `json:"report"` + + // Ident string encoded ahead of time. + ids string + // Backed by [rosa.Report], access must be prepared by HandleAccess. + status []byte +} + +// populate deterministically populates packageIndex, optionally with a report. +func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err error) { + if report != nil { + defer report.HandleAccess(&err)() + index.handleAccess = report.HandleAccess + } + + var work [rosa.PresetUnexportedStart]*metadata + index.names = make(map[string]*metadata) + for p := range rosa.PresetUnexportedStart { + m := metadata{ + p: p, + + Metadata: rosa.GetMetadata(p), + Version: rosa.Std.Version(p), + } + if m.Version == "" { + return errors.New("invalid version from " + m.Name) + } + if m.Version == rosa.Unversioned { + m.Version = "" + } + + if cache != nil && report != nil { + id := cache.Ident(rosa.Std.Load(p)) + m.ids = pkg.Encode(id.Value()) + m.status, m.Size = report.ArtifactOf(id) + m.HasReport = m.Size >= 0 + } + + work[p] = &m + index.names[m.Name] = &m + } + + index.sorts[declarationAscending] = work + index.sorts[declarationDescending] = work + slices.Reverse(index.sorts[declarationDescending][:]) + + index.sorts[nameAscending] = work + slices.SortFunc(index.sorts[nameAscending][:], func(a, b *metadata) int { + return strings.Compare(a.Name, b.Name) + }) + index.sorts[nameDescending] = index.sorts[nameAscending] + slices.Reverse(index.sorts[nameDescending][:]) + + index.sorts[sizeAscending] = work + slices.SortFunc(index.sorts[sizeAscending][:], func(a, b *metadata) int { + return cmp.Compare(a.Size, b.Size) + }) + index.sorts[sizeDescending] = index.sorts[sizeAscending] + slices.Reverse(index.sorts[sizeDescending][:]) + + return +} diff --git a/cmd/pkgserver/main.go b/cmd/pkgserver/main.go new file mode 100644 index 00000000..af59ff2e --- /dev/null +++ b/cmd/pkgserver/main.go @@ -0,0 +1,114 @@ +package main + +import ( + "context" + "errors" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "hakurei.app/command" + "hakurei.app/container/check" + "hakurei.app/internal/pkg" + "hakurei.app/internal/rosa" + "hakurei.app/message" +) + +const shutdownTimeout = 15 * time.Second + +func main() { + log.SetFlags(0) + log.SetPrefix("pkgserver: ") + + var ( + flagBaseDir string + flagAddr string + ) + + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + defer stop() + msg := message.New(log.Default()) + + c := command.New(os.Stderr, log.Printf, "pkgserver", func(args []string) error { + var ( + cache *pkg.Cache + report *rosa.Report + ) + switch len(args) { + case 0: + break + + case 1: + baseDir, err := check.NewAbs(flagBaseDir) + if err != nil { + return err + } + + cache, err = pkg.Open(ctx, msg, 0, baseDir) + if err != nil { + return err + } + defer cache.Close() + + report, err = rosa.OpenReport(args[0]) + if err != nil { + return err + } + + default: + return errors.New("pkgserver requires 1 argument") + + } + + var index packageIndex + index.search = make(searchCache) + if err := index.populate(cache, report); err != nil { + return err + } + ticker := time.NewTicker(1 * time.Minute) + go func() { + for { + select { + case <-ctx.Done(): + ticker.Stop() + return + case <-ticker.C: + index.search.clean() + } + } + }() + var mux http.ServeMux + uiRoutes(&mux) + index.registerAPI(&mux) + server := http.Server{ + Addr: flagAddr, + Handler: &mux, + } + go func() { + <-ctx.Done() + c, cancel := context.WithTimeout(context.Background(), shutdownTimeout) + defer cancel() + if err := server.Shutdown(c); err != nil { + log.Fatal(err) + } + }() + return server.ListenAndServe() + }).Flag( + &flagBaseDir, + "b", command.StringFlag(""), + "base directory for cache", + ).Flag( + &flagAddr, + "addr", command.StringFlag(":8067"), + "TCP network address to listen on", + ) + c.MustParse(os.Args[1:], func(err error) { + if errors.Is(err, http.ErrServerClosed) { + os.Exit(0) + } + log.Fatal(err) + }) +} diff --git a/cmd/pkgserver/main_test.go b/cmd/pkgserver/main_test.go new file mode 100644 index 00000000..6acaa8f6 --- /dev/null +++ b/cmd/pkgserver/main_test.go @@ -0,0 +1,96 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "reflect" + "testing" +) + +// newIndex returns the address of a newly populated packageIndex. +func newIndex(t *testing.T) *packageIndex { + t.Helper() + + var index packageIndex + if err := index.populate(nil, nil); err != nil { + t.Fatalf("populate: error = %v", err) + } + return &index +} + +// checkStatus checks response status code. +func checkStatus(t *testing.T, resp *http.Response, want int) { + t.Helper() + + if resp.StatusCode != want { + t.Errorf( + "StatusCode: %s, want %s", + http.StatusText(resp.StatusCode), + http.StatusText(want), + ) + } +} + +// checkHeader checks the value of a header entry. +func checkHeader(t *testing.T, h http.Header, key, want string) { + t.Helper() + + if got := h.Get(key); got != want { + t.Errorf("%s: %q, want %q", key, got, want) + } +} + +// checkAPIHeader checks common entries set for API endpoints. +func checkAPIHeader(t *testing.T, h http.Header) { + t.Helper() + + checkHeader(t, h, "Content-Type", "application/json; charset=utf-8") + checkHeader(t, h, "Cache-Control", "no-cache, no-store, must-revalidate") + checkHeader(t, h, "Pragma", "no-cache") + checkHeader(t, h, "Expires", "0") +} + +// checkPayloadFunc checks the JSON response of an API endpoint by passing it to f. +func checkPayloadFunc[T any]( + t *testing.T, + resp *http.Response, + f func(got *T) bool, +) { + t.Helper() + + var got T + r := io.Reader(resp.Body) + if testing.Verbose() { + var buf bytes.Buffer + r = io.TeeReader(r, &buf) + defer func() { t.Helper(); t.Log(buf.String()) }() + } + if err := json.NewDecoder(r).Decode(&got); err != nil { + t.Fatalf("Decode: error = %v", err) + } + + if !f(&got) { + t.Errorf("Body: %#v", got) + } +} + +// checkPayload checks the JSON response of an API endpoint. +func checkPayload[T any](t *testing.T, resp *http.Response, want T) { + t.Helper() + + checkPayloadFunc(t, resp, func(got *T) bool { + return reflect.DeepEqual(got, &want) + }) +} + +func checkError(t *testing.T, resp *http.Response, error string, code int) { + t.Helper() + + checkStatus(t, resp, code) + if got, _ := io.ReadAll(resp.Body); string(got) != fmt.Sprintln(error) { + t.Errorf("Body: %q, want %q", string(got), error) + } +} diff --git a/cmd/pkgserver/search.go b/cmd/pkgserver/search.go new file mode 100644 index 00000000..ba41628b --- /dev/null +++ b/cmd/pkgserver/search.go @@ -0,0 +1,77 @@ +package main + +import ( + "cmp" + "maps" + "regexp" + "slices" + "time" +) + +type searchCache map[string]searchCacheEntry +type searchResult struct { + NameIndices [][]int `json:"name_matches"` + DescIndices [][]int `json:"desc_matches,omitempty"` + Score float64 `json:"score"` + *metadata +} +type searchCacheEntry struct { + query string + results []searchResult + expiry time.Time +} + +func (index *packageIndex) performSearchQuery(limit int, i int, search string, desc bool) (int, []searchResult, error) { + entry, ok := index.search[search] + if ok { + return len(entry.results), entry.results[i:min(i+limit, len(entry.results))], nil + } + + regex, err := regexp.Compile(search) + if err != nil { + return 0, make([]searchResult, 0), err + } + res := make([]searchResult, 0) + for p := range maps.Values(index.names) { + nameIndices := regex.FindAllIndex([]byte(p.Name), -1) + var descIndices [][]int = nil + if desc { + descIndices = regex.FindAllIndex([]byte(p.Description), -1) + } + if nameIndices == nil && descIndices == nil { + continue + } + score := float64(indexsum(nameIndices)) / (float64(len(nameIndices)) + 1) + if desc { + score += float64(indexsum(descIndices)) / (float64(len(descIndices)) + 1) / 10.0 + } + res = append(res, searchResult{ + NameIndices: nameIndices, + DescIndices: descIndices, + Score: score, + metadata: p, + }) + } + slices.SortFunc(res[:], func(a, b searchResult) int { return -cmp.Compare(a.Score, b.Score) }) + expiry := time.Now().Add(1 * time.Minute) + entry = searchCacheEntry{ + query: search, + results: res, + expiry: expiry, + } + index.search[search] = entry + + return len(res), res[i:min(i+limit, len(entry.results))], nil +} +func (s *searchCache) clean() { + maps.DeleteFunc(*s, func(_ string, v searchCacheEntry) bool { + return v.expiry.Before(time.Now()) + }) +} +func indexsum(in [][]int) int { + sum := 0 + for i := 0; i < len(in); i++ { + sum += in[i][1] - in[i][0] + } + return sum +} diff --git a/cmd/pkgserver/ui.go b/cmd/pkgserver/ui.go new file mode 100644 index 00000000..e4c42832 --- /dev/null +++ b/cmd/pkgserver/ui.go @@ -0,0 +1,38 @@ +package main + +import "net/http" + +func serveWebUI(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + w.Header().Set("Pragma", "no-cache") + w.Header().Set("Expires", "0") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("X-XSS-Protection", "1") + w.Header().Set("X-Frame-Options", "DENY") + + http.ServeFileFS(w, r, content, "ui/index.html") +} +func serveStaticContent(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/static/style.css": + darkTheme := r.CookiesNamed("dark_theme") + if len(darkTheme) > 0 && darkTheme[0].Value == "true" { + http.ServeFileFS(w, r, content, "ui/static/dark.css") + } else { + http.ServeFileFS(w, r, content, "ui/static/light.css") + } + case "/favicon.ico": + http.ServeFileFS(w, r, content, "ui/static/favicon.ico") + case "/static/index.js": + http.ServeFileFS(w, r, content, "ui/static/index.js") + default: + http.NotFound(w, r) + + } +} + +func uiRoutes(mux *http.ServeMux) { + mux.HandleFunc("GET /{$}", serveWebUI) + mux.HandleFunc("GET /favicon.ico", serveStaticContent) + mux.HandleFunc("GET /static/", serveStaticContent) +} diff --git a/cmd/pkgserver/ui/index.html b/cmd/pkgserver/ui/index.html new file mode 100644 index 00000000..ec21a5f4 --- /dev/null +++ b/cmd/pkgserver/ui/index.html @@ -0,0 +1,35 @@ + + +
+ + +| Loading... |
Showing entries .
+« Previous 1 Next » + + + + + \ No newline at end of file diff --git a/cmd/pkgserver/ui/static/_common.scss b/cmd/pkgserver/ui/static/_common.scss new file mode 100644 index 00000000..e69de29b diff --git a/cmd/pkgserver/ui/static/dark.scss b/cmd/pkgserver/ui/static/dark.scss new file mode 100644 index 00000000..8d7ea847 --- /dev/null +++ b/cmd/pkgserver/ui/static/dark.scss @@ -0,0 +1,6 @@ +@use 'common'; + +html { + background-color: #2c2c2c; + color: ghostwhite; +} \ No newline at end of file diff --git a/cmd/pkgserver/ui/static/favicon.ico b/cmd/pkgserver/ui/static/favicon.ico new file mode 100644 index 00000000..2fefdfd7 Binary files /dev/null and b/cmd/pkgserver/ui/static/favicon.ico differ diff --git a/cmd/pkgserver/ui/static/index.ts b/cmd/pkgserver/ui/static/index.ts new file mode 100644 index 00000000..ab1a8315 --- /dev/null +++ b/cmd/pkgserver/ui/static/index.ts @@ -0,0 +1,155 @@ +class PackageIndexEntry { + name: string + size: number | null + description: string | null + website: string | null + version: string | null + report: boolean +} +function toHTML(entry: PackageIndexEntry): HTMLTableRowElement { + let v = entry.version != null ? `${escapeHtml(entry.version)}` : "" + let s = entry.size != null ? `Size: ${toByteSizeString(entry.size)} (${entry.size})
` : "" + let d = entry.description != null ? `${escapeHtml(entry.description)}
` : "" + let w = entry.website != null ? `Website` : "" + let r = entry.report ? `Log (View | Download)` : "" + let row =