aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/app/importer/importer.go47
-rw-r--r--cmd/bun/migrations/20241005143542_1_init.go27
-rw-r--r--cmd/bun/migrations/main.go7
-rw-r--r--cmd/shorg/main.go (renamed from cmd/app/main.go)17
-rw-r--r--cmd/shorg/migrator/migrator.go (renamed from cmd/bun/main.go)90
-rw-r--r--cmd/shorg/serve/serve.go (renamed from cmd/app/serve/serve.go)8
6 files changed, 71 insertions, 125 deletions
diff --git a/cmd/app/importer/importer.go b/cmd/app/importer/importer.go
deleted file mode 100644
index 0cc9bb7..0000000
--- a/cmd/app/importer/importer.go
+++ /dev/null
@@ -1,47 +0,0 @@
-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(&quotes); 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/bun/migrations/20241005143542_1_init.go b/cmd/bun/migrations/20241005143542_1_init.go
deleted file mode 100644
index dc92797..0000000
--- a/cmd/bun/migrations/20241005143542_1_init.go
+++ /dev/null
@@ -1,27 +0,0 @@
-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
deleted file mode 100644
index f7346fb..0000000
--- a/cmd/bun/migrations/main.go
+++ /dev/null
@@ -1,7 +0,0 @@
-package migrations
-
-import (
- "github.com/uptrace/bun/migrate"
-)
-
-var Migrations = migrate.NewMigrations()
diff --git a/cmd/app/main.go b/cmd/shorg/main.go
index dcd3eb0..df36817 100644
--- a/cmd/app/main.go
+++ b/cmd/shorg/main.go
@@ -5,8 +5,9 @@ import (
"os"
"github.com/urfave/cli/v2"
- "sh.org.ru/cmd/app/importer"
- "sh.org.ru/cmd/app/serve"
+
+ "sh.org.ru/cmd/shorg/migrator"
+ "sh.org.ru/cmd/shorg/serve"
)
func main() {
@@ -24,17 +25,7 @@ func main() {
},
},
},
- {
- Name: "import",
- Action: importer.Run,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "config",
- Value: "./config/dev.yaml",
- Usage: "config",
- },
- },
- },
+ migrator.Migrator(),
},
}
if err := app.Run(os.Args); err != nil {
diff --git a/cmd/bun/main.go b/cmd/shorg/migrator/migrator.go
index 62c9441..01dd819 100644
--- a/cmd/bun/main.go
+++ b/cmd/shorg/migrator/migrator.go
@@ -1,32 +1,21 @@
-package main
+package migrator
import (
+ "encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/uptrace/bun/migrate"
- "sh.org.ru/cmd/bun/migrations"
+ "github.com/urfave/cli/v2"
+ "sh.org.ru/migrations"
"sh.org.ru/pkg/config"
"sh.org.ru/pkg/db"
-
- "github.com/urfave/cli/v2"
+ "sh.org.ru/pkg/model"
)
-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 {
+func Migrator() *cli.Command {
return &cli.Command{
Name: "db",
Usage: "manage database migrations",
@@ -37,12 +26,12 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
- Value: "./config/dev.yaml",
+ Value: "./config/config.yaml",
Usage: "config",
},
},
Action: func(c *cli.Context) error {
- migrator, err := newMigrator(c, migrations)
+ migrator, err := newMigrator(c, migrations.Migrations)
if err != nil {
return err
}
@@ -56,12 +45,12 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
- Value: "./config/dev.yaml",
+ Value: "./config/config.yaml",
Usage: "config",
},
},
Action: func(c *cli.Context) error {
- migrator, err := newMigrator(c, migrations)
+ migrator, err := newMigrator(c, migrations.Migrations)
if err != nil {
return err
}
@@ -86,12 +75,12 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
- Value: "./config/dev.yaml",
+ Value: "./config/config.yaml",
Usage: "config",
},
},
Action: func(c *cli.Context) error {
- migrator, err := newMigrator(c, migrations)
+ migrator, err := newMigrator(c, migrations.Migrations)
if err != nil {
return err
}
@@ -116,12 +105,12 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
- Value: "./config/dev.yaml",
+ Value: "./config/config.yaml",
Usage: "config",
},
},
Action: func(c *cli.Context) error {
- migrator, err := newMigrator(c, migrations)
+ migrator, err := newMigrator(c, migrations.Migrations)
if err != nil {
return err
}
@@ -135,12 +124,12 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
- Value: "./config/dev.yaml",
+ Value: "./config/config.yaml",
Usage: "config",
},
},
Action: func(c *cli.Context) error {
- migrator, err := newMigrator(c, migrations)
+ migrator, err := newMigrator(c, migrations.Migrations)
if err != nil {
return err
}
@@ -154,12 +143,12 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
- Value: "./config/dev.yaml",
+ Value: "./config/config.yaml",
Usage: "config",
},
},
Action: func(c *cli.Context) error {
- migrator, err := newMigrator(c, migrations)
+ migrator, err := newMigrator(c, migrations.Migrations)
if err != nil {
return err
}
@@ -174,6 +163,49 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
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(&quotes); 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/serve/serve.go b/cmd/shorg/serve/serve.go
index 0609083..13354b7 100644
--- a/cmd/app/serve/serve.go
+++ b/cmd/shorg/serve/serve.go
@@ -27,8 +27,11 @@ func Run(c *cli.Context) error {
e.HTTPErrorHandler = handler.ErrorHandler
- e.Use(middleware.Recover())
- e.Use(middleware.Logger())
+ e.Use(
+ middleware.Recover(),
+ middleware.Logger(),
+ middleware.RemoveTrailingSlash(),
+ )
e.GET("/", h.Index)
e.GET("/quote/:id", h.Quote)
@@ -41,6 +44,7 @@ func Run(c *cli.Context) error {
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
})))