add cors middleware
This commit is contained in:
parent
b2000331ce
commit
0f3cbd6f05
17
backend/middlewares/cors.go
Normal file
17
backend/middlewares/cors.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package middlewares
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Cors(handler http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
origin := request.Header.Get("Origin")
|
||||||
|
|
||||||
|
switch origin {
|
||||||
|
case "http://localhost:5173", "http://localhost", "https://alai.cromer.cl":
|
||||||
|
writer.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -9,6 +9,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/middlewares"
|
||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
@ -26,6 +27,7 @@ func Initialize() *httprouter.Router {
|
|||||||
func Serve(router *httprouter.Router) {
|
func Serve(router *httprouter.Router) {
|
||||||
newRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
|
newRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
|
||||||
newRouter = handlers.CompressHandler(newRouter)
|
newRouter = handlers.CompressHandler(newRouter)
|
||||||
|
newRouter = middlewares.Cors(newRouter)
|
||||||
|
|
||||||
idleConnsClosed := make(chan struct{})
|
idleConnsClosed := make(chan struct{})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user