add CORS.

This commit is contained in:
Miroslav Šedivý 2021-01-23 18:18:14 +01:00
parent 79d67c4a09
commit d30d6deb79
6 changed files with 48 additions and 14 deletions

View file

@ -3,6 +3,8 @@ package config
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"demodesk/neko/internal/utils"
)
type Server struct {
@ -10,6 +12,7 @@ type Server struct {
Key string
Bind string
Static string
CORS []string
//Proxy bool
}
@ -34,6 +37,11 @@ func (Server) Init(cmd *cobra.Command) error {
return err
}
cmd.PersistentFlags().StringSlice("cors", []string{"http://icms:3001"}, "list of allowed origins for CORS")
if err := viper.BindPFlag("cors", cmd.PersistentFlags().Lookup("cors")); err != nil {
return err
}
//cmd.PersistentFlags().Bool("proxy", false, "allow reverse proxies")
//if err := viper.BindPFlag("proxy", cmd.PersistentFlags().Lookup("proxy")); err != nil {
// return err
@ -47,5 +55,17 @@ func (s *Server) Set() {
s.Key = viper.GetString("key")
s.Bind = viper.GetString("bind")
s.Static = viper.GetString("static")
s.CORS = viper.GetStringSlice("cors")
in, _ := utils.ArrayIn("*", s.CORS)
if len(s.CORS) == 0 || in {
s.CORS = []string{"*"}
}
//s.Proxy = viper.GetBool("proxy")
}
func (s *Server) AllowOrigin(origin string) bool {
in, _ := utils.ArrayIn(origin, s.CORS)
return in || s.CORS[0] == "*"
}