Add static serving
This commit is contained in:
36
web/root.route.go
Normal file
36
web/root.route.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
if path == "/" {
|
||||
path = "/index"
|
||||
}
|
||||
|
||||
templatePath := "templates" + path + ".html"
|
||||
if _, err := os.Stat(templatePath); err == nil {
|
||||
tmpl, err := template.ParseFiles("templates/base.html", templatePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing template: ", err)
|
||||
http.Error(w, "Error parsing template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tmpl.Execute(w, nil)
|
||||
return
|
||||
}
|
||||
|
||||
staticPath := "static" + path
|
||||
if _, err := os.Stat(staticPath); err == nil {
|
||||
http.ServeFile(w, r, staticPath)
|
||||
return
|
||||
}
|
||||
|
||||
// Return a 404 if the file does not exist in either directory.
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
Reference in New Issue
Block a user