65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
// server.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var globalRoutes RouteMap
|
|
|
|
// StartServer initializes the routing and starts the HTTP listener.
|
|
func StartServer() {
|
|
InitCache()
|
|
|
|
// Initial Scan
|
|
var err error
|
|
globalRoutes, err = ScanContent("content")
|
|
if err != nil {
|
|
log.Fatalf("Failed to scan content: %v", err)
|
|
}
|
|
|
|
// SINGLE ENTRY POINT
|
|
http.HandleFunc("/", handleRequest)
|
|
|
|
fmt.Println("Server is online at http://localhost:8080")
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|
// 1. Handle Test Mode (Re-scan on every request)
|
|
if TestMode {
|
|
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
|
|
routes, err := ScanContent("content")
|
|
if err == nil {
|
|
globalRoutes = routes
|
|
}
|
|
}
|
|
|
|
// 2. Normalize Request Path
|
|
reqPath := strings.ToLower(r.URL.Path)
|
|
|
|
// 3. Look up in Route Map
|
|
file, found := globalRoutes[reqPath]
|
|
if !found {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
// 4. Get Content Path (Ensures cache is fresh)
|
|
cachePath, err := GetContentPath(file)
|
|
if err != nil {
|
|
log.Printf("Error serving %s: %v", file.OriginalPath, err)
|
|
http.Error(w, "Internal Server Error", 500)
|
|
return
|
|
}
|
|
|
|
// 5. Serve File
|
|
// http.ServeFile handles Content-Type, Range requests, and Caching headers automatically.
|
|
http.ServeFile(w, r, cachePath)
|
|
}
|