bitvora / haven

High Availability Vault for Events on Nostr
75 stars 14 forks source link

panic when starting #18

Closed girino closed 1 month ago

girino commented 1 month ago

Since "outbox" is the default, when someone accesses "/" (the root of the relay address, example https://haven.girino.org/) it tries to create a second "outbox" and panics because the address "/static" is already registered to the mux.

haven-relay  | 2024/09/26 23:28:50 http: panic serving 192.168.41.101:45162: pattern "GET /static/" (registered at /app/main.go:257) conflicts with pattern "GET /static/" (registered at /app/main.go:257):
haven-relay  | GET /static/ matches the same requests as GET /static/
haven-relay  | goroutine 127 [running]:
haven-relay  | net/http.(*conn).serve.func1()
haven-relay  |  /usr/local/go/src/net/http/server.go:1947 +0xbe
haven-relay  | panic({0xd04ca0?, 0xc000116430?})

to temporarily solve it I added a boolean to check if outbox is already initialized. If so, return the already initialized object.

diff --git a/main.go b/main.go
index 550f7a3..bcd6aa0 100644
--- a/main.go
+++ b/main.go
@@ -67,6 +67,8 @@ func dynamicRelayHandler(w http.ResponseWriter, r *http.Request) {
        relay.ServeHTTP(w, r)
 }

+var outboxInited bool = false
+
 func makeNewRelay(relayType string) *khatru.Relay {
        switch relayType {
        case "/private":
@@ -237,6 +239,10 @@ func makeNewRelay(relayType string) *khatru.Relay {
                return inboxRelay

        default: // default to outbox
+               if outboxInited {
+                       return outboxRelay
+               }
+               outboxInited = true
                outboxRelay.StoreEvent = append(outboxRelay.StoreEvent, outboxDB.SaveEvent, func(ctx context.Context, event *nostr.Event) error {
                        go blast(event)
                        return nil
barrydeen commented 1 month ago

pull again i just fixed it