gophertype/pkg/controllers/static_files.go

40 lines
988 B
Go

package controllers
import (
"log"
"net/http"
"strings"
"git.kirsle.net/apps/gophertype/pkg/responses"
)
// CatchAllHandler handles the "/" and wildcard routes that are not
// picked up by any other controllers. It serves static files and Go
// templates from the builtin web root or the user root.
func CatchAllHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
log.Printf("Wildcard path: %s", path)
// Resolve the target path.
filepath, err := responses.ResolveFile(path)
if err != nil {
responses.Panic(w, http.StatusNotFound, "ResolveFile: "+err.Error())
return
}
// Files under /_builtin can only be taken literally.
if strings.HasPrefix(filepath, "_builtin/") {
responses.SendFile(w, r, filepath)
return
}
// Is it a Go template?
if strings.HasSuffix(filepath, ".gohtml") {
log.Printf("Resolved to Go Template path %s", filepath)
responses.RenderTemplate(w, filepath, nil)
return
}
http.ServeFile(w, r, "pvt-www/"+filepath)
}