From 7223ebaa7aa55664df4f2c9f477841f1d22aff4c Mon Sep 17 00:00:00 2001 From: Azuki-bar <42642269+Azuki-bar@users.noreply.github.com> Date: Fri, 10 Nov 2023 00:34:46 +0900 Subject: [PATCH] =?UTF-8?q?http=20router=E3=82=92go-chi=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=A6pprof=E3=82=92=E5=90=90=E3=81=8F=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../state-manager/pkg/connect/connect_handler.go | 16 +++++++++++----- go.mod | 1 + go.sum | 2 ++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/state-manager/pkg/connect/connect_handler.go b/backend/state-manager/pkg/connect/connect_handler.go index c0ed5143..0bc6acd9 100644 --- a/backend/state-manager/pkg/connect/connect_handler.go +++ b/backend/state-manager/pkg/connect/connect_handler.go @@ -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" @@ -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 }, } diff --git a/go.mod b/go.mod index f9464985..c3b1dfa7 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 71f3d93e..0f9b302e 100644 --- a/go.sum +++ b/go.sum @@ -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=