Create go backend for site SSR and Dockerfile for dockerization

This commit is contained in:
2026-03-10 18:44:07 -05:00
parent 92600c3159
commit b47820eeea
9 changed files with 336 additions and 0 deletions

71
static.go Normal file
View File

@@ -0,0 +1,71 @@
package main
import (
"errors"
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles(filepath.Join(contentDir, "templates/layout.gohtml"), filepath.Join(contentDir, "templates/page.gohtml")))
contents, err := os.ReadFile(filepath.Join(contentDir, "pages/home.md"))
if err != nil {
sendError(w, http.StatusInternalServerError, "There was a problem reading the data for that page")
return
}
contents_html := mdToHTML(contents)
data := PageData{
Header: config.HeaderData,
Body: template.HTML(contents_html),
}
tmpl.ExecuteTemplate(w, "layout", data)
}
func staticPageHandler(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(filepath.Join(contentDir, "pages", r.PathValue("page")+".md")); err == nil {
fmt.Println("Static Page Handler!", r.PathValue("page"))
} else if errors.Is(err, os.ErrNotExist) {
sendError(w, http.StatusNotFound, "That page could not be found.")
return
} else {
sendError(w, http.StatusInternalServerError, "There was a problem statting the file for that page: "+err.Error())
return
}
tmpl := template.Must(template.ParseFiles(filepath.Join(contentDir, "templates/layout.gohtml"), filepath.Join(contentDir, "templates/page.gohtml")))
contents, err := os.ReadFile(filepath.Join(contentDir, "pages", r.PathValue("page")+".md"))
if err != nil {
sendError(w, http.StatusInternalServerError, "There was a problem reading the file for that page: "+err.Error())
return
}
contents_html := mdToHTML(contents)
data := PageData{
Header: config.HeaderData,
Body: template.HTML(contents_html),
}
tmpl.ExecuteTemplate(w, "layout", data)
}
func staticCategoryHandler(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(filepath.Join(contentDir, "pages", r.PathValue("category"), r.PathValue("page")+".md")); err == nil {
} else if errors.Is(err, os.ErrNotExist) {
sendError(w, http.StatusNotFound, "That page could not be found.")
return
} else {
sendError(w, http.StatusInternalServerError, "There was a problem statting the file for that page: "+err.Error())
return
}
tmpl := template.Must(template.ParseFiles(filepath.Join(contentDir, "templates/layout.gohtml"), filepath.Join(contentDir, "templates/page.gohtml")))
contents, err := os.ReadFile(filepath.Join(contentDir, "pages", r.PathValue("category"), r.PathValue("page")+".md"))
if err != nil {
sendError(w, http.StatusInternalServerError, "There was a problem reading the file for that page: "+err.Error())
return
}
contents_html := mdToHTML(contents)
data := PageData{
Header: config.HeaderData,
Body: template.HTML(contents_html),
}
tmpl.ExecuteTemplate(w, "layout", data)
}