Skip to content
Draft
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
6 changes: 6 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"t:gateway":
- changed-files:
- any-glob-to-any-file: [ 'gateway/*' ]
"t:http interaction":
- changed-files:
- any-glob-to-any-file: [ 'httpinteraction/*' ]
"t:webhook event":
- changed-files:
- any-glob-to-any-file: [ 'webhookevent/*' ]
"t:handler":
- changed-files:
- any-glob-to-any-file: [ 'handler/*' ]
Expand Down
12 changes: 6 additions & 6 deletions _examples/application_commands/http/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/httpserver"
"github.com/disgoorg/disgo/httpinteraction"
)

var (
Expand Down Expand Up @@ -44,7 +44,7 @@ var (

type customVerifier struct{}

func (customVerifier) Verify(publicKey httpserver.PublicKey, message, sig []byte) bool {
func (customVerifier) Verify(publicKey httpinteraction.PublicKey, message, sig []byte) bool {
return ed25519.Verify(publicKey, message, sig)
}

Expand All @@ -57,11 +57,11 @@ func main() {
slog.Info("disgo version", slog.String("version", disgo.Version))

client, err := disgo.New(token,
bot.WithHTTPServerConfigOpts(publicKey,
httpserver.WithURL("/interactions/callback"),
httpserver.WithAddress(":80"),
bot.WithAddress(":80"),
bot.WithHTTPInteractionConfigOpts(publicKey,
httpinteraction.WithEndpoint("/interactions/callback"),
// use custom ed25519 verify implementation
httpserver.WithVerifier(customVerifier{}),
httpinteraction.WithVerifier(customVerifier{}),
),
bot.WithEventListenerFunc(commandListener),
)
Expand Down
13 changes: 9 additions & 4 deletions bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package bot

import (
"context"
"errors"
"log/slog"
"net/http"

"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/httpserver"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgo/sharding"
"github.com/disgoorg/disgo/voice"
Expand All @@ -26,7 +27,7 @@ type Client struct {
EventManager EventManager
ShardManager sharding.ShardManager
Gateway gateway.Gateway
HTTPServer httpserver.Server
HTTPServer *http.Server
VoiceManager voice.Manager
Caches cache.Caches
MemberChunkingManager MemberChunkingManager
Expand All @@ -46,7 +47,7 @@ func (c *Client) Close(ctx context.Context) {
c.ShardManager.Close(ctx)
}
if c.HTTPServer != nil {
c.HTTPServer.Close(ctx)
_ = c.HTTPServer.Close()
}
}

Expand Down Expand Up @@ -172,7 +173,11 @@ func (c *Client) OpenHTTPServer() error {
if c.HTTPServer == nil {
return discord.ErrNoHTTPServer
}
c.HTTPServer.Start()
go func() {
if err := c.HTTPServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
c.Logger.Error("HTTP server failed to run", slog.Any("err", err))
}
}()
return nil
}

Expand Down
125 changes: 98 additions & 27 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ package bot
import (
"fmt"
"log/slog"
"net/http"

"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/httpserver"
"github.com/disgoorg/disgo/httpinteraction"
"github.com/disgoorg/disgo/internal/tokenhelper"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgo/sharding"
"github.com/disgoorg/disgo/voice"
"github.com/disgoorg/disgo/webhookevent"
)

func defaultConfig(gatewayHandlers map[gateway.EventType]GatewayEventHandler, httpHandler HTTPServerEventHandler) config {
func defaultConfig(gatewayHandler GatewayEventHandler, httpInteractionHandler HTTPInteractionEventHandler, httpGatewayHandler HTTPGatewayEventHandler) config {
return config{
Logger: slog.Default(),
EventManagerConfigOpts: []EventManagerConfigOpt{WithGatewayHandlers(gatewayHandlers), WithHTTPServerHandler(httpHandler)},
MemberChunkingFilter: MemberChunkingFilterNone,
Logger: slog.Default(),
EventManagerConfigOpts: []EventManagerConfigOpt{
WithGatewayHandlers(gatewayHandler),
WithHTTPServerHandler(httpInteractionHandler),
WithHTTPGatewayHandler(httpGatewayHandler),
},
MemberChunkingFilter: MemberChunkingFilterNone,
HTTPServer: &http.Server{},
ServeMux: http.NewServeMux(),
Address: ":80",
}
}

Expand All @@ -41,9 +50,14 @@ type config struct {
ShardManager sharding.ShardManager
ShardManagerConfigOpts []sharding.ConfigOpt

HTTPServer httpserver.Server
PublicKey string
HTTPServerConfigOpts []httpserver.ConfigOpt
HTTPServer *http.Server
ServeMux *http.ServeMux
Address string
PublicKey string
CertFile string
KeyFile string
HTTPInteractionConfigOpts []httpinteraction.ConfigOpt
WebhookEventConfigOpts []webhookevent.ConfigOpt

Caches cache.Caches
CacheConfigOpts []cache.ConfigOpt
Expand Down Expand Up @@ -163,18 +177,58 @@ func WithShardManagerConfigOpts(opts ...sharding.ConfigOpt) ConfigOpt {
}
}

// WithHTTPServer lets you inject your own httpserver.Server.
func WithHTTPServer(httpServer httpserver.Server) ConfigOpt {
func WithHTTPServer(httpServer *http.Server) ConfigOpt {
return func(config *config) {
config.HTTPServer = httpServer
}
}

// WithHTTPServerConfigOpts lets you configure the default httpserver.Server.
func WithHTTPServerConfigOpts(publicKey string, opts ...httpserver.ConfigOpt) ConfigOpt {
func WithServeMux(serveMux *http.ServeMux) ConfigOpt {
return func(config *config) {
config.ServeMux = serveMux
}
}

// WithAddress sets the address the HTTP server will listen on.
func WithAddress(address string) ConfigOpt {
return func(config *config) {
config.Address = address
}
}

// WithCert lets you set the certificate and key files for the HTTP server.
func WithCert(certFile string, keyFile string) ConfigOpt {
return func(config *config) {
config.CertFile = certFile
config.KeyFile = keyFile
}
}

func WithHTTPInteractionConfigOpts(publicKey string, opts ...httpinteraction.ConfigOpt) ConfigOpt {
return func(config *config) {
config.PublicKey = publicKey
config.HTTPInteractionConfigOpts = append(config.HTTPInteractionConfigOpts, opts...)
}
}

func WithDefaultHTTPInteractions(publicKey string) ConfigOpt {
return func(config *config) {
config.PublicKey = publicKey
config.HTTPServerConfigOpts = append(config.HTTPServerConfigOpts, opts...)
config.HTTPInteractionConfigOpts = append(config.HTTPInteractionConfigOpts, httpinteraction.WithDefault())
}
}

func WithWebhookEventConfigOpts(publicKey string, opts ...webhookevent.ConfigOpt) ConfigOpt {
return func(config *config) {
config.PublicKey = publicKey
config.WebhookEventConfigOpts = append(config.WebhookEventConfigOpts, opts...)
}
}

func WithDefaultWebhookEvents(publicKey string) ConfigOpt {
return func(config *config) {
config.PublicKey = publicKey
config.WebhookEventConfigOpts = append(config.WebhookEventConfigOpts, webhookevent.WithDefault())
}
}

Expand Down Expand Up @@ -206,20 +260,25 @@ func WithMemberChunkingFilter(memberChunkingFilter MemberChunkingFilter) ConfigO
}
}

func defaultHTTPServerEventHandlerFunc(client *Client) httpserver.EventHandlerFunc {
return client.EventManager.HandleHTTPEvent
}

func defaultGatewayEventHandlerFunc(client *Client) gateway.EventHandlerFunc {
return client.EventManager.HandleGatewayEvent
}

func defaultInteractionHandlerFunc(client *Client) httpinteraction.InteractionHandlerFunc {
return client.EventManager.HandleHTTPInteractionEvent
}

func defaultHTTPGatewayEventHandlerFunc(client *Client) webhookevent.EventHandlerFunc {
return client.EventManager.HandleHTTPGatewayEvent
}

// BuildClient creates a new Client instance with the given Token, config, Gateway handlers, http handlers os, name, github & version.
func BuildClient(
token string,
otps []ConfigOpt,
gatewayHandlers map[gateway.EventType]GatewayEventHandler,
httpHandler HTTPServerEventHandler,
gatewayHandler GatewayEventHandler,
httpInteractionHandler HTTPInteractionEventHandler,
httpGatewayHandler HTTPGatewayEventHandler,
os string,
name string,
github string,
Expand All @@ -233,7 +292,7 @@ func BuildClient(
return nil, fmt.Errorf("error while getting application id from Token: %w", err)
}

cfg := defaultConfig(gatewayHandlers, httpHandler)
cfg := defaultConfig(gatewayHandler, httpInteractionHandler, httpGatewayHandler)
cfg.apply(otps)

client := &Client{
Expand Down Expand Up @@ -325,15 +384,27 @@ func BuildClient(
}
client.ShardManager = cfg.ShardManager

if cfg.HTTPServer == nil && cfg.PublicKey != "" {
cfg.HTTPServerConfigOpts = append([]httpserver.ConfigOpt{
httpserver.WithLogger(cfg.Logger),
}, cfg.HTTPServerConfigOpts...)
if len(cfg.HTTPInteractionConfigOpts) > 0 || len(cfg.WebhookEventConfigOpts) > 0 {
cfg.HTTPInteractionConfigOpts = append([]httpinteraction.ConfigOpt{
httpinteraction.WithLogger(cfg.Logger),
}, cfg.HTTPInteractionConfigOpts...)

cfg.HTTPServer, err = httpserver.New(cfg.PublicKey, defaultHTTPServerEventHandlerFunc(client), cfg.HTTPServerConfigOpts...)
if err != nil {
return nil, fmt.Errorf("error while creating http server: %w", err)
if err = httpinteraction.New(cfg.ServeMux, cfg.PublicKey, defaultInteractionHandlerFunc(client), cfg.HTTPInteractionConfigOpts...); err != nil {
return nil, fmt.Errorf("error while initializing http interaction handler: %w", err)
}

cfg.WebhookEventConfigOpts = append([]webhookevent.ConfigOpt{
webhookevent.WithLogger(cfg.Logger),
}, cfg.WebhookEventConfigOpts...)

if err = webhookevent.New(cfg.ServeMux, cfg.PublicKey, defaultHTTPGatewayEventHandlerFunc(client), cfg.WebhookEventConfigOpts...); err != nil {
return nil, fmt.Errorf("error while initializing webhook event handler: %w", err)
}

if cfg.HTTPServer.Addr == "" {
cfg.HTTPServer.Addr = cfg.Address
}
cfg.HTTPServer.Handler = cfg.ServeMux
}
client.HTTPServer = cfg.HTTPServer

Expand Down
Loading
Loading