-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
45 lines (39 loc) · 918 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package mocrelay
import (
"context"
"io"
"log/slog"
"net/http"
)
type ServeMux struct {
Relay *Relay
NIP11 *NIP11
Default http.Handler
Logger *slog.Logger
}
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Upgrade") != "" {
mux.logInfo(r.Context(), "got relay access")
mux.Relay.ServeHTTP(w, r)
} else if r.Header.Get("Accept") == "application/nostr+json" {
mux.logInfo(r.Context(), "got nip11 access")
if mux.NIP11 == nil {
io.WriteString(w, "{}")
} else {
mux.NIP11.ServeHTTP(w, r)
}
} else {
mux.logInfo(r.Context(), "got default access")
if mux.Default == nil {
io.WriteString(w, "Hello Mocrelay (`・ω・´)!")
} else {
mux.Default.ServeHTTP(w, r)
}
}
}
func (mux *ServeMux) logInfo(ctx context.Context, msg string, args ...any) {
if mux.Logger == nil {
return
}
mux.Logger.InfoContext(ctx, msg, args...)
}