diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-07 01:06:55 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-07 01:18:35 +0300 |
commit | 420e049415c8ec7f7a209a03110eecbe0c83e9e0 (patch) | |
tree | e93f7faf8c9704daa24c2653ca50b4f222fa2305 /cmd/shorg | |
parent | 0617918eb941c401b687d4a0dbc2a54c19e06fd1 (diff) |
Мелкие правки
Diffstat (limited to 'cmd/shorg')
-rw-r--r-- | cmd/shorg/main.go | 34 | ||||
-rw-r--r-- | cmd/shorg/migrator/migrator.go | 223 | ||||
-rw-r--r-- | cmd/shorg/serve/serve.go | 55 |
3 files changed, 312 insertions, 0 deletions
diff --git a/cmd/shorg/main.go b/cmd/shorg/main.go new file mode 100644 index 0000000..df36817 --- /dev/null +++ b/cmd/shorg/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "log" + "os" + + "github.com/urfave/cli/v2" + + "sh.org.ru/cmd/shorg/migrator" + "sh.org.ru/cmd/shorg/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", + }, + }, + }, + migrator.Migrator(), + }, + } + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/shorg/migrator/migrator.go b/cmd/shorg/migrator/migrator.go new file mode 100644 index 0000000..01dd819 --- /dev/null +++ b/cmd/shorg/migrator/migrator.go @@ -0,0 +1,223 @@ +package migrator + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strings" + + "github.com/uptrace/bun/migrate" + "github.com/urfave/cli/v2" + "sh.org.ru/migrations" + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/db" + "sh.org.ru/pkg/model" +) + +func Migrator() *cli.Command { + return &cli.Command{ + Name: "db", + Usage: "manage database migrations", + Subcommands: []*cli.Command{ + { + Name: "init", + Usage: "create migration tables", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, migrations.Migrations) + if err != nil { + return err + } + log.Println("init") + return migrator.Init(c.Context) + }, + }, + { + Name: "migrate", + Usage: "migrate database", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, migrations.Migrations) + if err != nil { + return err + } + + group, err := migrator.Migrate(c.Context) + if err != nil { + return err + } + + if group.ID == 0 { + fmt.Printf("there are no new migrations to run\n") + return nil + } + + fmt.Printf("migrated to %s\n", group) + return nil + }, + }, + { + Name: "rollback", + Usage: "rollback the last migration group", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, migrations.Migrations) + if err != nil { + return err + } + + group, err := migrator.Rollback(c.Context) + if err != nil { + return err + } + + if group.ID == 0 { + fmt.Printf("there are no groups to roll back\n") + return nil + } + + fmt.Printf("rolled back %s\n", group) + return nil + }, + }, + { + Name: "lock", + Usage: "lock migrations", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, migrations.Migrations) + if err != nil { + return err + } + + return migrator.Lock(c.Context) + }, + }, + { + Name: "unlock", + Usage: "unlock migrations", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, migrations.Migrations) + if err != nil { + return err + } + + return migrator.Unlock(c.Context) + }, + }, + { + Name: "create", + Usage: "create migration", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, migrations.Migrations) + if err != nil { + return err + } + + name := strings.Join(c.Args().Slice(), "_") + mf, err := migrator.CreateGoMigration(c.Context, name) + if err != nil { + return err + } + fmt.Printf("created migration %s (%s)\n", mf.Name, mf.Path) + + return nil + }, + }, + { + Name: "import", + Usage: "import json with quotes", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Value: "./config/config.yaml", + Usage: "config", + }, + }, + Action: func(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 + }, + }, + }, + } +} + +func newMigrator(c *cli.Context, migrations *migrate.Migrations) (*migrate.Migrator, error) { + configFile := c.String("config") + cfg, err := config.New(configFile) + if err != nil { + return nil, err + } + db := db.New(cfg.DB) + + migrator := migrate.NewMigrator(db, migrations) + return migrator, nil +} diff --git a/cmd/shorg/serve/serve.go b/cmd/shorg/serve/serve.go new file mode 100644 index 0000000..13354b7 --- /dev/null +++ b/cmd/shorg/serve/serve.go @@ -0,0 +1,55 @@ +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.HTTPErrorHandler = handler.ErrorHandler + + e.Use( + middleware.Recover(), + middleware.Logger(), + middleware.RemoveTrailingSlash(), + ) + + 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))) + + func(g *echo.Group) { + g.GET("/", h.Admin) + g.POST("/action", h.AdminAction) + g.GET("/export", h.AdminExport) + }(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) +} |