diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..ab07226 --- /dev/null +++ b/app/app.go @@ -0,0 +1,43 @@ +package app + +import ( + "codeberg.org/danjones000/lenore/config" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/logger" +) + +type passReq struct { + Pass string `json:"pass"` +} + +func New() *fiber.App { + app := fiber.New() + conf := config.GetConfig() + + app.Use(func(c *fiber.Ctx) error { + c.Set("X-Clacks-Overhead", "GNU Terry Pratchett, Robin Williams") + c.Next() + return nil + }) + app.Use(logger.New(logger.Config{ + Format: "${time} | ${status} | ${latency} | ${method} | ${path} | ${body}\n", + TimeFormat: "2006-01-02T15:04:05", + })) + app.Use(cors.New(cors.Config{MaxAge: 3600})) + + app.Get("/", func(c *fiber.Ctx) error { + return c.JSON(&conf) + }) + + app.Post("/check_pass", func(c *fiber.Ctx) error { + req := new(passReq) + if err := c.BodyParser(req); err != nil { + panic(err) + } + + return c.JSON(config.CheckPassword(req.Pass)) + }) + + return app +} diff --git a/commands/app.go b/commands/app.go index 5fddd11..226d87c 100644 --- a/commands/app.go +++ b/commands/app.go @@ -1,10 +1,7 @@ package commands import ( - "codeberg.org/danjones000/lenore/config" - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/logger" + web "codeberg.org/danjones000/lenore/app" "github.com/spf13/cobra" "log" ) @@ -13,34 +10,8 @@ type passReq struct { Pass string `json:"pass"` } -func app(cmd *cobra.Command, args []string) { - app := fiber.New() - conf := config.GetConfig() - - app.Use(func(c *fiber.Ctx) error { - c.Set("X-Clacks-Overhead", "GNU Terry Pratchett, Robin Williams") - c.Next() - return nil - }) - app.Use(logger.New(logger.Config{ - Format: "${time} | ${status} | ${latency} | ${method} | ${path} | ${body}\n", - TimeFormat: "2006-01-02T15:04:05", - })) - app.Use(cors.New(cors.Config{MaxAge: 3600})) - - app.Get("/", func(c *fiber.Ctx) error { - return c.JSON(&conf) - }) - - app.Post("/check_pass", func(c *fiber.Ctx) error { - req := new(passReq) - if err := c.BodyParser(req); err != nil { - panic(err) - } - - return c.JSON(config.CheckPassword(req.Pass)) - }) - +func run(cmd *cobra.Command, args []string) { + app := web.New() log.Fatal(app.Listen(":3000")) } @@ -49,5 +20,5 @@ var AppCmd = &cobra.Command{ Short: "Run the lenore webapp", Long: `This is the main command. This will start the application, and keep it running. You may want to run this from something like supervisor or systemd`, - Run: app, + Run: run, }