Skip to content

Commit

Permalink
Merge branch 'release' into 'main'
Browse files Browse the repository at this point in the history
Release

See merge request mawthuq-software/wireguard-manager-and-api!30
  • Loading branch information
RaspberryTech01 committed Feb 12, 2022
2 parents 5ee8ed7 + 43ba226 commit c1a1ed2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/api/router/headerMiddleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package router

import (
"net/http"
)

func setHeader(hand http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
res.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS")
res.Header().Set("Access-Control-Allow-Origin", "*")

if req.Method == "OPTIONS" {
res.WriteHeader(http.StatusOK)
return
}
hand.ServeHTTP(res, req)
})
}

func setCorsHeader(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
res.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS")
res.Header().Set("Access-Control-Allow-Origin", "*")

if req.Method == "OPTIONS" {
res.WriteHeader(http.StatusOK)
return
} else {
res.WriteHeader(http.StatusMethodNotAllowed)
}
}
9 changes: 8 additions & 1 deletion src/api/router/router.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package router

import "github.com/gorilla/mux"
import (
"net/http"

"github.com/gorilla/mux"
)

func NewRouter() *mux.Router {
router := mux.NewRouter() //Router for routes
router.Use(setHeader) //need to allow CORS and OPTIONS
router.Use(authMiddleware)

manager := router.PathPrefix("/manager").Subrouter() //main subrouter
Expand All @@ -18,5 +23,7 @@ func NewRouter() *mux.Router {
subscriptions := manager.PathPrefix("/subscription").Subrouter() //specific subrouter
subscriptions.HandleFunc("", getSubscriptions).Methods("GET")
subscriptions.HandleFunc("/edit", keySetSubscription).Methods("POST") //for editing subscription

router.MethodNotAllowedHandler = http.HandlerFunc(setCorsHeader) //if method is not found allow OPTIONS
return router
}

0 comments on commit c1a1ed2

Please sign in to comment.