From 34ccc98a942098faefb5f4211b215ff9ccc7ad0e Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Mon, 9 Dec 2024 01:07:15 +0300 Subject: Начальный MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/serve/serve.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 cmd/serve/serve.go (limited to 'cmd/serve') diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go new file mode 100644 index 0000000..8591084 --- /dev/null +++ b/cmd/serve/serve.go @@ -0,0 +1,67 @@ +package serve + +import ( + "context" + "os" + "time" + + "git.neonxp.ru/neonxp/guessr/pkg/api" + "git.neonxp.ru/neonxp/guessr/pkg/config" + "git.neonxp.ru/neonxp/guessr/pkg/db" + "git.neonxp.ru/neonxp/guessr/pkg/middleware" + "git.neonxp.ru/neonxp/guessr/pkg/service" + "github.com/gorilla/sessions" + echosession "github.com/labstack/echo-contrib/session" + "github.com/labstack/echo/v4" + echomiddleware "github.com/labstack/echo/v4/middleware" + "github.com/urfave/cli/v3" + "golang.org/x/time/rate" +) + +var Command = &cli.Command{ + Name: "serve", + Usage: "start api server", + Action: func(ctx context.Context, c *cli.Command) error { + cfg, err := config.New() + if err != nil { + return err + } + dbClient := db.New(cfg.DB) + placesService := service.New(dbClient) + + apiHandler := api.New(placesService) + + e := echo.New() + e.Debug = cfg.Debug + + e.Use( + echomiddleware.Recover(), + echomiddleware.Logger(), + echomiddleware.RemoveTrailingSlash(), + ) + + func(g *echo.Group) { + g.Use( + echosession.Middleware(sessions.NewCookieStore([]byte(cfg.Keys[0]), []byte(cfg.Keys[1]))), + middleware.PopulateState(), + echomiddleware.RateLimiter( + echomiddleware.NewRateLimiterMemoryStore( + rate.Every(time.Second), + ), + ), + ) + g.POST("/next", apiHandler.PostNext) + g.POST("/guess", apiHandler.PostGuess) + g.GET("/state", apiHandler.GetState) + g.POST("/state", apiHandler.PostState) + }(e.Group("/api")) + + // if cfg.Debug { + e.StaticFS("/", os.DirFS("./static")) + // } else { + // e.StaticFS("/", static.FS) + // } + + return e.Start(cfg.Listen) + }, +} -- cgit v1.2.3