Auth middleware.

This commit is contained in:
Ferdinand Mütsch 2019-05-05 23:23:54 +02:00
parent 0bd71b7708
commit 6e7f47dc79
3 changed files with 26 additions and 7 deletions

View File

@ -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)

22
main.go
View File

@ -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()
}

7
middlewares.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "net/http"
func AuthenticateMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
next(w, r)
}