-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (87 loc) · 3.12 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
//go:generate npx buf generate
import (
"cards/gen/proto/biome/biomeconnect"
"cards/gen/proto/card/cardconnect"
"cards/gen/proto/user/userconnect"
"cards/pkg/biome"
"cards/pkg/card"
"cards/pkg/database"
"cards/pkg/user"
"context"
"fmt"
"log/slog"
"net/http"
"github.com/bufbuild/connect-go"
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
func NewLogInterceptor() connect.UnaryInterceptorFunc {
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
return func(
ctx context.Context,
req connect.AnyRequest,
) (connect.AnyResponse, error) {
resp, err := next(ctx, req)
if err != nil {
// slog.Error("connect error", "error", fmt.Sprintf("%+v", err))
// TODO breadchris this should only be done for local dev
fmt.Printf("%+v\n", err)
}
return resp, err
}
}
return interceptor
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Debug("request", "method", r.Method, "path", r.URL.Path)
next.ServeHTTP(w, r)
})
}
func main() {
interceptors := connect.WithInterceptors(NewLogInterceptor())
apiRoot := http.NewServeMux()
database.InitDB()
cardService := &card.CardService{}
biomeService := &biome.BiomeService{}
userService := &user.UserService{}
imageServer := http.FileServer(http.Dir("./pkg/card_images"))
apiRoot.Handle("/card_images/", http.StripPrefix("/card_images/", imageServer))
apiRoot.Handle(cardconnect.NewCardServiceHandler(cardService, interceptors))
apiRoot.Handle(biomeconnect.NewBiomeServiceHandler(biomeService, interceptors))
apiRoot.Handle(userconnect.NewUserServiceHandler(userService, interceptors))
reflector := grpcreflect.NewStaticReflector(
"card.CardService",
)
recoverCall := func(_ context.Context, spec connect.Spec, _ http.Header, p any) error {
slog.Error("panic", "err", fmt.Sprintf("%+v", p))
if err, ok := p.(error); ok {
return err
}
return fmt.Errorf("panic: %v", p)
}
apiRoot.Handle(grpcreflect.NewHandlerV1(reflector, connect.WithRecover(recoverCall)))
// Many tools still expect the older version of the server reflection Service, so
// most servers should mount both handlers.
apiRoot.Handle(grpcreflect.NewHandlerV1Alpha(reflector, connect.WithRecover(recoverCall)))
addr := fmt.Sprintf(":%d", 8080)
slog.Info("starting http server", "addr", addr)
http.ListenAndServe(addr, h2c.NewHandler(corsMiddleware(apiRoot), &http2.Server{}))
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO breadchris how bad is this? lol
origin := r.Header.Get("Origin")
// TODO breadchris this should only be done for local dev
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Authorization, connect-protocol-version")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}