xonecas.io
8058
Ir

irvc / commit a3979ce

add proper command line flags, add a Makefile to unify build/test/fmt verbs across go+npm and make initial bootstrap simpler

Jason Moiron2026-10-12 a3979ce7a1200f38bdf73c56c614623a7ff6f694 parent afa3863

3 files changed

Makefile+16−0
@@ -0,0 +1,16 @@
1+.PHONY: all backend frontend fmt test
2+
3+all: backend frontend
4+
5+backend:
6+ go build -o irvc .
7+
8+frontend:
9+ cd web && npm install && npm run build
10+
11+fmt:
12+ goimports -w $$(find . -name '*.go' -not -path './web/node_modules/*')
13+
14+test:
15+ go test ./...
16+ cd web && npm run --if-present test
main.go+65−1
@@ -2,6 +2,7 @@ package main
22
33 import (
44 "database/sql"
5+ "flag"
56 "fmt"
67 "log"
78 "net/http"
@@ -19,12 +20,75 @@ type Server struct {
1920 hub *hub
2021 }
2122
23+// usage prints out human readable usage for fs
24+func usage(fs *flag.FlagSet) func() {
25+ return func() {
26+ w := fs.Output()
27+ fmt.Fprint(w, `irvc - self-hosted WebRTC text and voice chat
28+
29+Usage:
30+ irvc [flags] run the server
31+ irvc [flags] invite create an invite and print its token
32+
33+Flags:
34+`)
35+ printFlags(fs)
36+ fmt.Fprint(w, `
37+Secrets are read from the environment only:
38+ IRVC_SESSION_SECRET signs sessions (required to run the server)
39+ IRVC_LIVEKIT_API_KEY LiveKit API key
40+ IRVC_LIVEKIT_SECRET LiveKit API secret
41+
42+See README.md for details.
43+`)
44+ }
45+}
46+
47+// printFlags prints defaults like flag.PrintDefaults, but with GNU-style "--long" names
48+func printFlags(fs *flag.FlagSet) {
49+ fs.VisitAll(func(f *flag.Flag) {
50+ name, usage := flag.UnquoteUsage(f)
51+ head := " --" + f.Name
52+ if name != "" {
53+ head += " " + name
54+ }
55+ if f.DefValue != "" && f.DefValue != "0" {
56+ usage += fmt.Sprintf(" (default: %s)", f.DefValue)
57+ }
58+ fmt.Fprintf(fs.Output(), "%s\n \t%s\n", head, usage)
59+ })
60+}
61+
2262 func main() {
2363 cfg := LoadConfig()
64+
65+ // flags parsed before db/secret check so that --help always works
66+ fs := flag.NewFlagSet("irvc", flag.ExitOnError)
67+ fs.Usage = usage(fs)
68+ fs.StringVar(&cfg.ListenAddr, "listen", cfg.ListenAddr, "IRVC_LISTEN_ADDR, address to listen on")
69+ fs.StringVar(&cfg.DBPath, "db", cfg.DBPath, "IRVC_DB_PATH, path to the SQLite database")
70+ fs.StringVar(&cfg.MediaDir, "media-dir", cfg.MediaDir, "IRVC_MEDIA_DIR, directory for uploaded media")
71+ fs.Int64Var(&cfg.MaxUpload, "max-upload", cfg.MaxUpload, "IRVC_MAX_UPLOAD, max upload size in bytes")
72+ fs.StringVar(&cfg.LiveKitURL, "livekit-url", cfg.LiveKitURL, "IRVC_LIVEKIT_URL, LiveKit server URL for voice chat")
73+ genSecret := fs.Bool("gen-secret", false, "generate a random session secret, print it and exit")
74+ fs.Parse(os.Args[1:])
75+
76+ if *genSecret {
77+ fmt.Println(randToken())
78+ return
79+ }
80+
81+ cmd := fs.Arg(0)
82+ if cmd != "" && cmd != "invite" {
83+ fmt.Fprintf(fs.Output(), "unknown command %q\n\n", cmd)
84+ fs.Usage()
85+ os.Exit(2)
86+ }
87+
2488 db := OpenDB(cfg.DBPath)
2589 defer db.Close()
2690
27- if len(os.Args) > 1 && os.Args[1] == "invite" {
91+ if cmd == "invite" {
2892 makeInvite(db)
2993 return
3094 }
main_test.go+24−0
@@ -0,0 +1,24 @@
1+package main
2+
3+import (
4+ "encoding/base64"
5+ "testing"
6+)
7+
8+// randToken backs --gen-secret, so it must never repeat and must carry
9+// the full 32 bytes of entropy.
10+func TestRandTokenUnique(t *testing.T) {
11+ seen := map[string]bool{}
12+ for i := 0; i < 1000; i++ {
13+ tok := randToken()
14+ if seen[tok] {
15+ t.Fatalf("duplicate token after %d calls: %s", i, tok)
16+ }
17+ seen[tok] = true
18+
19+ raw, err := base64.RawURLEncoding.DecodeString(tok)
20+ if err != nil || len(raw) != 32 {
21+ t.Fatalf("token %q: want 32 decodable bytes, got %d (err %v)", tok, len(raw), err)
22+ }
23+ }
24+}