From a6f3dd91b62efd605c226c691f2414aa121b00bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Tue, 5 Apr 2022 10:24:28 +0200 Subject: [PATCH] cloudapi: split into handler and server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These "classes" are huge, let's split them into two files to make the file smaller. Signed-off-by: Ondřej Budai --- internal/cloudapi/v2/{v2.go => handler.go} | 42 ------------------ internal/cloudapi/v2/server.go | 51 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 42 deletions(-) rename internal/cloudapi/v2/{v2.go => handler.go} (97%) create mode 100644 internal/cloudapi/v2/server.go diff --git a/internal/cloudapi/v2/v2.go b/internal/cloudapi/v2/handler.go similarity index 97% rename from internal/cloudapi/v2/v2.go rename to internal/cloudapi/v2/handler.go index fa5913a21..bdbbdad4b 100644 --- a/internal/cloudapi/v2/v2.go +++ b/internal/cloudapi/v2/handler.go @@ -15,68 +15,26 @@ import ( "github.com/google/uuid" "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" "github.com/sirupsen/logrus" "github.com/osbuild/osbuild-composer/internal/auth" "github.com/osbuild/osbuild-composer/internal/blueprint" - "github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/distro" - "github.com/osbuild/osbuild-composer/internal/distroregistry" "github.com/osbuild/osbuild-composer/internal/jobqueue" osbuild "github.com/osbuild/osbuild-composer/internal/osbuild2" "github.com/osbuild/osbuild-composer/internal/ostree" - "github.com/osbuild/osbuild-composer/internal/prometheus" "github.com/osbuild/osbuild-composer/internal/rpmmd" "github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/worker" "github.com/osbuild/osbuild-composer/internal/worker/clienterrors" ) -// Server represents the state of the cloud Server -type Server struct { - workers *worker.Server - distros *distroregistry.Registry - config ServerConfig -} - -type ServerConfig struct { - AWSBucket string - TenantProviderFields []string - JWTEnabled bool -} - type apiHandlers struct { server *Server } type binder struct{} -func NewServer(workers *worker.Server, distros *distroregistry.Registry, config ServerConfig) *Server { - server := &Server{ - workers: workers, - distros: distros, - config: config, - } - return server -} - -func (server *Server) Handler(path string) http.Handler { - e := echo.New() - e.Binder = binder{} - e.HTTPErrorHandler = server.HTTPErrorHandler - e.Pre(common.OperationIDMiddleware) - e.Use(middleware.Recover()) - e.Logger = common.Logger() - - handler := apiHandlers{ - server: server, - } - RegisterHandlers(e.Group(path, prometheus.MetricsMiddleware), &handler) - - return e -} - func (b binder) Bind(i interface{}, ctx echo.Context) error { contentType := ctx.Request().Header["Content-Type"] if len(contentType) != 1 || contentType[0] != "application/json" { diff --git a/internal/cloudapi/v2/server.go b/internal/cloudapi/v2/server.go new file mode 100644 index 000000000..954a2cca2 --- /dev/null +++ b/internal/cloudapi/v2/server.go @@ -0,0 +1,51 @@ +package v2 + +import ( + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + + "github.com/osbuild/osbuild-composer/internal/common" + "github.com/osbuild/osbuild-composer/internal/distroregistry" + "github.com/osbuild/osbuild-composer/internal/prometheus" + "github.com/osbuild/osbuild-composer/internal/worker" +) + +// Server represents the state of the cloud Server +type Server struct { + workers *worker.Server + distros *distroregistry.Registry + config ServerConfig +} + +type ServerConfig struct { + AWSBucket string + TenantProviderFields []string + JWTEnabled bool +} + +func NewServer(workers *worker.Server, distros *distroregistry.Registry, config ServerConfig) *Server { + server := &Server{ + workers: workers, + distros: distros, + config: config, + } + return server +} + +func (s *Server) Handler(path string) http.Handler { + e := echo.New() + e.Binder = binder{} + e.HTTPErrorHandler = s.HTTPErrorHandler + e.Pre(common.OperationIDMiddleware) + e.Use(middleware.Recover()) + e.Logger = common.Logger() + + handler := apiHandlers{ + server: s, + } + RegisterHandlers(e.Group(path, prometheus.MetricsMiddleware), &handler) + + return e +}