neko/server/internal/config/server.go

179 lines
6.1 KiB
Go
Raw Permalink Normal View History

package config
import (
2023-04-29 08:19:52 +12:00
"path"
2024-07-19 07:48:09 +12:00
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2021-01-24 06:18:14 +13:00
"github.com/demodesk/neko/pkg/utils"
)
type Server struct {
2023-04-29 08:19:52 +12:00
Cert string
Key string
Bind string
Proxy bool
2023-04-29 08:19:52 +12:00
Static string
PathPrefix string
PProf bool
Metrics bool
CORS []string
}
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 {
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 {
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 {
return err
}
cmd.PersistentFlags().Bool("server.proxy", false, "trust reverse proxy headers")
if err := viper.BindPFlag("server.proxy", cmd.PersistentFlags().Lookup("server.proxy")); err != nil {
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 {
return err
}
2023-04-29 08:19:52 +12:00
cmd.PersistentFlags().String("server.path_prefix", "/", "path prefix for HTTP requests")
if err := viper.BindPFlag("server.path_prefix", cmd.PersistentFlags().Lookup("server.path_prefix")); err != nil {
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
}
2022-07-05 04:26:29 +12:00
cmd.PersistentFlags().Bool("server.metrics", true, "enable prometheus metrics available at /metrics")
if err := viper.BindPFlag("server.metrics", cmd.PersistentFlags().Lookup("server.metrics")); err != nil {
return err
}
cmd.PersistentFlags().StringSlice("server.cors", []string{}, "list of allowed origins for CORS, if empty CORS is disabled, if '*' is present all origins are allowed")
2021-03-17 03:24:58 +13:00
if err := viper.BindPFlag("server.cors", cmd.PersistentFlags().Lookup("server.cors")); err != nil {
2021-01-24 06:18:14 +13:00
return err
}
return nil
}
2024-07-19 07:48:09 +12:00
func (Server) InitV2(cmd *cobra.Command) error {
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().String("bind", "", "V2: address/port/socket to serve neko")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("bind", cmd.PersistentFlags().Lookup("bind")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().String("cert", "", "V2: path to the SSL cert used to secure the neko server")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("cert", cmd.PersistentFlags().Lookup("cert")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().String("key", "", "V2: path to the SSL key used to secure the neko server")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("key", cmd.PersistentFlags().Lookup("key")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().Bool("proxy", false, "V2: enable reverse proxy mode")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("proxy", cmd.PersistentFlags().Lookup("proxy")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().String("static", "", "V2: path to neko client files to serve")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("static", cmd.PersistentFlags().Lookup("static")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().String("path_prefix", "", "V2: path prefix for HTTP requests")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("path_prefix", cmd.PersistentFlags().Lookup("path_prefix")); err != nil {
return err
}
2024-07-19 07:54:53 +12:00
cmd.PersistentFlags().StringSlice("cors", []string{}, "V2: list of allowed origins for CORS")
2024-07-19 07:48:09 +12:00
if err := viper.BindPFlag("cors", cmd.PersistentFlags().Lookup("cors")); err != nil {
return err
}
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.Proxy = viper.GetBool("server.proxy")
2021-03-17 03:24:58 +13:00
s.Static = viper.GetString("server.static")
2023-04-29 08:19:52 +12:00
s.PathPrefix = path.Join("/", path.Clean(viper.GetString("server.path_prefix")))
2022-02-13 08:22:50 +13:00
s.PProf = viper.GetBool("server.pprof")
2022-07-05 04:26:29 +12:00
s.Metrics = viper.GetBool("server.metrics")
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{"*"}
}
}
2021-01-24 06:18:14 +13:00
2024-07-19 07:48:09 +12:00
func (s *Server) SetV2() {
if viper.IsSet("cert") {
s.Cert = viper.GetString("cert")
log.Warn().Msg("you are using v2 configuration 'NEKO_CERT' which is deprecated, please use 'NEKO_SERVER_CERT' instead")
}
if viper.IsSet("key") {
s.Key = viper.GetString("key")
log.Warn().Msg("you are using v2 configuration 'NEKO_KEY' which is deprecated, please use 'NEKO_SERVER_KEY' instead")
}
if viper.IsSet("bind") {
s.Bind = viper.GetString("bind")
log.Warn().Msg("you are using v2 configuration 'NEKO_BIND' which is deprecated, please use 'NEKO_SERVER_BIND' instead")
}
if viper.IsSet("proxy") {
s.Proxy = viper.GetBool("proxy")
log.Warn().Msg("you are using v2 configuration 'NEKO_PROXY' which is deprecated, please use 'NEKO_SERVER_PROXY' instead")
}
if viper.IsSet("static") {
s.Static = viper.GetString("static")
log.Warn().Msg("you are using v2 configuration 'NEKO_STATIC' which is deprecated, please use 'NEKO_SERVER_STATIC' instead")
}
if viper.IsSet("path_prefix") {
s.PathPrefix = path.Join("/", path.Clean(viper.GetString("path_prefix")))
log.Warn().Msg("you are using v2 configuration 'NEKO_PATH_PREFIX' which is deprecated, please use 'NEKO_SERVER_PATH_PREFIX' instead")
}
if viper.IsSet("cors") {
s.CORS = viper.GetStringSlice("cors")
in, _ := utils.ArrayIn("*", s.CORS)
if len(s.CORS) == 0 || in {
s.CORS = []string{"*"}
}
log.Warn().Msg("you are using v2 configuration 'NEKO_CORS' which is deprecated, please use 'NEKO_SERVER_CORS' instead")
}
}
func (s *Server) HasCors() bool {
return len(s.CORS) > 0
}
2021-01-24 06:18:14 +13:00
func (s *Server) AllowOrigin(origin string) bool {
// if CORS is disabled, allow all origins
if len(s.CORS) == 0 {
return true
}
// if CORS is enabled, allow only origins in the list
2021-01-24 06:18:14 +13:00
in, _ := utils.ArrayIn(origin, s.CORS)
return in || s.CORS[0] == "*"
}