Add CORS header through middleware

This commit is contained in:
Qian Wang 2019-07-25 13:03:32 +01:00
parent 2ce6f380d1
commit 09bdae2584
4 changed files with 14 additions and 3 deletions

1
go.mod
View File

@ -2,6 +2,7 @@ module github.com/cbeuw/Cloak
require ( require (
github.com/boltdb/bolt v1.3.1 github.com/boltdb/bolt v1.3.1
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/juju/ratelimit v1.0.1 github.com/juju/ratelimit v1.0.1
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY= github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY=

View File

@ -152,8 +152,7 @@ externalDocs:
description: Find out more about Swagger description: Find out more about Swagger
url: http://swagger.io url: http://swagger.io
# Added by API Auto Mocking Plugin # Added by API Auto Mocking Plugin
host: virtserver.swaggerhub.com host: 127.0.0.1:8080
basePath: /cbeuw/ck-server/1.0.0 basePath: /
schemes: schemes:
- https
- http - http

View File

@ -3,6 +3,7 @@ package server
import ( import (
"encoding/binary" "encoding/binary"
"log" "log"
"net/http"
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -42,12 +43,20 @@ func MakeLocalManager(dbPath string) (*localManager, error) {
return ret, nil return ret, nil
} }
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
next.ServeHTTP(w, r)
})
}
func (manager *localManager) registerMux() *gmux.Router { func (manager *localManager) registerMux() *gmux.Router {
r := gmux.NewRouter() r := gmux.NewRouter()
r.HandleFunc("/admin/users", manager.listAllUsersHlr).Methods("GET") r.HandleFunc("/admin/users", manager.listAllUsersHlr).Methods("GET")
r.HandleFunc("/admin/users/{UID}", manager.getUserInfo).Methods("GET") r.HandleFunc("/admin/users/{UID}", manager.getUserInfo).Methods("GET")
r.HandleFunc("/admin/users/{UID}", manager.writeUserInfo).Methods("POST") r.HandleFunc("/admin/users/{UID}", manager.writeUserInfo).Methods("POST")
r.HandleFunc("/admin/users/{UID}", manager.deleteUser).Methods("DELETE") r.HandleFunc("/admin/users/{UID}", manager.deleteUser).Methods("DELETE")
r.Use(corsMiddleware)
return r return r
} }