summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..b0c0787
--- /dev/null
+++ b/main.go
@@ -0,0 +1,99 @@
+package main
+
+import (
+ "embed"
+ "encoding/base64"
+ "flag"
+ "fmt"
+ "html/template"
+ "log"
+ "net/http"
+
+ "github.com/gomarkdown/markdown"
+ "github.com/gomarkdown/markdown/parser"
+)
+
+//go:embed templates/* assets/*
+var content embed.FS
+
+type Page struct {
+ Title string
+ Body template.HTML
+ CSS template.CSS
+ Favicon template.URL
+}
+
+func main() {
+ port := flag.Int("port", 8080, "Port to run the server on")
+ flag.Parse()
+ addr := fmt.Sprintf(":%d", *port)
+
+ http.HandleFunc("/", handler)
+ log.Printf("Server is running on http://localhost%s\n", addr)
+ log.Fatal(http.ListenAndServe(addr, nil))
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+ path := r.URL.Path
+ if path == "/" {
+ path = "/home"
+ }
+
+ filePath := fmt.Sprintf("assets/md%s.md", path)
+ mdContent, err := content.ReadFile(filePath)
+ if err != nil {
+ http.Error(w, "Page not found", http.StatusNotFound)
+ return
+ }
+
+ cssContent, err := content.ReadFile("templates/styles.css")
+ if err != nil {
+ http.Error(w, "Stylesheet not found", http.StatusInternalServerError)
+ return
+ }
+
+ htmlContent := convertMarkdownToHTML(mdContent)
+
+ faviconBytes, err := content.ReadFile("assets/favicon.png")
+ if err != nil {
+ http.Error(w, "Favicon not found", http.StatusInternalServerError)
+ return
+ }
+ faviconBase64 := fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(faviconBytes))
+
+ page := Page{
+ Title: "Blaster4385",
+ Body: template.HTML(htmlContent),
+ CSS: template.CSS(string(cssContent)),
+ Favicon: template.URL(faviconBase64),
+ }
+
+ w.Header().Set("Content-Type", "text/html")
+ renderTemplate(w, "index.html", page)
+}
+
+func convertMarkdownToHTML(markdownContent []byte) string {
+ mdParser := parser.New()
+
+ html := markdown.ToHTML(markdownContent, mdParser, nil)
+ return string(html)
+}
+
+func renderTemplate(w http.ResponseWriter, tmpl string, p Page) {
+ tmplContent, err := content.ReadFile("templates/" + tmpl)
+ if err != nil {
+ http.Error(w, "Template not found", http.StatusInternalServerError)
+ return
+ }
+
+ t, err := template.New(tmpl).Parse(string(tmplContent))
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ err = t.Execute(w, p)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ }
+}