diff --git a/go.mod b/go.mod index 29dade4a1b62b503877e60605938b9af5c836400..9ceb960dbd3a233719d073f05e9ab2fab9bfb205 100644 --- a/go.mod +++ b/go.mod @@ -1,24 +1,25 @@ module antragsraum-proxy -go 1.23.0 + go 1.23.0 -require ( - github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 - github.com/stretchr/testify v1.9.0 - google.golang.org/grpc v1.64.0 - gopkg.in/yaml.v3 v3.0.1 -) + require ( + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 + github.com/stretchr/testify v1.9.0 + google.golang.org/grpc v1.64.0 + gopkg.in/yaml.v3 v3.0.1 + go.elastic.co/ecslogrus master + ) -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.12.0 // indirect - golang.org/x/net v0.26.0 // indirect - golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.16.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect - google.golang.org/protobuf v1.34.2 // indirect - google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.4.0 // indirect -) + require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect + golang.org/x/net v0.26.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect + google.golang.org/protobuf v1.34.2 // indirect + google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.4.0 // indirect + ) diff --git a/internal/server/globals.go b/internal/server/globals.go index 4e832f3ef72522d0c28d434dd1724fdc93c8b0ca..adaf9bfd4a85d58389a1b80dd9f50487a1321b90 100644 --- a/internal/server/globals.go +++ b/internal/server/globals.go @@ -11,4 +11,5 @@ const ( ) var conf = config.LoadConfig() -var logger = logging.GetLogger() +var log := logrus.New() +log.SetFormatter(&ecslogrus.Formatter{}) diff --git a/internal/server/grpc_router.go b/internal/server/grpc_router.go index 3a1cb4007616e3223a41e1c623cd365b4e861907..61bf2a48024d247cb2e9a8be3d4e3008e5b51e61 100644 --- a/internal/server/grpc_router.go +++ b/internal/server/grpc_router.go @@ -94,7 +94,7 @@ func createRueckfrageClient(grpcAddress string) (pb.AntragraumServiceClient, fun target := GetGrpcServerUrl(grpcAddress, conf) conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { - logger.Error("rueckfrage router failed to route: %v", err) + log.WithError(err).Error("rueckfrage router failed to route: %v", err) return nil, nil, errors.New("rueckfrage router failed to route") } @@ -126,7 +126,7 @@ func createCommandClient(grpcAddress string) (pb.CommandServiceClient, func() er target := GetGrpcServerUrl(grpcAddress, conf) conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { - logger.Error("command router failed to route: %v", err) + log.WithError(err).Error("command router failed to route: %v", err) return nil, nil, errors.New("command router failed to route") } @@ -169,12 +169,12 @@ func StartGrpcRouter() *grpc.Server { lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.Grpc.Router.Port)) if err != nil { - logger.Fatal("gRPC router failed to listen: %v", err) + log.WithError(err).Error("gRPC router failed to listen: %v", err) } - logger.Info("gRPC router listening on port %d", conf.Grpc.Router.Port) + log.Info("gRPC router listening on port %d", conf.Grpc.Router.Port) if err = s.Serve(lis); err != nil { - logger.Fatal("gRPC router failed to serve: %v", err) + log..WithError(err).Error("gRPC router failed to serve: %v", err) } return s diff --git a/internal/server/handler.go b/internal/server/handler.go index dcceaf6bb95cd47ec2abfa43f57c8c1d5704d2dd..30d14d2b5e5b472220c5cb097614f783b93390b7 100644 --- a/internal/server/handler.go +++ b/internal/server/handler.go @@ -49,18 +49,20 @@ const ( fileChunkSizeInByte = 1024 * 1024 ) + + func RegisterHomeEndpoint(mux *runtime.ServeMux) { err := mux.HandlePath("GET", "/", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { defer func() { if err := recover(); err != nil { - logger.Error("failed to recover: %v", err) + log.WithError(err).Error("failed to recover: %v", err) http.Error(w, fmt.Sprintf("failed to recover: %v", err), http.StatusInternalServerError) } }() }) if err != nil { - logger.Fatal("failed to register home endpoint: %v", err) + log.WithError(err).Error("failed to register home endpoint: %v", err) } } @@ -68,7 +70,7 @@ func RegisterAntragraumEndpoints(ctx context.Context, mux *runtime.ServeMux, grp opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} err := pb.RegisterAntragraumServiceHandlerFromEndpoint(ctx, mux, grpcUrl, opts) if err != nil { - logger.Fatal("failed to register antragraum endpoints: %v", err) + log.WithError(err).Error("failed to register antragraum endpoint: %v", err) } } @@ -76,7 +78,8 @@ func RegisterCommandEndpoints(ctx context.Context, mux *runtime.ServeMux, grpcUr opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} err := pb.RegisterCommandServiceHandlerFromEndpoint(ctx, mux, grpcUrl, opts) if err != nil { - logger.Fatal("failed to register command endpoints: %v", err) + log.WithError(err).Error("failed to register command endpoint: %v", err) + } } @@ -84,7 +87,7 @@ func RegisterUploadAttachmentEndpoint(mux *runtime.ServeMux) { err := mux.HandlePath("POST", "/api/v1/file", attachmentUploadHandler) if err != nil { - logger.Fatal("failed to register file upload endpoint: %v", err) + log.WithError(err).Error("failed to register upload endpoint: %v", err) } } @@ -190,7 +193,8 @@ func RegisterGetAttachmentContentEndpoint(mux *runtime.ServeMux) { err := mux.HandlePath("POST", "/api/v1/file/content", getAttachmentContentHandler) if err != nil { - logger.Fatal("failed to register get file endpoint: %v", err) + log.WithError(err).Error("failed to register get file endpoint: %v", err) + } } @@ -288,6 +292,6 @@ func ErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime. _, writeErr := w.Write([]byte(st.Message())) if writeErr != nil { - logger.Fatal("failed to handle grpc error: %v", writeErr) + log.WithError(err).Error("failed to handle grpc error: %v", writeErr) } } diff --git a/internal/server/http_gateway.go b/internal/server/http_gateway.go index 22d600e5d9a91ff4fb9caa6349a08be6ee1d9236..4cbf459c98da3a380c3919e369c6f662217f8823 100644 --- a/internal/server/http_gateway.go +++ b/internal/server/http_gateway.go @@ -64,7 +64,7 @@ func StartHttpGateway() *http.Server { logger.Info("HTTP gateway listening on port %d", conf.Http.Server.Port) if err := httpServer.ListenAndServe(); err != nil { - logger.Fatal("HTTP gateway failed to serve: %v", err) + log.WithError(err).Error("HTTP gateway failed to serve: %v", err) } return httpServer diff --git a/internal/server/middleware.go b/internal/server/middleware.go index 41710750de6006fed161507fd8ba65846ef1601b..e6e7ea876a6ea733e7949f28f094be2607e73252 100644 --- a/internal/server/middleware.go +++ b/internal/server/middleware.go @@ -53,21 +53,21 @@ func RequestLoggingMiddleware(h http.Handler) http.Handler { body, err := io.ReadAll(r.Body) if err != nil { - logger.Error("failed to read %v request body for %v: %v", r.Method, r.URL.Path, err) + log..WithError(err).Error("failed to read %v request body for %v: %v", r.Method, r.URL.Path, err) http.Error(w, fmt.Sprintf("failed to read request body: %v", err), http.StatusBadRequest) return } r.Body = io.NopCloser(bytes.NewReader(body)) - logger.Debug("received %v request for %v with body: %v", r.Method, r.URL.Path, string(body)) + log.Debug("received %v request for %v with body: %v", r.Method, r.URL.Path, string(body)) lw := &logResponseWriter{w, http.StatusOK} h.ServeHTTP(lw, r) if lw.statusCode == http.StatusOK { - logger.Debug("successfully handled %v request for %v with body: %v", r.Method, r.URL.Path, string(body)) + log.Debug("successfully handled %v request for %v with body: %v", r.Method, r.URL.Path, string(body)) } else { - logger.Error("failed handling %v request for %v with body: %v, status code: %d", r.Method, r.URL.Path, string(body), lw.statusCode) + log.Error("failed handling %v request for %v with body: %v, status code: %d", r.Method, r.URL.Path, string(body), lw.statusCode) } }) }