diff --git a/.air.toml b/.air.toml index b38a4f0..f01223d 100644 --- a/.air.toml +++ b/.air.toml @@ -5,7 +5,7 @@ tmp_dir = ".air-tmp" bin = "./tijl.dev" cmd = "just build" delay = 1000 - exclude_dir = ["node_modules","modules/database","static/js","static/css"] + exclude_dir = ["node_modules","modules/database","web/assets/static"] exclude_unchanged = false follow_symlink = false include_ext = ["go", "html", "js", "ts"] diff --git a/.gitignore b/.gitignore index 7e0dca6..4baefbb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ node_modules/ tijl.dev -static/js/interactive.js -static/css/styles.css +web/assets/static/js/interactive.js +web/assets/static/css/styles.css config.yaml diff --git a/cmd/server/main.go b/cmd/server/main.go index 4e205cb..6387c32 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,132 +1,9 @@ package main import ( - "context" - "net/http" - - "git.tijl.dev/tijl/tijl.dev/internal/assets" - "git.tijl.dev/tijl/tijl.dev/internal/config" - "git.tijl.dev/tijl/tijl.dev/internal/db" - "git.tijl.dev/tijl/tijl.dev/internal/i18n" - "git.tijl.dev/tijl/tijl.dev/internal/oidc" - "git.tijl.dev/tijl/tijl.dev/internal/sessions" - "git.tijl.dev/tijl/tijl.dev/modules/logger" - "git.tijl.dev/tijl/tijl.dev/static" - "git.tijl.dev/tijl/tijl.dev/views" - "github.com/gofiber/contrib/fiberzerolog" - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/filesystem" - "github.com/gofiber/template/html/v2" + "git.tijl.dev/tijl/tijl.dev/internal/tijl.dev/service" ) func main() { - // Load initial context - ctx := context.Background() - - // Load config - config.Load() - - // Load database - db.Load() - - // Setup oidc - oidc.Load(ctx) - - // Load assets - assets.Load() - // Load translations - i18n.Load() - - // Init templating engine - engine := html.NewFileSystem(http.FS(views.Embed), ".html") - engine.AddFunc("icon", assets.Svg) - - // Init fiber - app := fiber.New(fiber.Config{ - Views: engine, - DisableStartupMessage: true, - }) - app.Use(fiberzerolog.New(fiberzerolog.Config{ - Logger: &log.Logger, - })) - - /* - Routes - */ - app.Get("/", func(c *fiber.Ctx) error { - data := getCommon(c) - data["Title"] = i18n.Translate(c, "home") - return c.Render("index", data, "layouts/base") - }) - app.Get("/blog", func(c *fiber.Ctx) error { - data := getCommon(c) - data["Title"] = i18n.Translate(c, "blog") - return c.Render("blog", data, "layouts/base") - }) - app.Get("/projects", func(c *fiber.Ctx) error { - data := getCommon(c) - data["Title"] = i18n.Translate(c, "projects") - return c.Render("projects", data, "layouts/base") - }) - app.Get("/about", func(c *fiber.Ctx) error { - data := getCommon(c) - data["Title"] = i18n.Translate(c, "about") - return c.Render("about", data, "layouts/base") - }) - app.Get("/settings", func(c *fiber.Ctx) error { - lang := c.Query("lang") - redirectURL := c.Query("redirect") - if lang != "" { - c.Cookie(&fiber.Cookie{ - Name: "language", - Value: lang, - Secure: false, - }) - } - if redirectURL != "" { - return c.Redirect(redirectURL) - } - return nil - }) - - app.Get("/login", func(c *fiber.Ctx) error { - _, err := sessions.GetSession(c) - if err == nil { - return c.Redirect("/account") - } else { - return oidc.HandleRedirect(c) - } - }) - app.Get(config.Config.Oidc.CallbackUrl, oidc.HandleCallback) - - // Static routes - app.Use("/static", filesystem.New(filesystem.Config{ - Root: http.FS(static.Embed), - })) - - // 404 - app.Use(func(c *fiber.Ctx) error { - data := getCommon(c) - return c.Render("404", data, "layouts/base") - }) - - // Listen web server - if err := app.Listen(":3000"); err != nil { - log.Fatal().Err(err).Msg("Fiber app error") - } -} - -// Common functions for in templating -func getCommon(c *fiber.Ctx) fiber.Map { - _, err := sessions.GetSession(c) - signedIn := false - if err == nil { - signedIn = true - } - return fiber.Map{ - "SignedIn": signedIn, - "Path": c.Path(), - "Language": i18n.GetLanguage(c), - "T": i18n.GetTranslations(i18n.GetLanguage(c)), - } + service.Listen() } diff --git a/migrations/migrations.go b/embed.go similarity index 100% rename from migrations/migrations.go rename to embed.go diff --git a/internal/assets/svg.go b/internal/assets/svg.go index fcdf5b4..9dfcf53 100644 --- a/internal/assets/svg.go +++ b/internal/assets/svg.go @@ -14,7 +14,9 @@ var SVGData map[string]string func loadSVGs() { SVGData = make(map[string]string) - dir := "static/assets/icons" + dir := "web/assets/icons" + + // assets.Embed files, err := filepath.Glob(filepath.Join(dir, "*.svg")) if err != nil { diff --git a/internal/handlers/login.go b/internal/handlers/login.go new file mode 100644 index 0000000..f12086f --- /dev/null +++ b/internal/handlers/login.go @@ -0,0 +1,16 @@ +package handlers + +import ( + "git.tijl.dev/tijl/tijl.dev/internal/oidc" + "git.tijl.dev/tijl/tijl.dev/internal/sessions" + "github.com/gofiber/fiber/v2" +) + +func loginHandler(c *fiber.Ctx) error { + _, err := sessions.GetSession(c) + if err == nil { + return c.Redirect("/account") + } else { + return oidc.HandleRedirect(c) + } +} diff --git a/internal/handlers/routes.go b/internal/handlers/routes.go new file mode 100644 index 0000000..601709e --- /dev/null +++ b/internal/handlers/routes.go @@ -0,0 +1,63 @@ +package handlers + +import ( + "io/fs" + "net/http" + + "git.tijl.dev/tijl/tijl.dev/internal/config" + "git.tijl.dev/tijl/tijl.dev/internal/i18n" + "git.tijl.dev/tijl/tijl.dev/internal/oidc" + "git.tijl.dev/tijl/tijl.dev/modules/web" + "git.tijl.dev/tijl/tijl.dev/web/assets" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/filesystem" +) + +func Setup() { + web.RegisterAppSetupFunc(routes) +} + +func routes(app *fiber.App) { + + app.Get("/", func(c *fiber.Ctx) error { + data := *web.Common(c) + data["Title"] = i18n.Translate(c, "home") + return c.Render("index", data, "layouts/base") + }) + app.Get("/blog", func(c *fiber.Ctx) error { + data := *web.Common(c) + data["Title"] = i18n.Translate(c, "blog") + return c.Render("blog", data, "layouts/base") + }) + app.Get("/projects", func(c *fiber.Ctx) error { + data := *web.Common(c) + data["Title"] = i18n.Translate(c, "projects") + return c.Render("projects", data, "layouts/base") + }) + app.Get("/about", func(c *fiber.Ctx) error { + data := *web.Common(c) + data["Title"] = i18n.Translate(c, "about") + return c.Render("about", data, "layouts/base") + }) + + app.Get("/settings", settingsHandler) + app.Get("/login", loginHandler) + app.Get(config.Config.Oidc.CallbackUrl, oidc.HandleCallback) + + /* + Static + */ + static, _ := fs.Sub(assets.StaticEmbed, "static") + app.Use("/static/", filesystem.New(filesystem.Config{ + Root: http.FS(static), + })) + + /* + 404 + */ + app.Use(func(c *fiber.Ctx) error { + data := *web.Common(c) + return c.Render("404", data, "layouts/base") + }) + +} diff --git a/internal/handlers/settings.go b/internal/handlers/settings.go new file mode 100644 index 0000000..cacba07 --- /dev/null +++ b/internal/handlers/settings.go @@ -0,0 +1,19 @@ +package handlers + +import "github.com/gofiber/fiber/v2" + +func settingsHandler(c *fiber.Ctx) error { + lang := c.Query("lang") + redirectURL := c.Query("redirect") + if lang != "" { + c.Cookie(&fiber.Cookie{ + Name: "language", + Value: lang, + Secure: false, + }) + } + if redirectURL != "" { + return c.Redirect(redirectURL) + } + return nil +} diff --git a/internal/web/common.go b/internal/web/common.go new file mode 100644 index 0000000..afe9adf --- /dev/null +++ b/internal/web/common.go @@ -0,0 +1,23 @@ +package web + +import ( + "git.tijl.dev/tijl/tijl.dev/internal/i18n" + "git.tijl.dev/tijl/tijl.dev/internal/sessions" + "git.tijl.dev/tijl/tijl.dev/modules/web" + "github.com/gofiber/fiber/v2" +) + +func Load() { + web.RegisterCommon(func(c *fiber.Ctx, m *fiber.Map) { + _, err := sessions.GetSession(c) + signedIn := false + if err == nil { + signedIn = true + } + + (*m)["SignedIn"] = signedIn + (*m)["Path"] = c.Path() + (*m)["Language"] = i18n.GetLanguage(c) + (*m)["T"] = i18n.GetTranslations(i18n.GetLanguage(c)) + }) +} diff --git a/static/static.go b/locales/embed.go similarity index 75% rename from static/static.go rename to locales/embed.go index c761987..f371a1b 100644 --- a/static/static.go +++ b/locales/embed.go @@ -1,4 +1,4 @@ -package static +package locales import "embed" diff --git a/migrations/embed.go b/migrations/embed.go new file mode 100644 index 0000000..6784cac --- /dev/null +++ b/migrations/embed.go @@ -0,0 +1,6 @@ +package migrations + +import "embed" + +//go:embed * +var Embed embed.FS diff --git a/modules/logger/logger.go b/modules/logger/logger.go index 99e70d4..84baea7 100644 --- a/modules/logger/logger.go +++ b/modules/logger/logger.go @@ -10,8 +10,9 @@ var ( Logger zerolog.Logger ) -func init() { +func Load() { Logger = zerolog.New(os.Stderr).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr}) + Logger.Level(zerolog.DebugLevel) } func Info() *zerolog.Event { diff --git a/modules/web/app.go b/modules/web/app.go new file mode 100644 index 0000000..20fbd91 --- /dev/null +++ b/modules/web/app.go @@ -0,0 +1,21 @@ +package web + +import "github.com/gofiber/fiber/v2" + +/* +Basic system for being able to add routes from other packages +*/ + +var setupFuncs []func(*fiber.App) + +func RegisterAppSetupFunc(function func(*fiber.App)) { + setupFuncs = append(setupFuncs, function) +} + +func Setup(app *fiber.App) { + + for _, function := range setupFuncs { + function(app) + } + +} diff --git a/modules/web/common.go b/modules/web/common.go new file mode 100644 index 0000000..8c68486 --- /dev/null +++ b/modules/web/common.go @@ -0,0 +1,22 @@ +package web + +import ( + log "git.tijl.dev/tijl/tijl.dev/modules/logger" + "github.com/gofiber/fiber/v2" +) + +var commons *fiber.Map = &fiber.Map{} +var commonFunctions []func(*fiber.Ctx, *fiber.Map) + +func RegisterCommon(function func(*fiber.Ctx, *fiber.Map)) { + commonFunctions = append(commonFunctions, function) +} + +func Common(c *fiber.Ctx) *fiber.Map { + theMap := &fiber.Map{} + for _, function := range commonFunctions { + function(c, theMap) + } + log.Debug().Interface("theMap", theMap).Interface("commonFunctions", commonFunctions).Msg("yo") + return theMap +} diff --git a/modules/web/engine.go b/modules/web/engine.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/modules/web/engine.go @@ -0,0 +1 @@ +package web diff --git a/package.json b/package.json index 420c03e..7c908e7 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "scripts": { "dev": "vite", "build": "vite build", - "build:css": "tailwindcss -i ./web/styles.css -o ./static/css/styles.css --minify" + "build:css": "tailwindcss -i ./web/styles.css -o ./web/assets/static/css/styles.css --minify" }, "devDependencies": { "@tailwindcss/typography": "^0.5.14", diff --git a/vite.config.ts b/vite.config.ts index 18c1c73..f9754d5 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,7 +3,7 @@ import { defineConfig } from "vite"; export default defineConfig({ plugins: [], build: { - outDir: "static/js", // Output directory for compiled JS + outDir: "web/assets/static/js", // Output directory for compiled JS rollupOptions: { output: { entryFileNames: "[name].js", diff --git a/web/assets/embed.go b/web/assets/embed.go new file mode 100644 index 0000000..f75d792 --- /dev/null +++ b/web/assets/embed.go @@ -0,0 +1,9 @@ +package assets + +import "embed" + +//go:embed * +var Embed embed.FS + +//go:embed static/* +var StaticEmbed embed.FS diff --git a/static/assets/icons/about.svg b/web/assets/icons/about.svg similarity index 100% rename from static/assets/icons/about.svg rename to web/assets/icons/about.svg diff --git a/static/assets/icons/blog.svg b/web/assets/icons/blog.svg similarity index 100% rename from static/assets/icons/blog.svg rename to web/assets/icons/blog.svg diff --git a/static/assets/icons/home.svg b/web/assets/icons/home.svg similarity index 100% rename from static/assets/icons/home.svg rename to web/assets/icons/home.svg diff --git a/static/assets/icons/language.svg b/web/assets/icons/language.svg similarity index 100% rename from static/assets/icons/language.svg rename to web/assets/icons/language.svg diff --git a/static/assets/icons/login.svg b/web/assets/icons/login.svg similarity index 100% rename from static/assets/icons/login.svg rename to web/assets/icons/login.svg diff --git a/static/assets/icons/projects.svg b/web/assets/icons/projects.svg similarity index 100% rename from static/assets/icons/projects.svg rename to web/assets/icons/projects.svg diff --git a/static/css/.gitkeep b/web/assets/static/css/.gitkeep similarity index 100% rename from static/css/.gitkeep rename to web/assets/static/css/.gitkeep diff --git a/static/assets/favicon.ico b/web/assets/static/favicon.ico similarity index 100% rename from static/assets/favicon.ico rename to web/assets/static/favicon.ico diff --git a/static/fonts/Cantarell-VF.woff2 b/web/assets/static/fonts/Cantarell-VF.woff2 similarity index 100% rename from static/fonts/Cantarell-VF.woff2 rename to web/assets/static/fonts/Cantarell-VF.woff2