diff --git a/cmd/api/command.go b/cmd/api/command.go index 0b4cc51..2ba3d84 100644 --- a/cmd/api/command.go +++ b/cmd/api/command.go @@ -33,6 +33,7 @@ func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Reques "Command", command, ) + w.Header().Set("Access-Control-Allow-Origin", "*") err = app.writeJSON(w, http.StatusOK, envelope{"command": command}, nil) if err != nil { app.serverErrorResponse(w, r, err) diff --git a/cmd/api/routes.go b/cmd/api/routes.go index c2ab691..1fdf1c3 100644 --- a/cmd/api/routes.go +++ b/cmd/api/routes.go @@ -12,9 +12,19 @@ func (app *application) routes() *httprouter.Router { router.NotFound = http.HandlerFunc(app.notFoundResponse) router.MethodNotAllowed = http.HandlerFunc(app.methodNotAllowedResponse) + // cors.Default().Handler(router) router.HandlerFunc(http.MethodGet, "/v1/commands/:name", app.showCommandHandler) router.HandlerFunc(http.MethodPost, "/v1/commands", app.createCommandHandler) router.HandlerFunc(http.MethodPatch, "/v1/commands/:name", app.updateCommandHandler) router.HandlerFunc(http.MethodDelete, "/v1/commands/:name", app.deleteCommandHandler) + return router } + +func MiddleCORS(next httprouter.Handle) httprouter.Handle { + return func(w http.ResponseWriter, + r *http.Request, ps httprouter.Params) { + w.Header().Set("Access-Control-Allow-Origin", "*") + next(w, r, ps) + } +} diff --git a/go.mod b/go.mod index 4ee339a..730a006 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( require ( github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/rs/cors v1.8.2 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.8.0 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect diff --git a/go.sum b/go.sum index ca0cf45..a6f0333 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U= +github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=