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.

55 lines
1.4 KiB

package controllers
import (
// 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
console.Debug("Wildcard path: %s", path)
// Is it a blog fragment?
if _, err := models.Posts.LoadFragment(path); err == nil {
PostFragment(w, r)
// No dot-files allowed.
if strings.Contains(path, "/.") {
console.Error("Path '%s' contains a dotfile; forbidden", path)
responses.Forbidden(w, r, "You're not supposed to be here.")
// Resolve the target path.
filepath, err := responses.ResolveFile(path)
if err != nil {
responses.NotFound(w, r)
// Files under /_builtin can only be taken literally.
if strings.HasPrefix(filepath, "_builtin/") {
responses.SendFile(w, r, filepath)
// Is it a Go template?
if strings.HasSuffix(filepath, ".gohtml") {
responses.RenderTemplate(w, r, filepath, nil)
} else if strings.HasSuffix(filepath, ".md") {
responses.RenderMarkdown(w, r, filepath)
responses.SendFile(w, r, filepath)