2020-10-23 03:54:50 +13:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2021-01-24 06:18:14 +13:00
|
|
|
|
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
Bind string
|
|
|
|
Static string
|
2022-02-13 08:22:50 +13:00
|
|
|
PProf bool
|
2021-01-24 06:18:14 +13:00
|
|
|
CORS []string
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Server) Init(cmd *cobra.Command) error {
|
2021-03-17 03:24:58 +13:00
|
|
|
cmd.PersistentFlags().String("server.bind", "127.0.0.1:8080", "address/port/socket to serve neko")
|
|
|
|
if err := viper.BindPFlag("server.bind", cmd.PersistentFlags().Lookup("server.bind")); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
cmd.PersistentFlags().String("server.cert", "", "path to the SSL cert used to secure the neko server")
|
|
|
|
if err := viper.BindPFlag("server.cert", cmd.PersistentFlags().Lookup("server.cert")); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
cmd.PersistentFlags().String("server.key", "", "path to the SSL key used to secure the neko server")
|
|
|
|
if err := viper.BindPFlag("server.key", cmd.PersistentFlags().Lookup("server.key")); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
cmd.PersistentFlags().String("server.static", "", "path to neko client files to serve")
|
|
|
|
if err := viper.BindPFlag("server.static", cmd.PersistentFlags().Lookup("server.static")); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:22:50 +13:00
|
|
|
cmd.PersistentFlags().Bool("server.pprof", false, "enable pprof endpoint available at /debug/pprof")
|
|
|
|
if err := viper.BindPFlag("server.pprof", cmd.PersistentFlags().Lookup("server.pprof")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
cmd.PersistentFlags().StringSlice("server.cors", []string{"*"}, "list of allowed origins for CORS")
|
|
|
|
if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil {
|
2021-01-24 06:18:14 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Set() {
|
2021-03-17 03:24:58 +13:00
|
|
|
s.Cert = viper.GetString("server.cert")
|
|
|
|
s.Key = viper.GetString("server.key")
|
|
|
|
s.Bind = viper.GetString("server.bind")
|
|
|
|
s.Static = viper.GetString("server.static")
|
2022-02-13 08:22:50 +13:00
|
|
|
s.PProf = viper.GetBool("server.pprof")
|
2021-01-24 06:18:14 +13:00
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
s.CORS = viper.GetStringSlice("server.cors")
|
2021-01-24 06:18:14 +13:00
|
|
|
in, _ := utils.ArrayIn("*", s.CORS)
|
|
|
|
if len(s.CORS) == 0 || in {
|
|
|
|
s.CORS = []string{"*"}
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-01-24 06:18:14 +13:00
|
|
|
|
|
|
|
func (s *Server) AllowOrigin(origin string) bool {
|
|
|
|
in, _ := utils.ArrayIn(origin, s.CORS)
|
|
|
|
return in || s.CORS[0] == "*"
|
|
|
|
}
|