diff --git a/handlers.go b/handlers.go index cc20c88..8ff99f6 100644 --- a/handlers.go +++ b/handlers.go @@ -9,10 +9,6 @@ import ( "github.com/n1try/wakapi/models" ) -func Authenticate(w http.ResponseWriter, r *http.Request) { - -} - func HeartbeatHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.WriteHeader(415) diff --git a/main.go b/main.go index 9b42c26..7cdfc1d 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,9 @@ import ( "strconv" "time" + "github.com/codegangsta/negroni" + "github.com/gorilla/mux" + _ "github.com/go-sql-driver/mysql" "github.com/n1try/wakapi/models" "github.com/n1try/wakapi/services" @@ -40,17 +43,30 @@ func main() { // Init Services HeartbeatSrvc = services.HeartbeatService{db} - // Define Routes - http.HandleFunc("/api/heartbeat", HeartbeatHandler) + // Setup Routing + router := mux.NewRouter() + apiRouter := mux.NewRouter().PathPrefix("/api").Subrouter() + n := negroni.Classic() + n.UseHandler(router) + + // API Routes + heartbeats := apiRouter.Path("/heartbeat").Subrouter() + heartbeats.Methods("POST").HandlerFunc(HeartbeatHandler) + + // Sub-Routes Setup + router.PathPrefix("/api").Handler(negroni.Classic().With( + negroni.HandlerFunc(AuthenticateMiddleware), + negroni.Wrap(apiRouter), + )) // Listen HTTP portString := ":" + strconv.Itoa(config.Port) s := &http.Server{ + Handler: router, Addr: portString, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, } - fmt.Printf("Listening on %+s\n", portString) s.ListenAndServe() } diff --git a/middlewares.go b/middlewares.go new file mode 100644 index 0000000..2c79c64 --- /dev/null +++ b/middlewares.go @@ -0,0 +1,7 @@ +package main + +import "net/http" + +func AuthenticateMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + next(w, r) +}