Skip to content

Commit

Permalink
http routerをgo-chiにしてpprofを吐くようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
Azuki-bar committed Nov 9, 2023
1 parent 79f7d72 commit b6e46dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
16 changes: 11 additions & 5 deletions backend/state-manager/pkg/connect/connect_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"connectrpc.com/connect"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
statev1 "github.com/ueckoken/plarail2023/backend/spec/state/v1"
"github.com/ueckoken/plarail2023/backend/spec/state/v1/statev1connect"
db "github.com/ueckoken/plarail2023/backend/state-manager/pkg/db"
Expand Down Expand Up @@ -171,13 +173,17 @@ func (s *StateManagerServer) UpdateTrainUUID(
}

func StartHandler(ctx context.Context) error {
server := &StateManagerServer{}
mux := http.NewServeMux()
path, handler := statev1connect.NewStateManagerServiceHandler(server)
mux.Handle(path, handler)
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(middleware.Heartbeat("/debug/ping"))
// TODO: slogとかでいい感じにログを吐くハンドラを入れる

r.Mount("/debug", middleware.Profiler())
r.Handle(statev1connect.NewStateManagerServiceHandler(&StateManagerServer{}))

srv := &http.Server{
Addr: net.JoinHostPort("0.0.0.0", "8080"),
Handler: h2c.NewHandler(mux, &http2.Server{}),
Handler: h2c.NewHandler(r, &http2.Server{}),
ReadHeaderTimeout: 60 * time.Second,
BaseContext: func(net.Listener) context.Context { return ctx },
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.4
require (
connectrpc.com/connect v1.12.0
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/go-chi/chi/v5 v5.0.10
github.com/joho/godotenv v1.5.1
go.mongodb.org/mongo-driver v1.13.0
golang.org/x/net v0.18.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQ
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down

0 comments on commit b6e46dc

Please sign in to comment.