113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package roleypoly
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/gofiber/fiber/v3/middleware/csrf"
|
|
"github.com/gofiber/fiber/v3/middleware/session"
|
|
"github.com/gofiber/fiber/v3/middleware/static"
|
|
"github.com/gofiber/template/html/v2"
|
|
|
|
"git.sapphic.engineer/roleypoly/v4/auth/authmiddleware"
|
|
"git.sapphic.engineer/roleypoly/v4/discord"
|
|
"git.sapphic.engineer/roleypoly/v4/interactions"
|
|
staticfs "git.sapphic.engineer/roleypoly/v4/static"
|
|
"git.sapphic.engineer/roleypoly/v4/templates"
|
|
"git.sapphic.engineer/roleypoly/v4/testing"
|
|
"git.sapphic.engineer/roleypoly/v4/types"
|
|
"git.sapphic.engineer/roleypoly/v4/utils"
|
|
)
|
|
|
|
func CreateFiberApp() *fiber.App {
|
|
app := fiber.New(fiber.Config{
|
|
Views: CreateViewEngine(),
|
|
ViewsLayout: "layouts/main",
|
|
})
|
|
|
|
return app
|
|
}
|
|
|
|
func CreateViewEngine() *html.Engine {
|
|
viewEngine := html.NewFileSystem(http.FS(templates.FS), ".html")
|
|
viewEngine.AddFuncMap(utils.TemplateFuncs())
|
|
return viewEngine
|
|
}
|
|
|
|
func oneStatic(c fiber.Ctx) error {
|
|
path := strings.Replace(c.OriginalURL(), "/", "", 1)
|
|
f, err := staticfs.FS.Open(path)
|
|
if err != nil {
|
|
log.Println("oneStatic:", c.OriginalURL(), " failed: ", err)
|
|
return c.SendStatus(500)
|
|
}
|
|
|
|
return c.SendStream(f)
|
|
}
|
|
|
|
func SetupStatic(app *fiber.App) {
|
|
app.Get("/static*", static.New("", static.Config{
|
|
FS: staticfs.FS,
|
|
Compress: true,
|
|
CacheDuration: 24 * 8 * time.Hour,
|
|
}))
|
|
app.Get("/favicon.ico", oneStatic)
|
|
app.Get("/manifest.json", oneStatic)
|
|
app.Get("/robots.txt", oneStatic)
|
|
app.Get("/humans.txt", oneStatic)
|
|
}
|
|
|
|
type ControllerConfig struct {
|
|
DiscordClient discord.IDiscordClient
|
|
PublicKey string
|
|
PublicBaseURL string
|
|
SupportIDs []string
|
|
SuperuserIDs []string
|
|
}
|
|
|
|
func SetupControllers(app *fiber.App, cfg ControllerConfig) {
|
|
app.Use(func(c fiber.Ctx) error {
|
|
c.Locals(types.ConfigKey, cfg)
|
|
return c.Next()
|
|
})
|
|
|
|
sessionMiddleware, sessionStore := session.NewWithStore()
|
|
sessionStore.RegisterType(authmiddleware.Session{})
|
|
|
|
app.Use(sessionMiddleware)
|
|
app.Use(csrf.New(csrf.Config{
|
|
Session: sessionStore,
|
|
}))
|
|
app.Use(authmiddleware.New(cfg.DiscordClient, cfg.SupportIDs, cfg.SuperuserIDs))
|
|
|
|
gs := discord.NewGuildService(cfg.DiscordClient)
|
|
|
|
(&testing.TestingController{Guilds: gs}).
|
|
Routes(app.Group("/testing"))
|
|
|
|
interactions.NewInteractions(cfg.PublicKey, cfg.PublicBaseURL, gs).
|
|
Routes(app.Group("/interactions"))
|
|
|
|
}
|
|
|
|
// func getStorageDirectory() string {
|
|
// path := os.Getenv("STORAGE_DIR")
|
|
// if path == "" {
|
|
// path = "./.storage"
|
|
// }
|
|
|
|
// _, err := os.Stat(path)
|
|
// if err != nil {
|
|
// if os.IsNotExist(err) {
|
|
// err := os.MkdirAll(path, 0744)
|
|
// if err != nil {
|
|
// log.Fatalln("failed to create storage directory: ", err)
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// return path
|
|
// }
|