diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-02-15 15:31:37 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-02-15 15:32:05 -0500 |
| commit | fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12 (patch) | |
| tree | 80844a62d5d18b30862cfdc610aae88713fe97d9 /http/main.go | |
| parent | d83f4bca3f7026696a41225caac11807ed06fc2f (diff) | |
| download | go-website-fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12.tar.gz go-website-fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12.tar.xz | |
Another re-structure. Deleting code is wonderful.v0.0.12
Diffstat (limited to 'http/main.go')
| -rw-r--r-- | http/main.go | 71 |
1 files changed, 0 insertions, 71 deletions
diff --git a/http/main.go b/http/main.go deleted file mode 100644 index c4726d8..0000000 --- a/http/main.go +++ /dev/null @@ -1,71 +0,0 @@ -// The HTTP package handles serving up web pages from the page interface -// This package assumes you'll be changing directory to where the -// Website's static content, templates and includes are located -package http - -import ( - "fmt" - "log" - "net/http" - "os" - "path/filepath" - - "riedstra.dev/mitch/go-website/local" - "riedstra.dev/mitch/go-website/page" - - "github.com/gorilla/mux" -) - -// NewPage is required to specify how we acquire a new page from a given URL -// since an interface is used, you can swap this out with a different -// library to load pages from different sources -var NewPage func(string) page.Page = func(u string) page.Page { - return local.NewPage(u) -} - -// ReindexRedirectTo is the path that we'll redirect to when a call to rebuild -// index is called -var ReindexRedirectTo = "/fullIndex" - -// Logger can be overridden, errors and access logging are combined. -var Logger = log.New(os.Stderr, "", log.LstdFlags) - -// GetHandler Returns a gorilla/mux Router which implements the http.Handler -// interface, allowing you to pull this website into your other Go applications -func GetHandler() *mux.Router { - if NewPage == nil { - fmt.Fprintln(os.Stderr, "Warning, global NewPage method is not defined!") - } - - r := mux.NewRouter() - r.HandleFunc("/rebuildIndex/", RebuildIndexHandler) - r.PathPrefix("/static/").Handler(StaticHandler()) - r.PathPrefix("/").HandlerFunc(PageHandler) - return r -} - -// PageHandler is usually not called directly from external handlers, but the -// option exists if you're building something custom. -func PageHandler(w http.ResponseWriter, r *http.Request) { - u := r.URL.Path - if u == "/" { - u = "/index" - } - u = filepath.Join(".", u) - - RenderForPath(w, r, u) -} - -func RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - p := NewPage("index") - _ = p.RebuildIndex() - http.Redirect(w, r, ReindexRedirectTo, 302) - } -} - -// StaticHandler simply returns a HTTP handler that looks at the current -// directory and exposes `static` via HTTP `/static` -func StaticHandler() http.Handler { - return http.StripPrefix("/static/", http.FileServer(http.Dir("static"))) -} |
