forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pages.go
26 lines (23 loc) · 790 Bytes
/
pages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package server
import (
"github.com/dimfeld/httptreemux"
"github.com/kabukky/journey/filenames"
"github.com/kabukky/journey/helpers"
"net/http"
"path/filepath"
"strings"
)
func pagesHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
path := filepath.Join(filenames.PagesFilepath, params["filepath"])
// If the path points to a directory, add a trailing slash to the path (needed if the page loads relative assets).
if helpers.IsDirectory(path) && !strings.HasSuffix(r.RequestURI, "/") {
http.Redirect(w, r, r.RequestURI+"/", 301)
return
}
http.ServeFile(w, r, path)
return
}
func InitializePages(router *httptreemux.TreeMux) {
// For serving standalone projects or pages saved in in content/pages
router.GET("/pages/*filepath", pagesHandler)
}