tijl.dev-core/cmd/server/main.go

72 lines
1.6 KiB
Go
Raw Normal View History

2024-08-19 22:32:43 +02:00
package main
import (
2024-08-20 11:49:05 +02:00
"html/template"
"net/http"
2024-08-19 22:32:43 +02:00
"git.tijl.dev/tijl/tijl.dev/internal/assets"
2024-08-20 13:35:49 +02:00
"git.tijl.dev/tijl/tijl.dev/internal/config"
2024-08-19 22:32:43 +02:00
"git.tijl.dev/tijl/tijl.dev/internal/i18n"
2024-08-20 11:49:05 +02:00
"git.tijl.dev/tijl/tijl.dev/static"
"git.tijl.dev/tijl/tijl.dev/views"
2024-08-19 22:32:43 +02:00
"github.com/gofiber/fiber/v2"
2024-08-20 11:49:05 +02:00
"github.com/gofiber/fiber/v2/middleware/filesystem"
2024-08-19 22:32:43 +02:00
"github.com/gofiber/template/html/v2"
2024-08-20 13:35:49 +02:00
"github.com/mikhail-bigun/fiberlogrus"
log "github.com/sirupsen/logrus"
2024-08-19 22:32:43 +02:00
)
func main() {
2024-08-20 13:35:49 +02:00
log.SetLevel(log.DebugLevel)
config.Load()
assets.Load()
i18n.Load()
2024-08-19 22:32:43 +02:00
2024-08-20 11:49:05 +02:00
engine := html.NewFileSystem(http.FS(views.Embed), ".html")
2024-08-19 22:32:43 +02:00
engine.AddFunc("icon", assets.Svg)
2024-08-20 11:49:05 +02:00
// Todo better language system
engine.AddFunc("translate", func(key string) template.HTML {
translations := i18n.GetTranslations()["en"] // for now we just fix it to english
if translation, ok := translations[key]; ok {
return template.HTML(translation)
}
return ""
})
2024-08-19 22:32:43 +02:00
app := fiber.New(fiber.Config{
2024-08-20 13:35:49 +02:00
Views: engine,
DisableStartupMessage: true,
2024-08-19 22:32:43 +02:00
})
2024-08-20 13:35:49 +02:00
app.Use(fiberlogrus.New())
2024-08-19 22:32:43 +02:00
app.Get("/", func(c *fiber.Ctx) error {
2024-08-20 13:35:49 +02:00
return c.Render("index", getCommon(c))
})
app.Get("/blog", func(c *fiber.Ctx) error {
return c.Render("blog", getCommon(c))
})
app.Get("/projects", func(c *fiber.Ctx) error {
return c.Render("projects", getCommon(c))
})
app.Get("/about", func(c *fiber.Ctx) error {
return c.Render("about", getCommon(c))
2024-08-19 22:32:43 +02:00
})
2024-08-20 11:49:05 +02:00
app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(static.Embed),
}))
2024-08-19 22:32:43 +02:00
log.Fatal(app.Listen(":3000"))
}
2024-08-20 13:35:49 +02:00
func getCommon(c *fiber.Ctx) fiber.Map {
return fiber.Map{
"Path": c.Path(),
}
}