♻️ Move code for web app to its own folder

This commit is contained in:
Dan Jones 2023-03-27 14:12:47 -05:00
commit 2ac49565fd
2 changed files with 47 additions and 33 deletions

43
app/app.go Normal file
View file

@ -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
}

View file

@ -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,
}