From 9637b38a3fa92f815c22cc1ea8e30a672155865f Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sat, 24 Mar 2018 13:37:18 -0500 Subject: [PATCH] Static files now served; auth is broken --- src/api/routes/handlers.go | 15 +++++++++++++++ src/api/routes/router.go | 1 - src/api/routes/routes.go | 15 +++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/api/routes/handlers.go b/src/api/routes/handlers.go index 4325a05..7fb35bd 100644 --- a/src/api/routes/handlers.go +++ b/src/api/routes/handlers.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "net/http" + "strings" "github.com/danieljsummers/myPrayerJournal/src/api/data" ) @@ -38,3 +39,17 @@ func journal(w http.ResponseWriter, r *http.Request) { } sendJSON(w, r, reqs) } + +func staticFiles(w http.ResponseWriter, r *http.Request) { + // serve index for known routes handled client-side by the app + for _, prefix := range ClientPrefixes { + log.Print("Checking " + r.URL.Path) + if strings.HasPrefix(r.URL.Path, prefix) { + w.Header().Add("Content-Type", "text/html") + http.ServeFile(w, r, "./public/index.html") + return + } + } + // 404 here is fine; quit hacking, y'all... + http.ServeFile(w, r, "./public"+r.URL.Path) +} diff --git a/src/api/routes/router.go b/src/api/routes/router.go index a5919ab..623f528 100644 --- a/src/api/routes/router.go +++ b/src/api/routes/router.go @@ -62,6 +62,5 @@ func NewRouter(cfg *AuthConfig) *vestigo.Router { router.Add(route.Method, route.Pattern, withAuth(route.Func, cfg)) } } - router.Get("/*", http.FileServer(http.Dir("/public")).ServeHTTP) return router } diff --git a/src/api/routes/routes.go b/src/api/routes/routes.go index ec8551b..69d05e7 100644 --- a/src/api/routes/routes.go +++ b/src/api/routes/routes.go @@ -21,9 +21,20 @@ type Routes []Route var routes = Routes{ Route{ "Journal", - "GET", - "/api/journal", + http.MethodGet, + "/api/journal/", journal, false, }, + // keep this route last + Route{ + "StaticFiles", + http.MethodGet, + "/*", + staticFiles, + true, + }, } + +// ClientPrefixes is a list of known route prefixes handled by the Vue app. +var ClientPrefixes = []string{"/answered", "/journal", "/user"}