Skip to content

Commit

Permalink
Merge pull request #12 from sinhashubham95/fixes
Browse files Browse the repository at this point in the history
Fixed CORS Handling
  • Loading branch information
sinhashubham95 authored May 17, 2022
2 parents 75ecf24 + 6837215 commit d0dd218
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 0 additions & 6 deletions controllers/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ func Handle(ctx *fasthttp.RequestCtx) {
Path: path,
}

// add the cors headers
ctx.Response.Header.Add("Access-Control-Allow-Origin", "*")
ctx.Response.Header.Add("Access-Control-Allow-Credentials", "true")
ctx.Response.Header.Add("Access-Control-Allow-Headers", "*")
ctx.Response.Header.Add("Access-Control-Allow-Methods", "POST,HEAD,PATCH,OPTIONS,GET,PUT")

// try to fetch the mock
err := PersistenceView(&mock)
if err == nil {
Expand Down
12 changes: 12 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ func getFastHTTPHandler() fasthttp.RequestHandler {
Prefix: commons.ActuatorPrefix,
})
return actuatorCore.WrapFastHTTPHandler(func(ctx *fasthttp.RequestCtx) {
// actuator
if strings.HasPrefix(string(ctx.Path()), commons.ActuatorPrefix) && string(ctx.Method()) == http.MethodGet {
// this means it is an actuator endpoint
actuatorHandler(ctx)
return
}

// cors
ctx.Response.Header.Add("Access-Control-Allow-Origin", "*")
ctx.Response.Header.Add("Access-Control-Allow-Credentials", "true")
ctx.Response.Header.Add("Access-Control-Allow-Headers", "*")
ctx.Response.Header.Add("Access-Control-Allow-Methods", "POST,HEAD,PATCH,OPTIONS,GET,PUT")
if strings.ToUpper(string(ctx.Method())) == http.MethodOptions {
ctx.SetStatusCode(http.StatusNoContent)
return
}

switch string(ctx.Path()) {
case commons.BasePath:
handle(ctx, http.MethodGet, controllers.HandleBase)
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "moxy",
"version": "1.0.5"
"version": "1.0.6"
}

0 comments on commit d0dd218

Please sign in to comment.