From 5c9cb6b5cb09b51b2fa18351ec58d53ad82ac425 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Sat, 27 Aug 2016 16:18:16 -0400 Subject: Allow 404s to redirect to index file --- todolist/webapp.go | 73 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/todolist/webapp.go b/todolist/webapp.go index 39d1a9b..5c93e90 100644 --- a/todolist/webapp.go +++ b/todolist/webapp.go @@ -10,7 +10,8 @@ import ( ) const ( - VERSION = "0.2.0" + VERSION = "0.3.0" + S3URL = "https://s3.amazonaws.com/todolist-local/" + VERSION ) type Webapp struct { @@ -27,44 +28,62 @@ func (w *Webapp) Run() { func setupRoutes() *httprouter.Router { router := httprouter.New() - router.GET("/", Scaffold) + router.GET("/", IndexScaffold) router.OPTIONS("/todos", TodoOptions) router.GET("/todos", GetTodos) router.POST("/todos", SaveTodos) + router.NotFound = http.HandlerFunc(RedirectScaffold) return router } -func Scaffold(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func IndexScaffold(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { template := ` - - - - - - - - - - Todolist - - - -
- - - - + + + + + + + Todolist + + + +
+ + + + + ` + fmt.Fprintf(w, template) +} + +func RedirectScaffold(w http.ResponseWriter, r *http.Request) { + template := ` + + + + + + + Todolist + + + +
+ + + + ` fmt.Fprintf(w, template) } func urlFor(file string) string { - return "https://s3.amazonaws.com/todolist-local/" + VERSION + "/" + file + return S3URL + "/" + file +} + +func RedirectToIndex(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, S3URL+r.URL.Path, 301) } func GetTodos(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { -- cgit v1.3