aboutsummaryrefslogtreecommitdiff
path: root/http
diff options
context:
space:
mode:
Diffstat (limited to 'http')
-rw-r--r--http/main.go71
-rw-r--r--http/render.go62
2 files changed, 0 insertions, 133 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")))
-}
diff --git a/http/render.go b/http/render.go
deleted file mode 100644
index b73ae76..0000000
--- a/http/render.go
+++ /dev/null
@@ -1,62 +0,0 @@
-package http
-
-import (
- "net/http"
- "path/filepath"
- "strings"
-)
-
-func Render(w http.ResponseWriter, r *http.Request,
- path string, vars map[string]interface{}, statusCode int) {
-
- u := r.URL.Path
- if u == "/" {
- u = "/index"
- }
- u = filepath.Join(".", u)
-
- // Sepcifically use the specified path for the page
- p := NewPage(path)
-
- if vars != nil {
- p.SetVars(vars)
- }
-
- err := p.Render(w)
- if err != nil {
- if strings.HasSuffix(err.Error(), "no such file or directory") {
- Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, 404, u)
- p = NewPage("404")
- w.WriteHeader(404)
- err := p.Render(w)
- if err != nil {
- Logger.Printf("%s %s path: %s while trying 404: %s", r.RemoteAddr, r.Method, u, err)
- http.Error(w, "Internal server error", 500)
- return
- }
- return
- } else {
- Logger.Printf("%s %s path: %s encountered: %s", r.RemoteAddr, r.Method, u, err)
- http.Error(w, "Internal server error", 500)
- return
- }
- }
-
- Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, statusCode, u)
-
-}
-
-// RenderWithVars allows you to specify a specific page and whether or not
-// you wish to override vars. If left nil they will not be overridden.
-// Also see RenderForPath if you don't need to override them
-func RenderWithVars(w http.ResponseWriter, r *http.Request,
- path string, vars map[string]interface{}) {
-
- Render(w, r, path, vars, 200)
-}
-
-// RenderForPath takes the path to a page and finish up the rendering
-// Allowing you to place logic on what page is rendered by your handlers
-func RenderForPath(w http.ResponseWriter, r *http.Request, path string) {
- RenderWithVars(w, r, path, nil)
-}