Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/config/server.go

81 lines
2.2 KiB
Go
Raw Normal View History

2020-01-13 21:05:38 +13:00
package config
import (
2022-11-20 08:33:03 +13:00
"net/http"
2022-09-02 04:22:01 +12:00
"path"
2020-01-13 21:05:38 +13:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2022-11-20 08:45:38 +13:00
"m1k1o/neko/internal/utils"
2020-01-13 21:05:38 +13:00
)
2020-01-19 12:30:09 +13:00
type Server struct {
2022-09-02 04:22:01 +12:00
Cert string
Key string
Bind string
2023-11-20 02:50:23 +13:00
Proxy bool
2022-09-02 04:22:01 +12:00
Static string
PathPrefix string
2022-11-20 08:33:03 +13:00
CORS []string
2020-01-13 21:05:38 +13:00
}
2020-01-19 12:30:09 +13:00
func (Server) Init(cmd *cobra.Command) error {
2020-01-29 14:27:59 +13:00
cmd.PersistentFlags().String("bind", "127.0.0.1:8080", "address/port/socket to serve neko")
2020-01-13 21:05:38 +13:00
if err := viper.BindPFlag("bind", cmd.PersistentFlags().Lookup("bind")); err != nil {
return err
}
2020-01-29 14:27:59 +13:00
cmd.PersistentFlags().String("cert", "", "path to the SSL cert used to secure the neko server")
2020-01-13 21:05:38 +13:00
if err := viper.BindPFlag("cert", cmd.PersistentFlags().Lookup("cert")); err != nil {
return err
}
2020-01-29 14:27:59 +13:00
cmd.PersistentFlags().String("key", "", "path to the SSL key used to secure the neko server")
2020-01-13 21:05:38 +13:00
if err := viper.BindPFlag("key", cmd.PersistentFlags().Lookup("key")); err != nil {
return err
}
2023-11-20 02:50:23 +13:00
cmd.PersistentFlags().Bool("proxy", false, "enable reverse proxy mode")
if err := viper.BindPFlag("proxy", cmd.PersistentFlags().Lookup("proxy")); err != nil {
return err
}
2020-01-29 14:27:59 +13:00
cmd.PersistentFlags().String("static", "./www", "path to neko client files to serve")
2020-01-13 21:05:38 +13:00
if err := viper.BindPFlag("static", cmd.PersistentFlags().Lookup("static")); err != nil {
return err
}
2022-09-02 04:22:01 +12:00
cmd.PersistentFlags().String("path_prefix", "/", "path prefix for HTTP requests")
if err := viper.BindPFlag("path_prefix", cmd.PersistentFlags().Lookup("path_prefix")); err != nil {
return err
}
2022-11-20 08:33:03 +13:00
cmd.PersistentFlags().StringSlice("cors", []string{"*"}, "list of allowed origins for CORS")
if err := viper.BindPFlag("cors", cmd.PersistentFlags().Lookup("cors")); err != nil {
return err
}
2020-01-13 21:05:38 +13:00
return nil
}
2020-01-19 12:30:09 +13:00
func (s *Server) Set() {
2020-01-13 21:05:38 +13:00
s.Cert = viper.GetString("cert")
s.Key = viper.GetString("key")
s.Bind = viper.GetString("bind")
2023-11-20 02:50:23 +13:00
s.Proxy = viper.GetBool("proxy")
2020-01-13 21:05:38 +13:00
s.Static = viper.GetString("static")
2022-09-02 04:22:01 +12:00
s.PathPrefix = path.Join("/", path.Clean(viper.GetString("path_prefix")))
2022-11-20 08:33:03 +13:00
s.CORS = viper.GetStringSlice("cors")
in, _ := utils.ArrayIn("*", s.CORS)
if len(s.CORS) == 0 || in {
s.CORS = []string{"*"}
}
}
func (s *Server) AllowOrigin(r *http.Request, origin string) bool {
in, _ := utils.ArrayIn(origin, s.CORS)
return in || s.CORS[0] == "*"
2020-01-13 21:05:38 +13:00
}