summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..85a4a54
--- /dev/null
+++ b/main.go
@@ -0,0 +1,42 @@
+// main.go
+package main
+
+import (
+ "log"
+ "net/http"
+ "os"
+)
+
+func main() {
+ tokensPath := "tokens.json"
+ if v := os.Getenv("TOKENS_PATH"); v != "" {
+ tokensPath = v
+ }
+
+ tokens, err := loadTokens(tokensPath)
+ if err != nil {
+ log.Fatalf("failed to load tokens from %s: %v", tokensPath, err)
+ }
+ log.Printf("loaded %d tokens", len(tokens))
+
+ hub := NewHub(tokens)
+ adminAuth := newAdminAuthFromEnv()
+
+ http.HandleFunc("/ws", wsHandler(hub))
+
+ // admin routes (basic auth)
+ http.Handle("/admin", adminAuth.requireAdmin(http.HandlerFunc(adminPageHandler)))
+ http.Handle("/admin/", adminAuth.requireAdmin(http.HandlerFunc(adminPageHandler)))
+ http.Handle("/admin/api/tokens", adminAuth.requireAdmin(http.HandlerFunc(adminTokensHandler(hub, tokensPath))))
+ http.Handle("/admin/api/tokens/revoke", adminAuth.requireAdmin(http.HandlerFunc(adminTokenRevokeHandler(hub, tokensPath))))
+ http.Handle("/admin/api/events", adminAuth.requireAdmin(http.HandlerFunc(adminEventsHandler(hub))))
+
+ addr := ":8080"
+ if v := os.Getenv("BRIDGE_ADDR"); v != "" {
+ addr = v
+ }
+ log.Printf("bridge server listening on %s", addr)
+ if err := http.ListenAndServe(addr, nil); err != nil {
+ log.Fatalf("ListenAndServe: %v", err)
+ }
+} \ No newline at end of file