Add development CORS header when the "ENVIRONMENT" environment variable is set to "dev"

This commit is contained in:
TwinProduction 2021-01-24 04:48:07 -05:00
parent bc6ca2ebd0
commit f1aa5191bf
2 changed files with 15 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
@ -34,7 +35,10 @@ func init() {
// Handle creates the router and starts the server
func Handle() {
cfg := config.Get()
router := CreateRouter(cfg)
var router http.Handler = CreateRouter(cfg)
if os.Getenv("ENVIRONMENT") == "dev" {
router = developmentCorsHandler(router)
}
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.Web.Address, cfg.Web.Port),
Handler: router,

10
controller/cors.go Normal file
View File

@ -0,0 +1,10 @@
package controller
import "net/http"
func developmentCorsHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8081")
next.ServeHTTP(w, r)
})
}