added embed.FS for templates, cleaned up routes, restructured project

This commit is contained in:
fanir 2022-01-30 20:14:49 +01:00
parent feee1184d6
commit 9dccf24cf2
7 changed files with 49 additions and 21 deletions

View file

@ -40,8 +40,8 @@ var config = Config{
}, },
Server: ServerConfig{ Server: ServerConfig{
Address: ":3000", Address: ":3000",
TemplateDir: "../../templates", TemplateDir: "",
StaticDir: "../../static", StaticDir: "",
}, },
} }

View file

@ -6,11 +6,12 @@ import (
"log" "log"
"time" "time"
"git.zom.bi/fanir/feedizer/cmd/feedizer/config"
"git.zom.bi/fanir/feedizer/models/migrations"
"github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres" "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs" "github.com/golang-migrate/migrate/v4/source/iofs"
"git.zom.bi/fanir/feedizer/cmd/feedizer/internal/config"
"git.zom.bi/fanir/feedizer/models/migrations"
) )
var LogName string var LogName string

View file

@ -1,16 +1,30 @@
package server package server
import ( import (
"io/fs"
"log" "log"
"net/http"
"os"
"git.zom.bi/fanir/feedizer/cmd/feedizer/config"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html" "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) { func Start(cfg *config.Config) {
htmlEngine := html.New(cfg.Server.TemplateDir, ".html") 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{ app := fiber.New(fiber.Config{
Views: htmlEngine, Views: htmlEngine,
}) })
@ -18,26 +32,33 @@ func Start(cfg *config.Config) {
registerRoutes(app, cfg) 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)) log.Fatalln(app.Listen(cfg.Server.Address))
} }
func registerRoutes(app *fiber.App, cfg *config.Config) { func registerRoutes(app *fiber.App, cfg *config.Config) {
app.Get(`/`, func(ctx *fiber.Ctx) error { return ctx.Render("home", nil) }) app.Get(`/`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/home", nil) })
app.Get(`/about`, func(ctx *fiber.Ctx) error { return ctx.Render("about", 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("bot", 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("tos", 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("imprint", nil) }) app.Get(`/imprint`, func(ctx *fiber.Ctx) error { return ctx.Render("pages/imprint", nil) })
app.Static("/static", cfg.Server.StaticDir, fiber.Static{ app.Use("/static", filesystem.New(filesystem.Config{
Compress: true, Root: http.FS(os.DirFS(cfg.Server.StaticDir)),
}) }))
// app.Static("/static", cfg.Server.StaticDir, fiber.Static{
// Compress: true,
// })
app.Get(`/announcement/(?'id'[0-9]+)(/.*)?`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented }) 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.Post(`/feed`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
app.Get(`/feed/{slug}`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented }) app.Get(`/feed/:id`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
app.Get(`/feed/{slug}/page/(?'feedType'atom|rss)`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented }) app.Get(`/feed/:id/page`, func(c *fiber.Ctx) error { return fiber.ErrNotImplemented })
app.Get(`/feed/{slug}/diff/(?'feedType'atom|rss)`, 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 { func SendAPIResponse(ctx fiber.Ctx, template string, data interface{}) error {

View file

@ -0,0 +1,6 @@
package templates
import "embed"
//go:embed */*.html
var FS embed.FS

View file

@ -8,9 +8,9 @@ import (
"log" "log"
"os" "os"
"git.zom.bi/fanir/feedizer/cmd/feedizer/config" "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/config"
"git.zom.bi/fanir/feedizer/cmd/feedizer/database" "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/database"
"git.zom.bi/fanir/feedizer/cmd/feedizer/server" "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/server"
"github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4"
) )