mirror of
https://github.com/m1k1o/neko.git
synced 2025-06-01 10:22:35 +02:00
add CORS.
This commit is contained in:
parent
79d67c4a09
commit
d30d6deb79
6 changed files with 48 additions and 14 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
|
@ -27,6 +28,16 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c
|
|||
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.Recoverer) // Recover from panics without crashing server
|
||||
router.Use(cors.Handler(cors.Options{
|
||||
AllowOriginFunc: func(r *http.Request, origin string) bool {
|
||||
return conf.AllowOrigin(origin)
|
||||
},
|
||||
AllowedMethods: []string{"GET", "POST", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
ExposedHeaders: []string{"Link"},
|
||||
AllowCredentials: false,
|
||||
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
||||
}))
|
||||
router.Use(middleware.RequestID) // Create a request ID for each request
|
||||
router.Use(Logger) // Log API request calls using custom logger function
|
||||
|
||||
|
@ -34,7 +45,9 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c
|
|||
|
||||
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||
//nolint
|
||||
WebSocketManager.Upgrade(w, r)
|
||||
WebSocketManager.Upgrade(w, r, func(r *http.Request) bool {
|
||||
return conf.AllowOrigin(r.Header.Get("Origin"))
|
||||
})
|
||||
})
|
||||
|
||||
if conf.Static != "" {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue