initial commit - added base, need to fix templ errors
This commit is contained in:
156
main.go
Normal file
156
main.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.valxntine.dev/valxntine/blog/models"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
fs := http.FileServer(http.Dir("./static/"))
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
|
||||
mux.HandleFunc("/", handleHome)
|
||||
mux.HandleFunc("/posts/", handlePost)
|
||||
mux.HandleFunc("/tags/", handleTag)
|
||||
|
||||
handler := loggingMiddleware(mux)
|
||||
|
||||
log.Println("Serving on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", handler))
|
||||
}
|
||||
|
||||
func loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%s %s %s", r.Method, r.URL.Path, r.RemoteAddr)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
page := 1
|
||||
if p := r.URL.Query().Get("page"); p != "" {
|
||||
if parsed, err := strconv.Atoi(p); err == nil && parsed > 0 {
|
||||
page = parsed
|
||||
}
|
||||
}
|
||||
|
||||
posts := GetPaginatedPosts(page, 5)
|
||||
total := GetTotalPages(5)
|
||||
|
||||
d := models.Data{
|
||||
Title: "~/valxntine/blog",
|
||||
Subtitle: "Thought from the Forge",
|
||||
Posts: posts,
|
||||
CurrentPage: page,
|
||||
TotalPages: total,
|
||||
}
|
||||
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := templates.PostList(
|
||||
d.Posts,
|
||||
d.CurrentPage,
|
||||
d.TotalPages,
|
||||
).Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := templates.HomePage(d).Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func handlePost(w http.ResponseWriter, r *http.Request) {
|
||||
slug := strings.TrimPrefix(r.URL.Path, "/posts/")
|
||||
if slug == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
post := GetPostBySlug(slug)
|
||||
if post == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := templates.PostDetail(*post).Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
// err := templates.Layout(post.Title + " - ~/valxntine/blog") {
|
||||
// @templates.Header("~/valxntine/blog", "Thoughts from the Forge")
|
||||
// @templates.Nav()
|
||||
// <main>
|
||||
// @templates.PostDetail(*post)
|
||||
// </main>
|
||||
// @templates.Footer()
|
||||
// }.Render(r.Context(), w)
|
||||
// if err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
func handleTag(w http.ResponseWriter, r *http.Request) {
|
||||
tag := strings.TrimPrefix(r.URL.Path, "/tags/")
|
||||
if tag == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
page := 1
|
||||
if p := r.URL.Query().Get("page"); p != "" {
|
||||
if parsed, err := strconv.Atoi(p); err == nil && parsed > 0 {
|
||||
page = parsed
|
||||
}
|
||||
}
|
||||
|
||||
posts := GetPostsByTag(tag, page, 5)
|
||||
total := GetTotalPagesByTag(tag, 5)
|
||||
|
||||
d := models.Data{
|
||||
Title: "~/valxntine/blog",
|
||||
Subtitle: fmt.Sprintf("Posts tagged with '%s'", tag),
|
||||
Posts: posts,
|
||||
CurrentPage: page,
|
||||
TotalPages: total,
|
||||
}
|
||||
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := templates.PostList(d.Posts, d.CurrentPage, d.TotalPages).Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := templates.HomePage(d).Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user