diff --git a/cmd/feedizer/config/config.go b/cmd/feedizer/internal/config/config.go similarity index 95% rename from cmd/feedizer/config/config.go rename to cmd/feedizer/internal/config/config.go index cb14b78..403de04 100644 --- a/cmd/feedizer/config/config.go +++ b/cmd/feedizer/internal/config/config.go @@ -40,8 +40,8 @@ var config = Config{ }, Server: ServerConfig{ Address: ":3000", - TemplateDir: "../../templates", - StaticDir: "../../static", + TemplateDir: "", + StaticDir: "", }, } diff --git a/cmd/feedizer/config/env.go b/cmd/feedizer/internal/config/env.go similarity index 100% rename from cmd/feedizer/config/env.go rename to cmd/feedizer/internal/config/env.go diff --git a/cmd/feedizer/config/files.go b/cmd/feedizer/internal/config/files.go similarity index 100% rename from cmd/feedizer/config/files.go rename to cmd/feedizer/internal/config/files.go diff --git a/cmd/feedizer/database/database.go b/cmd/feedizer/internal/database/database.go similarity index 97% rename from cmd/feedizer/database/database.go rename to cmd/feedizer/internal/database/database.go index e193c9f..ca1a5fa 100644 --- a/cmd/feedizer/database/database.go +++ b/cmd/feedizer/internal/database/database.go @@ -6,11 +6,12 @@ import ( "log" "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/database/postgres" "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 diff --git a/cmd/feedizer/server/server.go b/cmd/feedizer/internal/server/server.go similarity index 54% rename from cmd/feedizer/server/server.go rename to cmd/feedizer/internal/server/server.go index 52c99e3..c2818b6 100644 --- a/cmd/feedizer/server/server.go +++ b/cmd/feedizer/internal/server/server.go @@ -1,16 +1,30 @@ package server import ( + "io/fs" "log" + "net/http" + "os" - "git.zom.bi/fanir/feedizer/cmd/feedizer/config" "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) { - 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{ Views: htmlEngine, }) @@ -18,26 +32,33 @@ func Start(cfg *config.Config) { 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("home", nil) }) - app.Get(`/about`, func(ctx *fiber.Ctx) error { return ctx.Render("about", nil) }) - app.Get(`/bot`, func(ctx *fiber.Ctx) error { return ctx.Render("bot", nil) }) - app.Get(`/tos`, func(ctx *fiber.Ctx) error { return ctx.Render("tos", nil) }) - app.Get(`/imprint`, func(ctx *fiber.Ctx) error { return ctx.Render("imprint", 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("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.Static("/static", cfg.Server.StaticDir, fiber.Static{ - Compress: true, - }) + 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(`/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.Get(`/feed/{slug}`, 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/{slug}/diff/(?'feedType'atom|rss)`, 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 { diff --git a/cmd/feedizer/internal/templates/templates.go b/cmd/feedizer/internal/templates/templates.go new file mode 100644 index 0000000..b299964 --- /dev/null +++ b/cmd/feedizer/internal/templates/templates.go @@ -0,0 +1,6 @@ +package templates + +import "embed" + +//go:embed */*.html +var FS embed.FS diff --git a/cmd/feedizer/main.go b/cmd/feedizer/main.go index ee825c5..7fc7b35 100644 --- a/cmd/feedizer/main.go +++ b/cmd/feedizer/main.go @@ -8,9 +8,9 @@ import ( "log" "os" - "git.zom.bi/fanir/feedizer/cmd/feedizer/config" - "git.zom.bi/fanir/feedizer/cmd/feedizer/database" - "git.zom.bi/fanir/feedizer/cmd/feedizer/server" + "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/config" + "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/database" + "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/server" "github.com/golang-migrate/migrate/v4" )