static-hoster/src/router/router.go
Nikurasu e0440801e7
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
Switched to fiber
2023-04-22 17:42:29 +02:00

37 lines
874 B
Go

package router
import (
"fmt"
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/basicauth"
"nikurasu.gay/static-hoster/api"
"nikurasu.gay/static-hoster/envloader"
)
func Create(env *envloader.Environment) *fiber.App {
router := fiber.New()
router.Static("", env.StaticDir)
apiRoutes := router.Group("/api", basicauth.New(basicauth.Config{
Users: map[string]string{
env.User: env.ApiKey,
},
}))
apiRoutes.Post("/update", api.PostUpdate(env))
// Ping test
router.Get("/ping", func(c *fiber.Ctx) error {
c.Status(http.StatusOK)
return c.SendString("Pong!")
})
// Use the "old" method becuase I don't know how to pass the error code to the user generated html
router.Use(func(ctx *fiber.Ctx) error {
return ctx.Status(http.StatusNotFound).SendFile(fmt.Sprintf("%s404.html", env.StaticDir))
})
return router
}