A web blog and personal homepage engine written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
988 B

  1. package controllers
  2. import (
  3. "log"
  4. "net/http"
  5. "strings"
  6. "git.kirsle.net/apps/gophertype/pkg/responses"
  7. )
  8. // CatchAllHandler handles the "/" and wildcard routes that are not
  9. // picked up by any other controllers. It serves static files and Go
  10. // templates from the builtin web root or the user root.
  11. func CatchAllHandler(w http.ResponseWriter, r *http.Request) {
  12. path := r.URL.Path
  13. log.Printf("Wildcard path: %s", path)
  14. // Resolve the target path.
  15. filepath, err := responses.ResolveFile(path)
  16. if err != nil {
  17. responses.Panic(w, http.StatusNotFound, "ResolveFile: "+err.Error())
  18. return
  19. }
  20. // Files under /_builtin can only be taken literally.
  21. if strings.HasPrefix(filepath, "_builtin/") {
  22. responses.SendFile(w, r, filepath)
  23. return
  24. }
  25. // Is it a Go template?
  26. if strings.HasSuffix(filepath, ".gohtml") {
  27. log.Printf("Resolved to Go Template path %s", filepath)
  28. responses.RenderTemplate(w, filepath, nil)
  29. return
  30. }
  31. http.ServeFile(w, r, "pvt-www/"+filepath)
  32. }