added cors

This commit is contained in:
2017-04-10 20:03:17 +01:00
parent cae49d6443
commit 1c28a8e932
2 changed files with 23 additions and 2 deletions

22
main.go
View File

@@ -9,6 +9,7 @@ import (
"fmt"
"io"
"log"
"os"
"strings"
"time"
@@ -28,12 +29,31 @@ type Vault struct {
func main() {
router := gin.Default()
router.Use(CORS(os.Getenv("VAULT_APP_URL")))
router.POST("/", createAction)
router.POST("/decrypt", decryptAction)
router.Run(":7014")
}
// CORS handles setting up the CORS security headers.
func CORS(allowOrigin string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", allowOrigin)
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PATCH, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
fmt.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}
func show(c *gin.Context) {
log.Println("asdasdsa")
}