From 8c968e21ffe78670871dea395c63fbeca68bc04d Mon Sep 17 00:00:00 2001 From: Blaster4385 Date: Sun, 28 Apr 2024 11:43:08 +0530 Subject: refactor: rewrite in go - Bye Bye JavaScript --- assets/favicon.png | Bin 0 -> 1562 bytes go.mod | 5 +++ go.sum | 2 ++ index.html | 62 -------------------------------- main.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ markdown.js | 25 ------------- styles.css | 94 ------------------------------------------------ templates/index.html | 43 ++++++++++++++++++++++ templates/styles.css | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 243 insertions(+), 181 deletions(-) create mode 100644 assets/favicon.png create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 index.html create mode 100644 main.go delete mode 100644 markdown.js delete mode 100644 styles.css create mode 100644 templates/index.html create mode 100644 templates/styles.css diff --git a/assets/favicon.png b/assets/favicon.png new file mode 100644 index 0000000..7f69b54 Binary files /dev/null and b/assets/favicon.png differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bf74bf0 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module portfolio + +go 1.22.2 + +require github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..008cbd4 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 h1:yEt5djSYb4iNtmV9iJGVday+i4e9u6Mrn5iP64HH5QM= +github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= diff --git a/index.html b/index.html deleted file mode 100644 index 8a71920..0000000 --- a/index.html +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - Blaster4385 - - - - - - - - -
- -
-
- - - - - - \ No newline at end of file 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) + } +} diff --git a/markdown.js b/markdown.js deleted file mode 100644 index 4b9521f..0000000 --- a/markdown.js +++ /dev/null @@ -1,25 +0,0 @@ -function loadMarkdownFile(fileName) { - fetch(`/assets/md/${fileName}`) - .then(response => response.text()) - .then(text => { - document.getElementById('markdown').innerHTML = convertMarkdownToHTML(text); - addBlankTargetToLinks(); - }) - .catch(error => { - console.error('Error loading Markdown file:', error); - }); -} - -function convertMarkdownToHTML(markdown) { - return marked.parse(markdown); -} - -function addBlankTargetToLinks() { - const links = document.querySelectorAll('a'); - links.forEach(link => { - if (!link.getAttribute('href').startsWith('http') && !link.getAttribute('href').startsWith('https')) { - return; - } - link.setAttribute('target', '_blank'); - }); -} \ No newline at end of file diff --git a/styles.css b/styles.css deleted file mode 100644 index 242d9d8..0000000 --- a/styles.css +++ /dev/null @@ -1,94 +0,0 @@ -:root { - --font-family: 'JetBrains Mono', monospace; - --color-background: #282828; - --color-text: #ebdbb2; - --color-accent: #fabd2f; - --color-highlight: #b8bb26; -} - -body { - font-family: var(--font-family); - background-color: var(--color-background); - color: var(--color-text); - margin: 0; - padding: 0; -} - -body::-webkit-scrollbar { - width: 11px; -} - -body::-webkit-scrollbar-track { - background: var(--color-background); -} -body::-webkit-scrollbar-thumb { - background-color: var(--color-text) ; - border-radius: 6px; - border: 3px solid var(--color-background); -} - -body::-webkit-scrollbar-thumb:hover { - border: 1px solid var(--color-background); -} - -.container { - margin: auto; - max-width: 1280px; -} - -h1, -h2, -h3, -h4, -h5, -h6 { - font-weight: bold; - color: var(--color-accent); -} - -a { - color: inherit; - text-decoration: none; -} - -code { - background-color: #444; - display: inline-block; - padding: 8px; - border-radius: 4px; - color: #b8bb26; -} - -#markdown { - margin: 20px; -} - -#header { - text-align: center; - padding: 20px; -} - -ul { - list-style-type: none; - padding-left: 20px; -} - -li::before { - content: "*"; - color: var(--color-highlight); - margin-right: 10px; -} - -li { - margin-bottom: 10px; -} - -p { - font-size: 1.5 rem; - line-height: 1.5; - text-align: justify; -} - -hr { - border: 1px solid var(--color-text); -} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..57948b4 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + {{.Title}} + + + + + +
+ +
+ {{.Body}} +
+
+ + + \ No newline at end of file diff --git a/templates/styles.css b/templates/styles.css new file mode 100644 index 0000000..9f8d7bd --- /dev/null +++ b/templates/styles.css @@ -0,0 +1,94 @@ +:root { + --font-family: 'JetBrains Mono', monospace; + --color-background: #282828; + --color-text: #ebdbb2; + --color-accent: #fabd2f; + --color-highlight: #b8bb26; + } + + body { + font-family: var(--font-family); + background-color: var(--color-background); + color: var(--color-text); + margin: 0; + padding: 0; + } + + body::-webkit-scrollbar { + width: 11px; + } + + body::-webkit-scrollbar-track { + background: var(--color-background); + } + body::-webkit-scrollbar-thumb { + background-color: var(--color-text) ; + border-radius: 6px; + border: 3px solid var(--color-background); + } + + body::-webkit-scrollbar-thumb:hover { + border: 1px solid var(--color-background); + } + + .container { + margin: auto; + max-width: 1280px; + } + + h1, + h2, + h3, + h4, + h5, + h6 { + font-weight: bold; + color: var(--color-accent); + } + + a { + color: inherit; + text-decoration: none; + } + + code { + background-color: #444; + display: inline-block; + padding: 8px; + border-radius: 4px; + color: #b8bb26; + } + + #markdown { + margin: 20px; + } + + #header { + text-align: center; + padding: 20px; + } + + ul { + list-style-type: none; + padding-left: 20px; + } + + li::before { + content: "*"; + color: var(--color-highlight); + margin-right: 10px; + } + + li { + margin-bottom: 10px; + } + + p { + font-size: 1.5 rem; + line-height: 1.5; + text-align: justify; + } + + hr { + border: 1px solid var(--color-text); + } \ No newline at end of file -- cgit v1.2.3-73-gaa49b