72 lines
2.7 KiB
Go
72 lines
2.7 KiB
Go
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)
|
|
}
|