From 6160b4fdc5f37e4ceaa6b3c5acc855f466049d61 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Sun, 6 Oct 2024 17:04:37 +0300 Subject: Первая версия MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/importer/importer.go | 47 +++++++ cmd/app/main.go | 43 +++++++ cmd/app/serve/serve.go | 49 +++++++ cmd/bun/main.go | 191 ++++++++++++++++++++++++++++ cmd/bun/migrations/20241005143542_1_init.go | 27 ++++ cmd/bun/migrations/main.go | 7 + 6 files changed, 364 insertions(+) create mode 100644 cmd/app/importer/importer.go create mode 100644 cmd/app/main.go create mode 100644 cmd/app/serve/serve.go create mode 100644 cmd/bun/main.go create mode 100644 cmd/bun/migrations/20241005143542_1_init.go create mode 100644 cmd/bun/migrations/main.go (limited to 'cmd') 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) +} diff --git a/cmd/bun/main.go b/cmd/bun/main.go new file mode 100644 index 0000000..62c9441 --- /dev/null +++ b/cmd/bun/main.go @@ -0,0 +1,191 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/uptrace/bun/migrate" + "sh.org.ru/cmd/bun/migrations" + "sh.org.ru/pkg/config" + "sh.org.ru/pkg/db" + + "github.com/urfave/cli/v2" +) + +func main() { + app := &cli.App{ + Name: "bun", + Commands: []*cli.Command{ + newDBCommand(migrations.Migrations), + }, + } + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} + +func newDBCommand(migrations *migrate.Migrations) *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/dev.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, 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/dev.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, 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/dev.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, 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/dev.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, 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/dev.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, 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/dev.yaml", + Usage: "config", + }, + }, + Action: func(c *cli.Context) error { + migrator, err := newMigrator(c, 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 + }, + }, + }, + } +} + +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/bun/migrations/20241005143542_1_init.go b/cmd/bun/migrations/20241005143542_1_init.go new file mode 100644 index 0000000..dc92797 --- /dev/null +++ b/cmd/bun/migrations/20241005143542_1_init.go @@ -0,0 +1,27 @@ +package migrations + +import ( + "context" + "fmt" + + "github.com/uptrace/bun" + "sh.org.ru/pkg/model" +) + +func init() { + Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error { + fmt.Print(" [up migration] ") + if _, err := db.NewCreateTable().Model((*model.Quote)(nil)).Exec(ctx); err != nil { + return err + } + + return nil + }, func(ctx context.Context, db *bun.DB) error { + fmt.Print(" [down migration] ") + if _, err := db.NewDropTable().Model((*model.Quote)(nil)).Exec(ctx); err != nil { + return err + } + + return nil + }) +} diff --git a/cmd/bun/migrations/main.go b/cmd/bun/migrations/main.go new file mode 100644 index 0000000..f7346fb --- /dev/null +++ b/cmd/bun/migrations/main.go @@ -0,0 +1,7 @@ +package migrations + +import ( + "github.com/uptrace/bun/migrate" +) + +var Migrations = migrate.NewMigrations() -- cgit v1.2.3