the rest
This commit is contained in:
54
internal/handlers/home.go
Normal file
54
internal/handlers/home.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"decor-by-hannahs/internal/db"
|
||||
)
|
||||
|
||||
func HomeHandler(q *db.Queries, tmpl *template.Template) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("HomeHandler called: method=%s path=%s", r.Method, r.URL.Path)
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var photos []Photo
|
||||
|
||||
carouselDir := "assets/carousel-photos"
|
||||
entries, err := os.ReadDir(carouselDir)
|
||||
if err == nil {
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
ext := filepath.Ext(entry.Name())
|
||||
if ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif" {
|
||||
photos = append(photos, Photo{
|
||||
URL: "/assets/carousel-photos/" + entry.Name(),
|
||||
Caption: "",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(photos, func(i, j int) bool {
|
||||
return photos[i].URL < photos[j].URL
|
||||
})
|
||||
|
||||
type data struct {
|
||||
Title string
|
||||
Photos []Photo
|
||||
ActivePage string
|
||||
}
|
||||
if err := tmpl.ExecuteTemplate(w, "home.tmpl", data{Title: "Home", Photos: photos, ActivePage: "home"}); err != nil {
|
||||
log.Printf("Error executing home.tmpl: %v", err)
|
||||
http.Error(w, "Failed to render page", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user