Skip to content

Commit cf0d849

Browse files
high-moctaneclaude
andcommitted
feat: Add welcome message for non-WebSocket GET requests
Instead of returning WebSocket upgrade error for plain GET requests, now returns RelayInfo.Name (if set) or empty 200 OK. Users who want custom messages can wrap Relay with their own ServeMux. 🌸 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Sakura 🌸 <noreply@anthropic.com>
1 parent efa659d commit cf0d849

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

relay.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,29 @@ func (r *Relay) unregisterConn(id uint64) {
114114

115115
// ServeHTTP implements http.Handler.
116116
func (r *Relay) ServeHTTP(w http.ResponseWriter, req *http.Request) {
117+
// Check if relay is shutting down (for all requests)
118+
r.mu.Lock()
119+
closed := r.closed
120+
r.mu.Unlock()
121+
122+
if closed {
123+
http.Error(w, "relay is shutting down", http.StatusServiceUnavailable)
124+
return
125+
}
126+
117127
// NIP-11: Respond with relay info if Accept header is application/nostr+json
118128
if r.Info != nil && req.Header.Get("Accept") == "application/nostr+json" {
119129
r.serveNIP11(w, req)
120130
return
121131
}
122132

123-
// Check if relay is shutting down and register connection atomically
133+
// Non-WebSocket GET: return simple message instead of upgrade error
134+
if req.Header.Get("Upgrade") == "" {
135+
r.serveWelcome(w, req)
136+
return
137+
}
138+
139+
// WebSocket connection: track with WaitGroup
124140
r.mu.Lock()
125141
if r.closed {
126142
r.mu.Unlock()
@@ -384,6 +400,18 @@ func (r *Relay) logWarn(ctx context.Context, msg string, args ...any) {
384400
}
385401
}
386402

403+
// serveWelcome responds with a simple welcome message for non-WebSocket requests.
404+
// If RelayInfo.Name is set, it returns the name. Otherwise, returns empty 200 OK.
405+
func (r *Relay) serveWelcome(w http.ResponseWriter, req *http.Request) {
406+
if r.Info != nil && r.Info.Name != "" {
407+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
408+
w.Write([]byte(r.Info.Name))
409+
return
410+
}
411+
// Empty 200 OK
412+
w.WriteHeader(http.StatusOK)
413+
}
414+
387415
// serveNIP11 responds with the NIP-11 Relay Information Document.
388416
func (r *Relay) serveNIP11(w http.ResponseWriter, req *http.Request) {
389417
// CORS headers (required by NIP-11)

0 commit comments

Comments
 (0)