1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

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" "github.com/n1try/wakapi/models"
) )
func Authenticate(w http.ResponseWriter, r *http.Request) {
}
func HeartbeatHandler(w http.ResponseWriter, r *http.Request) { func HeartbeatHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
w.WriteHeader(415) w.WriteHeader(415)

22
main.go
View File

@ -9,6 +9,9 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/n1try/wakapi/models" "github.com/n1try/wakapi/models"
"github.com/n1try/wakapi/services" "github.com/n1try/wakapi/services"
@ -40,17 +43,30 @@ func main() {
// Init Services // Init Services
HeartbeatSrvc = services.HeartbeatService{db} HeartbeatSrvc = services.HeartbeatService{db}
// Define Routes // Setup Routing
http.HandleFunc("/api/heartbeat", HeartbeatHandler) 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 // Listen HTTP
portString := ":" + strconv.Itoa(config.Port) portString := ":" + strconv.Itoa(config.Port)
s := &http.Server{ s := &http.Server{
Handler: router,
Addr: portString, Addr: portString,
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
} }
fmt.Printf("Listening on %+s\n", portString) fmt.Printf("Listening on %+s\n", portString)
s.ListenAndServe() 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)
}