109 lines
3 KiB
Go
109 lines
3 KiB
Go
package server
|
|
|
|
import (
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/template/html"
|
|
|
|
"git.zom.bi/fanir/feedizer/cmd/feedizer/internal/config"
|
|
"git.zom.bi/fanir/feedizer/cmd/feedizer/internal/templates"
|
|
)
|
|
|
|
func Start(cfg *config.Config) {
|
|
var templateFS fs.FS
|
|
templateFS = templates.FS
|
|
if cfg.Server.TemplateDir != "" {
|
|
templateFS = os.DirFS(cfg.Server.TemplateDir)
|
|
}
|
|
|
|
htmlEngine := html.NewFileSystem(http.FS(templateFS), ".html")
|
|
htmlEngine.Reload(cfg.Server.TemplateDir != "")
|
|
|
|
app := fiber.New(fiber.Config{
|
|
Views: htmlEngine,
|
|
})
|
|
app.Use(logger.New())
|
|
|
|
registerRoutes(app, cfg)
|
|
|
|
// Handle 404 errors - This must be the last registered handler!
|
|
app.Use(func(ctx *fiber.Ctx) error { return ctx.Render("errors/404", nil) })
|
|
|
|
log.Fatalln(app.Listen(cfg.Server.Address))
|
|
}
|
|
|
|
func registerRoutes(app *fiber.App, cfg *config.Config) {
|
|
app.Get(`/`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/home", nil) })
|
|
app.Get(`/about`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/about", nil) })
|
|
app.Get(`/bot`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/bot", nil) })
|
|
app.Get(`/tos`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/tos", nil) })
|
|
app.Get(`/imprint`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/imprint", nil) })
|
|
|
|
app.Use("/static", filesystem.New(filesystem.Config{
|
|
Root: http.FS(os.DirFS(cfg.Server.StaticDir)),
|
|
}))
|
|
// app.Static("/static", cfg.Server.StaticDir, fiber.Static{
|
|
// Compress: true,
|
|
// })
|
|
|
|
app.Get(`/announcements`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
|
|
app.Get(`/announcement/:id`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
|
|
|
|
app.Post(`/feed`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
|
|
app.Get(`/feed/:id`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
|
|
app.Get(`/feed/:id/page`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
|
|
app.Get(`/feed/:id/diff`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
|
|
}
|
|
|
|
func SendAPIResponse(ctx fiber.Ctx, template string, data interface{}) error {
|
|
switch ctx.Accepts("text/html", "application/json") {
|
|
case "text/html":
|
|
return ctx.Render(template, data)
|
|
case "application/json":
|
|
return ctx.JSON(data)
|
|
}
|
|
return fiber.ErrNotAcceptable
|
|
}
|
|
|
|
const (
|
|
AtomFeed = "atom"
|
|
RSSFeed = "rss"
|
|
JSONFeed = "json"
|
|
)
|
|
|
|
func RequestedFeedType(ctx fiber.Ctx) string {
|
|
format := ctx.Query("format",
|
|
ctx.Accepts(
|
|
"application/atom+xml", // Atom
|
|
"application/rss+xml", "text/xml", // RSS
|
|
"application/feed+json", // JSON Feed
|
|
),
|
|
)
|
|
switch format {
|
|
case "atom", "application/atom+xml":
|
|
return AtomFeed
|
|
case "rss", "application/rss+xml", "text/xml":
|
|
return RSSFeed
|
|
case "json", "application/feed+json":
|
|
return JSONFeed
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
func SendFeedResponse(ctx fiber.Ctx, format string, data interface{}) error {
|
|
switch format {
|
|
case AtomFeed:
|
|
case RSSFeed:
|
|
case JSONFeed:
|
|
}
|
|
|
|
return fiber.ErrNotAcceptable
|
|
}
|