Added CORS Header Set

This commit is contained in:
konrad 2017-11-08 16:59:48 +01:00 committed by kolaente
parent 07c9fcdea1
commit 9a745916b2
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 20 additions and 0 deletions

16
routes/cors.go Normal file
View File

@ -0,0 +1,16 @@
package routes
import (
"github.com/labstack/echo"
"net/http"
)
// SetCORSHeader sets relevant CORS headers for Cross-Site-Requests to the api
func SetCORSHeader (c echo.Context) error {
res := c.Response()
res.Header().Set("Access-Control-Allow-Origin", "*")
res.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
res.Header().Set("Access-Control-Allow-Headers", "authorization,content-type")
res.Header().Set("Access-Control-Expose-Headers", "authorization,content-type")
return c.String(http.StatusOK, "")
}

View File

@ -26,6 +26,10 @@ func NewEcho() *echo.Echo {
// RegisterRoutes registers all routes for the application
func RegisterRoutes(e *echo.Echo) {
// CORS
e.OPTIONS("/login", SetCORSHeader)
e.OPTIONS("/api/v1/books", SetCORSHeader)
// Basic Route
e.Static("/", "assets/index.html")