mirror of
https://github.com/Luzifer/restis.git
synced 2024-12-30 07:01:16 +00:00
Add CORS header middleware
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
4b9090edd8
commit
956122ef95
3 changed files with 47 additions and 1 deletions
|
@ -13,7 +13,8 @@
|
||||||
|
|
||||||
```console
|
```console
|
||||||
# restis --help
|
# restis --help
|
||||||
Usage of restis:
|
Usage of ./restis:
|
||||||
|
--disable-cors Disable setting CORS headers for all requests
|
||||||
--listen string Port/IP to listen on (default ":3000")
|
--listen string Port/IP to listen on (default ":3000")
|
||||||
--log-level string Log level (debug, info, warn, error, fatal) (default "info")
|
--log-level string Log level (debug, info, warn, error, fatal) (default "info")
|
||||||
--redis-conn-string string Connection string for redis (default "redis://localhost:6379/0")
|
--redis-conn-string string Connection string for redis (default "redis://localhost:6379/0")
|
||||||
|
|
30
cors.go
Normal file
30
cors.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func corsMiddleware(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Allow the client to send us credentials
|
||||||
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
|
||||||
|
// We don't care about headers at all, allow sending all
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||||
|
|
||||||
|
// List all accepted methods no matter whether they are accepted by the specified endpoint
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", strings.Join([]string{
|
||||||
|
http.MethodDelete,
|
||||||
|
http.MethodGet,
|
||||||
|
http.MethodPut,
|
||||||
|
}, ", "))
|
||||||
|
|
||||||
|
// Public API: Let everyone in
|
||||||
|
if origin := r.Header.Get("Origin"); origin != "" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
15
main.go
15
main.go
|
@ -16,6 +16,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg = struct {
|
cfg = struct {
|
||||||
|
DisableCORS bool `flag:"disable-cors" default:"false" description:"Disable setting CORS headers for all requests"`
|
||||||
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
|
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
|
||||||
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
||||||
RedisConnString string `flag:"redis-conn-string" default:"redis://localhost:6379/0" description:"Connection string for redis"`
|
RedisConnString string `flag:"redis-conn-string" default:"redis://localhost:6379/0" description:"Connection string for redis"`
|
||||||
|
@ -62,6 +63,20 @@ func main() {
|
||||||
router = mux.NewRouter()
|
router = mux.NewRouter()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if !cfg.DisableCORS {
|
||||||
|
router.Use(corsMiddleware)
|
||||||
|
}
|
||||||
|
|
||||||
|
router.MethodNotAllowedHandler = corsMiddleware(http.HandlerFunc(func(res http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
// Most likely JS client asking for CORS headers
|
||||||
|
res.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
}))
|
||||||
|
|
||||||
router.Methods(http.MethodDelete).HandlerFunc(handlerDelete(client))
|
router.Methods(http.MethodDelete).HandlerFunc(handlerDelete(client))
|
||||||
router.Methods(http.MethodGet).HandlerFunc(handlerGet(client))
|
router.Methods(http.MethodGet).HandlerFunc(handlerGet(client))
|
||||||
router.Methods(http.MethodPut).HandlerFunc(handlerPut(client))
|
router.Methods(http.MethodPut).HandlerFunc(handlerPut(client))
|
||||||
|
|
Loading…
Reference in a new issue