From ea4d94fa841b159fa9c90040e11451bfa93d3fb0 Mon Sep 17 00:00:00 2001 From: Deluan Date: Thu, 24 Mar 2016 20:00:35 -0400 Subject: [PATCH] Enabling CORS --- conf/router.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/conf/router.go b/conf/router.go index f28a05c04..b14fc016d 100644 --- a/conf/router.go +++ b/conf/router.go @@ -5,13 +5,14 @@ import ( "github.com/astaxie/beego" "github.com/astaxie/beego/context" + "github.com/astaxie/beego/plugins/cors" "github.com/deluan/gosonic/controllers" ) func init() { mapEndpoints() mapControllers() - mapFilters() + initFilters() } func mapEndpoints() { @@ -58,13 +59,20 @@ func mapControllers() { beego.ErrorController(&controllers.MainController{}) } -func mapFilters() { +func initFilters() { var ValidateRequest = func(ctx *context.Context) { c := api.BaseAPIController{} c.Ctx = ctx c.Data = make(map[interface{}]interface{}) api.Validate(c) } + beego.InsertFilter("/rest/*", beego.BeforeRouter, cors.Allow(&cors.Options{ + AllowOrigins: []string{"*"}, + AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin"}, + ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"}, + AllowCredentials: true, + })) beego.InsertFilter("/rest/*", beego.BeforeRouter, ValidateRequest) }