From 48dbc0bbed7442e60e20df092f3ba7c7fbf72d3b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 2 Apr 2024 12:23:37 +0200 Subject: [PATCH] Simplified server initialization --- App.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 37 ++--------------- public/app.css | 48 ++++++++++++++++++++++ public/app.html | 4 +- public/body.html | 58 +------------------------- 5 files changed, 158 insertions(+), 92 deletions(-) create mode 100644 App.go diff --git a/App.go b/App.go new file mode 100644 index 0000000..99fe173 --- /dev/null +++ b/App.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "git.akyoto.dev/go/markdown" + "git.akyoto.dev/go/web" + "git.akyoto.dev/go/web/send" +) + +type App struct { + html string + posts map[string]string +} + +func (app *App) Init() { + app.html = mustLoadClean("public/app.html") + css := mustLoadClean("public/app.css") + body := mustLoadClean("public/body.html") + app.html = strings.Replace(app.html, "{head}", fmt.Sprintf("", css), 1) + app.html = strings.Replace(app.html, "{body}", body, 1) + app.posts = loadPosts("posts") +} + +func (app *App) Run() { + s := web.NewServer() + + s.Use(func(ctx web.Context) error { + defer func() { + err := recover() + + if err != nil { + fmt.Println("Recovered panic:", err) + } + }() + + return ctx.Next() + }) + + render := func(ctx web.Context, content string) error { + return send.HTML(ctx, strings.Replace(app.html, "{content}", content, 1)) + } + + s.Get("/", func(ctx web.Context) error { + return render(ctx, markdown.Render("# Frontpage")) + }) + + s.Get("/:post", func(ctx web.Context) error { + post := ctx.Request().Param("post") + return render(ctx, app.posts[post]) + }) + + s.Run(":8080") +} + +func mustLoadClean(path string) string { + data := mustLoad(path) + data = strings.ReplaceAll(data, "\t", "") + data = strings.ReplaceAll(data, "\n", "") + return data +} + +func mustLoad(path string) string { + dataBytes, err := os.ReadFile(path) + + if err != nil { + panic(err) + } + + return string(dataBytes) +} + +func loadPosts(directory string) map[string]string { + entries, err := os.ReadDir(directory) + + if err != nil { + panic(err) + } + + posts := map[string]string{} + + for _, entry := range entries { + fileName := entry.Name() + + if !strings.HasSuffix(fileName, ".md") { + continue + } + + baseName := strings.TrimSuffix(fileName, ".md") + content := mustLoad("posts/" + fileName) + + if strings.HasPrefix(content, "---\n") { + end := strings.Index(content[4:], "---\n") + 4 + content = content[end+4:] + } + + posts[baseName] = markdown.Render(content) + } + + return posts +} diff --git a/main.go b/main.go index 3fed544..a10028f 100644 --- a/main.go +++ b/main.go @@ -1,38 +1,7 @@ package main -import ( - "fmt" - "os" - "strings" - - "git.akyoto.dev/go/web" - "git.akyoto.dev/go/web/send" -) - func main() { - html := mustLoad("public/app.html") - css := mustLoad("public/app.css") - body := mustLoad("public/body.html") - layout := strings.Replace(html, "%head%", fmt.Sprintf("", css), 1) - - s := web.NewServer() - - s.Get("/", func(ctx web.Context) error { - return send.HTML(ctx, strings.Replace(layout, "%body%", body, 1)) - }) - - s.Run(":8080") -} - -func mustLoad(path string) string { - dataBytes, err := os.ReadFile(path) - - if err != nil { - panic(err) - } - - data := string(dataBytes) - data = strings.ReplaceAll(data, "\t", "") - data = strings.ReplaceAll(data, "\n", "") - return data + app := App{} + app.Init() + app.Run() } diff --git a/public/app.css b/public/app.css index 1e0e65d..a4bb874 100644 --- a/public/app.css +++ b/public/app.css @@ -138,6 +138,54 @@ th:empty { } } +header, +main, +footer { + width: 100%; + max-width: var(--max-width); + padding: var(--padding); +} + +header { + max-width: calc(var(--max-width) + 4rem); +} + +main { + flex: 1; +} + +footer { + display: flex; + flex-direction: column; + align-items: center; + font-size: 0.75em; + color: var(--grey-color); +} + +nav { + display: flex; + gap: 1rem; + list-style-type: none; + align-items: center; +} + +.nav-item { + color: var(--grey-color); +} + +.nav-item:hover { + text-decoration: none; +} + +.title { + color: var(--main-color); +} + +time { + font-size: 0.8rem; + color: var(--grey-color); +} + code { font-family: var(--font-family-mono); word-break: break-word; diff --git a/public/app.html b/public/app.html index 0c4b284..cf4796c 100644 --- a/public/app.html +++ b/public/app.html @@ -4,9 +4,9 @@ - %head% + {head} - %body% + {body} \ No newline at end of file diff --git a/public/body.html b/public/body.html index d320562..bff51e6 100644 --- a/public/body.html +++ b/public/body.html @@ -1,56 +1,3 @@ - - - - - -
- Test. -
- \ No newline at end of file + {content} + \ No newline at end of file