Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl ServeHTTP #26

Merged
merged 4 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Mux struct {
Client *http.Client
AppID Snowflake
BotToken string

handler http.Handler
}

// Lock the mux, to be able to mount or unmount routes
Expand Down Expand Up @@ -105,7 +107,8 @@ func (m *Mux) Route(pattern string, fn func(m *Mux)) {
// 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 {
return &Mux{

m := &Mux{
rMu: &sync.RWMutex{},
routes: routes{
command: radix.New[Handler](),
Expand All @@ -128,6 +131,9 @@ func NewMux(publicKey string, appID Snowflake, botToken string) *Mux {
AppID: appID,
BotToken: botToken,
}

m.handler = rest.Verify(publicKey)(http.HandlerFunc(m.route))
return m
}

// Handler handles incoming requests
Expand All @@ -153,17 +159,17 @@ type InteractionRequest struct {
// ListenAndServe starts the gateway listening to events
func (m *Mux) ListenAndServe(addr string) error {
r := http.NewServeMux()
r.Handle(m.BasePath, m.Handler())
r.Handle(m.BasePath, m)

return http.ListenAndServe(addr, r)
}

// Handler returns an http.Handler for the mux
func (m *Mux) Handler() http.Handler {
validator := rest.Verify(m.PublicKey)
return validator(http.HandlerFunc(m.route))
// ServeHTTP will serve HTTP requests with discord public key validation
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request){
m.handler.ServeHTTP(w, r)
}


// route handles routing the requests
func (m *Mux) route(w http.ResponseWriter, r *http.Request) {
i := &InteractionRequest{
Expand Down
9 changes: 8 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package corde

import (
"github.com/matryer/is"
"net/http/httptest"
"testing"

"github.com/matryer/is"
)

func TestRoute(t *testing.T) {
Expand Down Expand Up @@ -39,3 +41,8 @@ func TestRoute(t *testing.T) {

assert.Equal(commands, []string{"foo/bar/baz"}) // There should only be a single command on the router
}

func TestServeHTTP(t *testing.T) {
m := NewMux("", Snowflake(0), "")
httptest.NewServer(m)
}
2 changes: 1 addition & 1 deletion sample-component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestComponentInteraction(t *testing.T) {
},
}

s := httptest.NewServer(mux.Handler())
s := httptest.NewServer(mux)
err := owmock.NewWithClient(s.URL, s.Client()).PostExpect(t, SampleComponent, expect)
assert.NoErr(err)
}
Expand Down