Switched to vestigo router

Also moved db reference to data module; it now starts, but doesn't serve index.html for root yet
This commit is contained in:
Daniel J. Summers
2018-03-22 22:11:38 -05:00
parent b248f7ca7f
commit 59b5574b16
5 changed files with 43 additions and 52 deletions

View File

@@ -1,13 +1,11 @@
package routes
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"github.com/danieljsummers/myPrayerJournal/src/api/data"
"github.com/julienschmidt/httprouter"
)
/* Support */
@@ -32,9 +30,9 @@ func sendJSON(w http.ResponseWriter, r *http.Request, result interface{}) {
/* Handlers */
func journal(w http.ResponseWriter, r *http.Request, _ httprouter.Params, db *sql.DB) {
func journal(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(ContextUserKey)
reqs := data.Journal(db, user.(string))
reqs := data.Journal(user.(string))
if reqs == nil {
reqs = []data.JournalRequest{}
}

View File

@@ -2,13 +2,11 @@ package routes
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"
"github.com/auth0-community/go-auth0"
"github.com/julienschmidt/httprouter"
"github.com/husobee/vestigo"
"gopkg.in/square/go-jose.v2"
)
@@ -19,27 +17,14 @@ type AuthConfig struct {
ClientSecret string `json:"secret"`
}
// DBHandler extends httprouter's handler with a DB instance.
type DBHandler func(http.ResponseWriter, *http.Request, httprouter.Params, *sql.DB)
//type APIHandler func(http.ResponseWriter, *http.Request, httprouter.Params, *sql.DB, string)
// ContextKey is the type of key used in our contexts.
type ContextKey string
// ContextUserKey is the key for the current user in the context.
const ContextUserKey ContextKey = "user"
func withDB(next DBHandler, db *sql.DB) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(60*time.Second))
defer cancel()
next(w, r.WithContext(ctx), p, db)
}
}
func withAuth(next DBHandler, cfg *AuthConfig) DBHandler {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params, db *sql.DB) {
func withAuth(next http.HandlerFunc, cfg *AuthConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
secret := []byte(cfg.ClientSecret)
secretProvider := auth0.NewKeyProvider(secret)
audience := []string{fmt.Sprintf("https://%s/userinfo", cfg.Domain)}
@@ -61,22 +46,22 @@ func withAuth(next DBHandler, cfg *AuthConfig) DBHandler {
}
r = r.WithContext(context.WithValue(r.Context(), ContextUserKey, values["sub"]))
// TODO pass the user ID (sub) along; this -> doesn't work | r.Header.Add("user-id", token.Claims("sub"))
next(w, r, p, db)
next(w, r)
}
}
}
// NewRouter returns a configured router to handle all incoming requests.
func NewRouter(db *sql.DB, cfg *AuthConfig) *httprouter.Router {
router := httprouter.New()
func NewRouter(cfg *AuthConfig) *vestigo.Router {
router := vestigo.NewRouter()
for _, route := range routes {
if route.IsPublic {
router.Handle(route.Method, route.Pattern, withDB(route.Func, db))
router.Add(route.Method, route.Pattern, route.Func)
} else {
router.Handle(route.Method, route.Pattern, withDB(withAuth(route.Func, cfg), db))
router.Add(route.Method, route.Pattern, withAuth(route.Func, cfg))
}
}
// router.ServeFiles("/*filepath", http.Dir("/public"))
router.Get("/*", http.FileServer(http.Dir("/public")).ServeHTTP)
return router
}

View File

@@ -1,12 +1,16 @@
// Package routes contains endpoint handlers for the myPrayerJournal API.
package routes
import (
"net/http"
)
// Route is a route served in the application.
type Route struct {
Name string
Method string
Pattern string
Func DBHandler
Func http.HandlerFunc
IsPublic bool
}