From e849e705c30cceec3cf7336a21bed96c8a911e90 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Tue, 8 Oct 2024 03:43:08 +0300 Subject: Добавил рейтинг Добавил страницу топа Добавил rss/xml/json feed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/shorg/serve/serve.go | 52 ++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'cmd') diff --git a/cmd/shorg/serve/serve.go b/cmd/shorg/serve/serve.go index 73a7c68..04b6ad5 100644 --- a/cmd/shorg/serve/serve.go +++ b/cmd/shorg/serve/serve.go @@ -1,14 +1,22 @@ package serve import ( + echosession "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" - "github.com/ssoda/captcha" + echomiddleware "github.com/labstack/echo/v4/middleware" "github.com/uptrace/bun/extra/bundebug" "github.com/urfave/cli/v2" "sh.org.ru/pkg/config" "sh.org.ru/pkg/db" "sh.org.ru/pkg/handler" + "sh.org.ru/pkg/handler/add" + "sh.org.ru/pkg/handler/admin" + "sh.org.ru/pkg/handler/captcha" + "sh.org.ru/pkg/handler/feed" + "sh.org.ru/pkg/handler/quote" + "sh.org.ru/pkg/handler/rate" + "sh.org.ru/pkg/middleware" + "sh.org.ru/pkg/middleware/session" "sh.org.ru/static" ) @@ -21,34 +29,30 @@ func Run(c *cli.Context) error { db := db.New(cfg.DB) db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(cfg.Debug))) - h := handler.Handler{DB: db} + store, err := session.New(db, cfg.Keypairs) + if err != nil { + return err + } e := echo.New() - e.HTTPErrorHandler = handler.ErrorHandler e.Use( - middleware.Recover(), - middleware.Logger(), - middleware.RemoveTrailingSlash(), + echomiddleware.Recover(), + echomiddleware.Logger(), + echomiddleware.RemoveTrailingSlash(), + echosession.Middleware(store), + middleware.Context("config", cfg), ) - e.GET("/", h.Index) - e.GET("/quote/:id", h.Quote) - e.GET("/random", h.Random) - e.GET("/add", h.AddQuote) - e.POST("/add", h.AddQuotePost) - e.GET("/add/success", h.AddQuoteSuccess) - e.GET("/captcha/*", echo.WrapHandler(captcha.Server(400, 65))) - - adminMW := middleware.BasicAuth(func(u, p string, ctx echo.Context) (bool, error) { - return cfg.Admins[u] == p, nil - }) - - func(g *echo.Group) { - g.GET("/", h.Admin) - g.POST("/action", h.AdminAction) - g.GET("/export", h.AdminExport) - }(e.Group("/admin", adminMW)) + router := handler.Router{ + "/": quote.NewHandler(db), + "/add": add.NewHandler(db), + "/admin": admin.NewHandler(db, cfg), + "/captcha": captcha.NewHandler(), + "/rate": rate.NewHandler(db), + "/feed": feed.NewHandler(db, cfg), + } + router.Register(e) e.StaticFS("/", static.FS) -- cgit v1.2.3