-
Notifications
You must be signed in to change notification settings - Fork 3
/
mux.go
94 lines (79 loc) · 2.24 KB
/
mux.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package corde
import (
"context"
"fmt"
"log"
"net/http"
"path"
"strings"
"sync"
"time"
"github.com/Karitham/corde/internal/rest"
"github.com/akrennmair/go-radix"
)
// Mux is a discord gateway muxer, which handles the routing
type Mux struct {
rMu *sync.RWMutex
routes *radix.Tree[Handlers]
PublicKey string // the hex public key provided by discord
BasePath string // base route path, default is "/"
OnNotFound func(context.Context, ResponseWriter, *Interaction[JsonRaw])
Client *http.Client
AppID Snowflake
BotToken string
handler http.Handler
}
// Lock the mux, to be able to mount or unmount routes
func (m *Mux) Lock() {
m.rMu.Lock()
}
// Unlock the mux, so it can route again
func (m *Mux) Unlock() {
m.rMu.Unlock()
}
// NewMux returns a new mux for routing slash commands
//
// When you mount a command on the mux, it's prefix based routed,
// which means you can route to a button like `/list/next/456132153` having mounted `/list/next`
func NewMux(publicKey string, appID Snowflake, botToken string) *Mux {
m := &Mux{
rMu: &sync.RWMutex{},
routes: radix.New[Handlers](),
PublicKey: publicKey,
BasePath: "/",
OnNotFound: func(_ context.Context, _ ResponseWriter, i *Interaction[JsonRaw]) {
log.Printf("No handler for registered command: %s\n", i.Route)
},
Client: &http.Client{
Timeout: 10 * time.Second,
},
AppID: appID,
BotToken: botToken,
}
m.handler = rest.Verify(publicKey)(http.HandlerFunc(m.route))
return m
}
// Handlers handles incoming requests
type Handlers map[InnerInteractionType]any
// Route routes common parts along a pattern
func (m *Mux) Route(pattern string, fn func(m *Mux)) {
if fn == nil {
panic(fmt.Sprintf("corde: attempting to Route() a nil subrouter on %q", pattern))
}
r := NewMux(m.PublicKey, m.AppID, m.BotToken)
fn(r)
pattern = strings.TrimLeft(pattern, "/")
for route, handler := range r.routes.ToMap() {
m.routes.Insert(path.Join(pattern, route), handler)
}
}
// Mount is for mounting a Handler on the Mux
func (m *Mux) Mount(typ InnerInteractionType, route string, handler any) {
m.rMu.Lock()
defer m.rMu.Unlock()
if r, ok := m.routes.Get(route); ok {
(*r)[typ] = handler
return
}
m.routes.Insert(route, &Handlers{typ: handler})
}