diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-06 17:04:37 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-06 17:04:37 +0300 |
commit | 6160b4fdc5f37e4ceaa6b3c5acc855f466049d61 (patch) | |
tree | b6e41f9cad22515a61cc50b0a82d98ee55e0c3e3 /cmd/app | |
parent | 81b13617c4d0ca68afb181d1105386f0c339864d (diff) |
Первая версия
Diffstat (limited to 'cmd/app')
-rw-r--r-- | cmd/app/importer/importer.go | 47 | ||||
-rw-r--r-- | cmd/app/main.go | 43 | ||||
-rw-r--r-- | cmd/app/serve/serve.go | 49 |
3 files changed, 139 insertions, 0 deletions
diff --git a/cmd/app/importer/importer.go b/cmd/app/importer/importer.go new file mode 100644 index 0000000..0cc9bb7 --- /dev/null +++ b/cmd/app/importer/importer.go @@ -0,0 +1,47 @@ +package importer + +import ( + "encoding/json" + "os" + + "github.com/urfave/cli/v2" + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/db" + "sh.org.ru/pkg/model" +) + +func Run(c *cli.Context) error { + configFile := c.String("config") + cfg, err := config.New(configFile) + if err != nil { + return err + } + db := db.New(cfg.DB) + + file := c.Args().First() + + quotes := []string{} + + fp, err := os.Open(file) + if err != nil { + return err + } + defer fp.Close() + + if err := json.NewDecoder(fp).Decode("es); err != nil { + return err + } + + for _, text := range quotes { + q := &model.Quote{ + Quote: text, + Approved: true, + Archive: true, + } + if _, err := db.NewInsert().Model(q).Exec(c.Context); err != nil { + return err + } + } + + return nil +} diff --git a/cmd/app/main.go b/cmd/app/main.go new file mode 100644 index 0000000..dcd3eb0 --- /dev/null +++ b/cmd/app/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "log" + "os" + + "github.com/urfave/cli/v2" + "sh.org.ru/cmd/app/importer" + "sh.org.ru/cmd/app/serve" +) + +func main() { + app := &cli.App{ + Name: "app", + Commands: []*cli.Command{ + { + Name: "serve", + Action: serve.Run, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/dev.yaml", + Usage: "config", + }, + }, + }, + { + Name: "import", + Action: importer.Run, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/dev.yaml", + Usage: "config", + }, + }, + }, + }, + } + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/app/serve/serve.go b/cmd/app/serve/serve.go new file mode 100644 index 0000000..dcc296b --- /dev/null +++ b/cmd/app/serve/serve.go @@ -0,0 +1,49 @@ +package serve + +import ( + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "github.com/ssoda/captcha" + "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/static" +) + +func Run(c *cli.Context) error { + configFile := c.String("config") + cfg, err := config.New(configFile) + if err != nil { + return err + } + db := db.New(cfg.DB) + db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(cfg.Debug))) + + h := handler.Handler{DB: db} + + e := echo.New() + + e.Use(middleware.Recover()) + e.Use(middleware.Logger()) + + e.GET("/", h.Index) + e.GET("/random", h.Random) + e.GET("/quote/:id", h.Quote) + 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))) + + func(g *echo.Group) { + g.GET("/", h.Admin) + g.POST("/action", h.AdminAction) + }(e.Group("/admin", middleware.BasicAuth(func(u, p string, ctx echo.Context) (bool, error) { + return cfg.Admins[u] == p, nil + }))) + + e.StaticFS("/", static.FS) + + return e.Start(cfg.Listen) +} |