diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-08 03:43:08 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-08 03:50:53 +0300 |
commit | e849e705c30cceec3cf7336a21bed96c8a911e90 (patch) | |
tree | 93f559bcd4cf3e53193930d112e564a2b7462ac8 /cmd/shorg | |
parent | 3ee654f6fb3cdf119630bfba8066c96ec26428c3 (diff) |
Добавил рейтинг
Добавил страницу топа
Добавил rss/xml/json feed
Diffstat (limited to 'cmd/shorg')
-rw-r--r-- | cmd/shorg/serve/serve.go | 52 |
1 files changed, 28 insertions, 24 deletions
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) |