summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaster4385 <blaster4385@tablaster.dev>2024-04-28 11:43:08 +0530
committerBlaster4385 <blaster4385@tablaster.dev>2024-04-29 21:04:10 +0530
commit8c968e21ffe78670871dea395c63fbeca68bc04d (patch)
treefd26b2c8c6bf8ad0d109a42ab551db1edad84f45
parentcf454871b15fcfccd42adfac762deabe3513d2ad (diff)
refactor: rewrite in go
- Bye Bye JavaScript
-rw-r--r--assets/favicon.pngbin0 -> 1562 bytes
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--index.html62
-rw-r--r--main.go99
-rw-r--r--markdown.js25
-rw-r--r--styles.css94
-rw-r--r--templates/index.html43
-rw-r--r--templates/styles.css94
9 files changed, 243 insertions, 181 deletions
diff --git a/assets/favicon.png b/assets/favicon.png
new file mode 100644
index 0000000..7f69b54
--- /dev/null
+++ b/assets/favicon.png
Binary files 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 @@
-<!DOCTYPE html>
-<html lang="en">
-
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <!-- Meta tags for SEO -->
- <meta name="description"
- content="Venkatesh Chaturvedi, passionate about Linux and FOSS.">
- <meta name="keywords"
- content="Venkatesh Chaturvedi, Blaster4385, tablaster, FOSS, CLI tools, Linux, Android, Open Source, git">
- <meta name="author" content="Venkatesh Chaturvedi">
- <meta name="robots" content="index, follow">
- <!-- Open Graph meta tags for social sharing -->
- <meta property="og:title" content="Blaster4385">
- <meta property="og:description"
- content="Venkatesh Chaturvedi, passionate about Linux and FOSS.">
- <meta property="og:image" content="https://github.com/Blaster4385.png">
- <meta property="og:url" content="https://tablaster.dev">
- <meta property="og:type" content="website">
- <!-- Twitter Card meta tags for social sharing -->
- <meta name="twitter:card" content="summary">
- <meta name="twitter:creator" content="@Blaster4385">
- <meta name="twitter:title" content="Blaster4385">
- <meta name="twitter:description"
- content="Venkatesh Chaturvedi, passionate about Linux and FOSS.">
- <meta name="twitter:image" content="https://github.com/Blaster4385.png">
- <title>Blaster4385</title>
- <link rel="icon" type="image/png" href="https://github.com/Blaster4385.png">
- <link rel="preconnect" href="https://fonts.gstatic.com">
- <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
- <link rel="stylesheet" href="styles.css">
- <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
-</head>
-
-<body>
- <main class="container">
- <header id="header">
- <h1><a href="/">$ tablaster.dev</a></h1>
- </header>
- <div id="markdown"></div>
- </main>
-
- <script src="markdown.js"></script>
- <script>
- document.addEventListener('DOMContentLoaded', function () {
- // Get the pathname of the URL
- const path = window.location.pathname;
- // Remove leading slash if present
- const trimmedPath = path.replace(/^\/|\/$/g, '');
-
- if (trimmedPath === '') {
- loadMarkdownFile('home.md');
- return;
- }
- // Load the markdown file based on the path
- loadMarkdownFile(`${trimmedPath}.md`);
- });
- </script>
-</body>
-
-</html> \ 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 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <!-- Meta tags for SEO -->
+ <meta name="description" content="Venkatesh Chaturvedi, passionate about Linux and FOSS.">
+ <meta name="keywords"
+ content="Venkatesh Chaturvedi, Blaster4385, tablaster, FOSS, CLI tools, Linux, Android, Open Source, git">
+ <meta name="author" content="Venkatesh Chaturvedi">
+ <meta name="robots" content="index, follow">
+ <!-- Open Graph meta tags for social sharing -->
+ <meta property="og:title" content="Blaster4385">
+ <meta property="og:description" content="Venkatesh Chaturvedi, passionate about Linux and FOSS.">
+ <meta property="og:image" content="https://github.com/Blaster4385.png">
+ <meta property="og:url" content="https://tablaster.dev">
+ <meta property="og:type" content="website">
+ <!-- Twitter Card meta tags for social sharing -->
+ <meta name="twitter:card" content="summary">
+ <meta name="twitter:creator" content="@Blaster4385">
+ <meta name="twitter:title" content="Blaster4385">
+ <meta name="twitter:description" content="Venkatesh Chaturvedi, passionate about Linux and FOSS.">
+ <meta name="twitter:image" content="https://github.com/Blaster4385.png">
+ <title>{{.Title}}</title>
+ <style>
+ {{.CSS}}
+ </style>
+ <link rel="icon" type="image/png" href="{{.Favicon}}">
+</head>
+
+<body>
+ <main class="container">
+ <header id="header">
+ <h1><a href="/">$ tablaster.dev</a></h1>
+ </header>
+ <div id="markdown">
+ {{.Body}}
+ </div>
+ </main>
+</body>
+
+</html> \ 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