Commit before this refactoring with partials and stuff gets out of control
This commit is contained in:
48
main.go
48
main.go
@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -102,6 +103,12 @@ func NewServer() *Server {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse templates: %v", err)
|
||||
}
|
||||
// Optionally parse partials if any exist
|
||||
if matches, _ := filepath.Glob("templates/partials/*.html"); len(matches) > 0 {
|
||||
if _, err := templates.ParseFiles(matches...); err != nil {
|
||||
log.Fatalf("Failed to parse partial templates: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &Server{
|
||||
config: config,
|
||||
@ -109,6 +116,21 @@ func NewServer() *Server {
|
||||
}
|
||||
}
|
||||
|
||||
// cleanupOldSubmissions periodically purges stale rate-limit entries
|
||||
func cleanupOldSubmissions() {
|
||||
for {
|
||||
time.Sleep(10 * time.Minute)
|
||||
mu.Lock()
|
||||
cutoff := time.Now().Add(-submissionCooldown)
|
||||
for ip, t := range lastSubmissionTime {
|
||||
if t.Before(cutoff) {
|
||||
delete(lastSubmissionTime, ip)
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// createPageData creates PageData with the given title and current page
|
||||
func (s *Server) createPageData(title, currentPage string) PageData {
|
||||
return PageData{
|
||||
@ -208,7 +230,7 @@ func (s *Server) handleContactForm(w http.ResponseWriter, r *http.Request, data
|
||||
mu.Unlock()
|
||||
|
||||
// On success, render success message
|
||||
data.SuccessMessage = "Bedankt voor uw bericht! We nemen zo snel mogelijk contact met u op."
|
||||
data.SuccessMessage = "Bedankt voor uw bericht! Ik neem zo snel mogelijk contact met u op."
|
||||
data.FormData = ContactForm{} // Clear form data
|
||||
s.renderTemplate(w, "contact.html", *data)
|
||||
}
|
||||
@ -238,7 +260,7 @@ func (s *Server) sendToTelegram(form ContactForm) error {
|
||||
payload := map[string]interface{}{
|
||||
"chat_id": s.config.TelegramChatID,
|
||||
"text": message,
|
||||
"parse_mode": "Markdown",
|
||||
"parse_mode": "MarkdownV2",
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
@ -362,7 +384,7 @@ func (s *Server) linuxHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) setupRoutes() {
|
||||
// Static files
|
||||
fs := http.FileServer(http.Dir("static/"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", cacheControlMiddleware(fs)))
|
||||
|
||||
// Page routes
|
||||
http.HandleFunc("/", s.homeHandler)
|
||||
@ -376,7 +398,27 @@ func (s *Server) setupRoutes() {
|
||||
func main() {
|
||||
server := NewServer()
|
||||
server.setupRoutes()
|
||||
// Start background cleanup for rate limiting map
|
||||
go cleanupOldSubmissions()
|
||||
|
||||
log.Printf("Server starting on %s", server.config.Port)
|
||||
log.Fatal(http.ListenAndServe(server.config.Port, nil))
|
||||
}
|
||||
|
||||
// cacheControlMiddleware sets Cache-Control headers for static assets
|
||||
func cacheControlMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
switch {
|
||||
case strings.HasSuffix(path, ".css") || strings.HasSuffix(path, ".js") ||
|
||||
strings.HasSuffix(path, ".png") || strings.HasSuffix(path, ".jpg") ||
|
||||
strings.HasSuffix(path, ".jpeg") || strings.HasSuffix(path, ".webp") ||
|
||||
strings.HasSuffix(path, ".svg") || strings.HasSuffix(path, ".ico") ||
|
||||
strings.HasSuffix(path, ".woff2"):
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
default:
|
||||
w.Header().Set("Cache-Control", "public, max-age=300")
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user