-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
61 lines (52 loc) · 1.22 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"encoding/hex"
"github.com/disgoorg/disgo/httpserver"
"github.com/labstack/echo/v4"
"log/slog"
)
type Server struct {
*echo.Echo
EventHandlerFunc httpserver.EventHandlerFunc
publicKey string
}
var _ httpserver.Server = (*Server)(nil)
func NewServer(publicKey string, eventHandlerFunc httpserver.EventHandlerFunc) Server {
return Server{
Echo: echo.New(),
EventHandlerFunc: eventHandlerFunc,
publicKey: publicKey,
}
}
func (s *Server) Start() {
hexKey, err := hex.DecodeString(s.publicKey)
if err != nil {
slog.Error("failed to decode public key",
"error", err)
panic(err)
}
handlerFunc := httpserver.HandleInteraction(
hexKey,
slog.Default(),
s.EventHandlerFunc,
)
s.POST("/interactions", func(ctx echo.Context) error {
handlerFunc(ctx.Response().Writer, ctx.Request())
return nil
})
s.Logger.Fatal(s.Echo.Start(":8080"))
}
func (s *Server) Close(ctx context.Context) {
if err := s.Echo.Shutdown(ctx); err != nil {
slog.Error("failed to shutdown http server",
"error", err)
}
}
func startServer() {
r := echo.New()
r.GET("/", func(c echo.Context) error {
return c.String(200, "Hello, World!")
})
r.Logger.Fatal(r.Start(":8080"))
}