added embed.FS for templates, cleaned up routes, restructured project
This commit is contained in:
parent
feee1184d6
commit
9dccf24cf2
7 changed files with 49 additions and 21 deletions
|
@ -40,8 +40,8 @@ var config = Config{
|
|||
},
|
||||
Server: ServerConfig{
|
||||
Address: ":3000",
|
||||
TemplateDir: "../../templates",
|
||||
StaticDir: "../../static",
|
||||
TemplateDir: "",
|
||||
StaticDir: "",
|
||||
},
|
||||
}
|
||||
|
|
@ -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
|
|
@ -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 {
|
6
cmd/feedizer/internal/templates/templates.go
Normal file
6
cmd/feedizer/internal/templates/templates.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package templates
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed */*.html
|
||||
var FS embed.FS
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue