Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ static/*.db-wal

.secrets
.env

web/dist/
web/node_modules/
web/.astro/
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ WORKDIR /app
COPY . .
RUN go build -o reverse-watch main.go

# Build the Astro dashboard to web/dist. `npm ci` installs from the
# committed package-lock.json for reproducible, supply-chain-pinned deps.
FROM node:22 AS web-builder

WORKDIR /app/web
COPY web/package.json web/package-lock.json ./
RUN npm ci
COPY web/ ./
RUN npm run build

FROM gcr.io/distroless/base-debian12

WORKDIR /app
COPY --from=builder /app/reverse-watch /app/reverse-watch
COPY --from=builder /app/static/index.html /app/static/index.html
COPY --from=web-builder /app/web/dist /app/web/dist

EXPOSE 80
CMD ["./reverse-watch"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [reverse.watch](https://reverse.watch)

Community-driven open trade reversal tracking database for Steam. Participating entities can report trade reverals to the open database.
Community-driven open trade reversal tracking database for Steam. Participating entities can report trade reversals to the open database.

## Interested in Participating?

Expand Down
35 changes: 34 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package server

import (
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"

Expand All @@ -15,11 +18,33 @@ import (
"github.com/go-chi/cors"
)

// webDistDir is the directory (relative to the server's working directory)
// containing the Astro-built dashboard. It is produced by `npm run build`
// in web/ and is intentionally gitignored, so it must exist at runtime.
const webDistDir = "web/dist"

type Server struct {
r chi.Router
}

// verifyWebDist fails fast when the Astro build output is missing. web/dist is
// gitignored, so any run path that skips the frontend build (e.g. `go run`
// locally, or a deploy that doesn't run `npm run build`) would otherwise
// silently serve 404s for the dashboard. Requiring index.html turns that into
// an obvious startup error with remediation instructions.
func verifyWebDist(dir string) error {
index := filepath.Join(dir, "index.html")
if _, err := os.Stat(index); err != nil {
return fmt.Errorf("dashboard assets not found at %q: the Astro build output (%s) is gitignored and must be built before starting the server. Run `npm ci && npm run build` in web/, or use the Docker image which builds it automatically: %w", index, dir, err)
}
return nil
}

func New(cfg config.Config, factory repository.Factory) (*Server, error) {
if err := verifyWebDist(webDistDir); err != nil {
return nil, err
}

r := chi.NewRouter()

firefoxExtensionOrigin := regexp.MustCompile("^moz-extension://[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
Expand Down Expand Up @@ -60,8 +85,16 @@ func New(cfg config.Config, factory repository.Factory) (*Server, error) {

r.Use(rwmiddleware.FactoryMiddleware(factory))

// Serve the Astro-built dashboard from web/dist. `npm run build`
// emits the hashed bundles under web/dist/_astro and copies
// public/static verbatim to web/dist/static, so we hand both
// prefixes to a single FileServer and fall back to index.html for
// the root document.
fs := http.FileServer(http.Dir(webDistDir))
r.Handle("/static/*", fs)
r.Handle("/_astro/*", fs)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
http.ServeFile(w, r, filepath.Join(webDistDir, "index.html"))
})

r.Mount("/api", api.Router())
Expand Down
31 changes: 31 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package server

import (
"os"
"path/filepath"
"testing"
)

func TestVerifyWebDist(t *testing.T) {
t.Run("missing directory returns error", func(t *testing.T) {
if err := verifyWebDist(filepath.Join(t.TempDir(), "does-not-exist")); err == nil {
t.Fatal("expected error for missing web/dist directory, got nil")
}
})

t.Run("directory without index.html returns error", func(t *testing.T) {
if err := verifyWebDist(t.TempDir()); err == nil {
t.Fatal("expected error when index.html is absent, got nil")
}
})

t.Run("directory with index.html succeeds", func(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "index.html"), []byte("<html></html>"), 0o600); err != nil {
t.Fatalf("failed to write test index.html: %v", err)
}
if err := verifyWebDist(dir); err != nil {
t.Fatalf("expected no error when index.html exists, got %v", err)
}
})
}
Loading
Loading