diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 6695b01..ac8be44 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -12,6 +12,11 @@ "Comment": "v6.1.0-4-g6cef6a7", "Rev": "6cef6a7f8b01a4288118f1a4c5c303f117c1c7f0" }, + { + "ImportPath": "github.com/gin-contrib/cors", + "Comment": "v1.3.0-1-g2a8f489", + "Rev": "2a8f489b42294786cbf465c1f20588b49117f1fe" + }, { "ImportPath": "github.com/gin-contrib/sse", "Comment": "v0.1.0-1-g43f0f29", diff --git a/main.go b/main.go index 64c6e46..78178eb 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "gitea-issue/giteaClient" "github.com/caarlos0/env" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" @@ -14,8 +15,13 @@ import ( var ( db *gorm.DB giteaConfig giteaClient.GiteaConfig + proxyConfig ProxyConfig ) +type ProxyConfig struct { + ProjectOrigin string `env:"PROJECT_ORIGIN,required"` +} + func init() { //open a db connection var err error @@ -26,6 +32,9 @@ func init() { if err := env.Parse(&giteaConfig); err != nil { panic("ENV error") } + if err := env.Parse(&proxyConfig); err != nil { + panic("ENV error") + } giteaClient.SetUp(giteaConfig) db.AutoMigrate(&userModel{}) @@ -34,6 +43,9 @@ func init() { func main() { router := gin.Default() + config := cors.DefaultConfig() + config.AllowOrigins = []string{proxyConfig.ProjectOrigin} + router.Use(cors.New(config)) v1 := router.Group("/api/v1/issues") {