Skip to content

Commit

Permalink
better k8s env exclusion, refactor main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Jan 7, 2025
1 parent 891f699 commit 30d0443
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 77 deletions.
21 changes: 21 additions & 0 deletions internal/app/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app

import (
"github.com/centrifugal/centrifugo/v5/internal/config"
"github.com/spf13/cobra"
)

func Centrifugo() *cobra.Command {
var configFile string
cmd := &cobra.Command{
Use: "",
Short: "Centrifugo",
Long: "Centrifugo – scalable real-time messaging server in language-agnostic way",
Run: func(cmd *cobra.Command, args []string) {
Run(cmd, configFile)
},
}
cmd.Flags().StringVarP(&configFile, "config", "c", "config.json", "path to config file")
config.DefineFlags(cmd)
return cmd
}
6 changes: 3 additions & 3 deletions internal/cli/checkconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"github.com/spf13/cobra"
)

func CheckConfigCommand() *cobra.Command {
func CheckConfig() *cobra.Command {
var checkConfigFile string
var checkConfigStrict bool
var checkConfigCmd = &cobra.Command{
Use: "checkconfig",
Short: "Check configuration file",
Long: `Check Centrifugo configuration file`,
Run: func(cmd *cobra.Command, args []string) {
CheckConfig(cmd, checkConfigFile, checkConfigStrict)
checkConfig(cmd, checkConfigFile, checkConfigStrict)
},
}
checkConfigCmd.Flags().StringVarP(&checkConfigFile, "config", "c", "config.json", "path to config file to check")
checkConfigCmd.Flags().BoolVarP(&checkConfigStrict, "strict", "s", false, "strict check - fail on unknown fields")
return checkConfigCmd
}

func CheckConfig(cmd *cobra.Command, checkConfigFile string, strict bool) {
func checkConfig(cmd *cobra.Command, checkConfigFile string, strict bool) {
cfg, cfgMeta, err := config.GetConfig(cmd, checkConfigFile)
if err != nil {
fmt.Printf("error getting config: %v\n", err)
Expand Down
12 changes: 6 additions & 6 deletions internal/cli/checksubtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (
"github.com/spf13/cobra"
)

func CheckSubTokenCommand() *cobra.Command {
func CheckSubToken() *cobra.Command {
var checkSubTokenConfigFile string
var checkSubTokenCmd = &cobra.Command{
Use: "checksubtoken [TOKEN]",
Short: "Check subscription JWT",
Long: `Check subscription JWT`,
Run: func(cmd *cobra.Command, args []string) {
CheckSubToken(cmd, checkSubTokenConfigFile, args)
checkSubToken(cmd, checkSubTokenConfigFile, args)
},
}
checkSubTokenCmd.Flags().StringVarP(&checkSubTokenConfigFile, "config", "c", "config.json", "path to config file")
return checkSubTokenCmd
}

func CheckSubToken(cmd *cobra.Command, checkSubTokenConfigFile string, args []string) {
func checkSubToken(cmd *cobra.Command, checkSubTokenConfigFile string, args []string) {
cfg, _, err := config.GetConfig(cmd, checkSubTokenConfigFile)
if err != nil {
fmt.Printf("error getting config: %v\n", err)
Expand All @@ -49,7 +49,7 @@ func CheckSubToken(cmd *cobra.Command, checkSubTokenConfigFile string, args []st
fmt.Printf("error: provide token to check [centrifugo checksubtoken <TOKEN>]\n")
os.Exit(1)
}
subject, channel, claims, err := checkSubToken(verifierConfig, cfg, args[0])
subject, channel, claims, err := parseAndVerifySubToken(verifierConfig, cfg, args[0])
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
Expand All @@ -61,8 +61,8 @@ func CheckSubToken(cmd *cobra.Command, checkSubTokenConfigFile string, args []st
fmt.Printf("valid subscription token for %s and channel \"%s\"\npayload: %s\n", user, channel, string(claims))
}

// checkSubToken checks subscription JWT for user.
func checkSubToken(config jwtverify.VerifierConfig, cfg config.Config, t string) (string, string, []byte, error) {
// parseAndVerifySubToken checks subscription JWT for user.
func parseAndVerifySubToken(config jwtverify.VerifierConfig, cfg config.Config, t string) (string, string, []byte, error) {
token, err := jwt.ParseNoVerify([]byte(t)) // Will be verified later.
if err != nil {
return "", "", nil, err
Expand Down
12 changes: 6 additions & 6 deletions internal/cli/checktoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (
"github.com/spf13/cobra"
)

func CheckTokenCommand() *cobra.Command {
func CheckToken() *cobra.Command {
var checkTokenConfigFile string
var checkTokenCmd = &cobra.Command{
Use: "checktoken [TOKEN]",
Short: "Check connection JWT",
Long: `Check connection JWT`,
Run: func(cmd *cobra.Command, args []string) {
CheckToken(cmd, checkTokenConfigFile, args)
checkToken(cmd, checkTokenConfigFile, args)
},
}
checkTokenCmd.Flags().StringVarP(&checkTokenConfigFile, "config", "c", "config.json", "path to config file")
return checkTokenCmd
}

func CheckToken(cmd *cobra.Command, checkTokenConfigFile string, args []string) {
func checkToken(cmd *cobra.Command, checkTokenConfigFile string, args []string) {
cfg, _, err := config.GetConfig(cmd, checkTokenConfigFile)
if err != nil {
fmt.Printf("error getting config: %v\n", err)
Expand All @@ -42,7 +42,7 @@ func CheckToken(cmd *cobra.Command, checkTokenConfigFile string, args []string)
fmt.Printf("error: provide token to check [centrifugo checktoken <TOKEN>]\n")
os.Exit(1)
}
subject, claims, err := checkToken(verifierConfig, cfg, args[0])
subject, claims, err := parseAndVerifyToken(verifierConfig, cfg, args[0])
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
Expand All @@ -54,8 +54,8 @@ func CheckToken(cmd *cobra.Command, checkTokenConfigFile string, args []string)
fmt.Printf("valid token for %s\npayload: %s\n", user, string(claims))
}

// checkToken checks JWT for user.
func checkToken(config jwtverify.VerifierConfig, ruleConfig config.Config, t string) (string, []byte, error) {
// parseAndVerifyToken checks JWT for user.
func parseAndVerifyToken(config jwtverify.VerifierConfig, ruleConfig config.Config, t string) (string, []byte, error) {
token, err := jwt.ParseNoVerify([]byte(t)) // Will be verified later.
if err != nil {
return "", nil, err
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"gopkg.in/yaml.v3"
)

func DefaultConfigCommand() *cobra.Command {
func DefaultConfig() *cobra.Command {
var defaultConfigFile string
var baseConfigFile string
var dryRun bool
Expand All @@ -25,7 +25,7 @@ func DefaultConfigCommand() *cobra.Command {
Short: "Generate full configuration file with defaults",
Long: `Generate full Centrifugo configuration file with defaults`,
Run: func(cmd *cobra.Command, args []string) {
DefaultConfig(defaultConfigFile, baseConfigFile, dryRun)
defaultConfig(defaultConfigFile, baseConfigFile, dryRun)
},
}
defaultConfigCmd.Flags().StringVarP(&defaultConfigFile, "config", "c", "config.json", "path to default config file to generate")
Expand All @@ -34,7 +34,7 @@ func DefaultConfigCommand() *cobra.Command {
return defaultConfigCmd
}

func DefaultConfig(configFile string, baseFile string, dryRun bool) {
func defaultConfig(configFile string, baseFile string, dryRun bool) {
if !dryRun {
exists, err := tools.PathExists(configFile)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/defaultenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
"github.com/spf13/cobra"
)

func DefaultEnvCommand() *cobra.Command {
func DefaultEnv() *cobra.Command {
var baseConfigFile string
var defaultEnvCmd = &cobra.Command{
Use: "defaultenv",
Short: "Generate full environment var list with defaults",
Long: `Generate full Centrifugo environment var list with defaults`,
Run: func(cmd *cobra.Command, args []string) {
DefaultEnv(baseConfigFile)
defaultEnv(baseConfigFile)
},
}
defaultEnvCmd.Flags().StringVarP(&baseConfigFile, "base", "b", "", "path to the base config file to use")
return defaultEnvCmd
}

func DefaultEnv(baseFile string) {
func defaultEnv(baseFile string) {
conf, meta, err := config.GetConfig(nil, baseFile)
if err != nil {
fmt.Printf("error: %v\n", err)
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
"github.com/spf13/cobra"
)

func GenConfigCommand() *cobra.Command {
func GenConfig() *cobra.Command {
var outputConfigFile string
var genConfigCmd = &cobra.Command{
Use: "genconfig",
Short: "Generate minimal configuration file to start with",
Long: `Generate minimal configuration file to start with`,
Run: func(cmd *cobra.Command, args []string) {
GenConfig(cmd, outputConfigFile)
genConfig(cmd, outputConfigFile)
},
}
genConfigCmd.Flags().StringVarP(&outputConfigFile, "config", "c", "config.json", "path to output config file")
return genConfigCmd
}

func GenConfig(cmd *cobra.Command, outputConfigFile string) {
func genConfig(cmd *cobra.Command, outputConfigFile string) {
err := tools.GenerateConfig(outputConfigFile)
if err != nil {
fmt.Printf("error: %v\n", err)
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/gensubtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/tidwall/sjson"
)

func GenSubTokenCommand() *cobra.Command {
func GenSubToken() *cobra.Command {
var genSubTokenConfigFile string
var genSubTokenUser string
var genSubTokenChannel string
Expand All @@ -27,7 +27,7 @@ func GenSubTokenCommand() *cobra.Command {
Short: "Generate sample subscription JWT for user",
Long: `Generate sample subscription JWT for user`,
Run: func(cmd *cobra.Command, args []string) {
GenSubToken(cmd, genSubTokenConfigFile, genSubTokenUser, genSubTokenChannel, genSubTokenTTL, genSubTokenQuiet)
genSubToken(cmd, genSubTokenConfigFile, genSubTokenUser, genSubTokenChannel, genSubTokenTTL, genSubTokenQuiet)
},
}
genSubTokenCmd.Flags().StringVarP(&genSubTokenConfigFile, "config", "c", "config.json", "path to config file")
Expand All @@ -38,7 +38,7 @@ func GenSubTokenCommand() *cobra.Command {
return genSubTokenCmd
}

func GenSubToken(
func genSubToken(
cmd *cobra.Command, genSubTokenConfigFile string, genSubTokenUser string,
genSubTokenChannel string, genSubTokenTTL int64, genSubTokenQuiet bool,
) {
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/gentoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/tidwall/sjson"
)

func GenTokenCommand() *cobra.Command {
func GenToken() *cobra.Command {
var genTokenConfigFile string
var genTokenUser string
var genTokenTTL int64
Expand All @@ -26,7 +26,7 @@ func GenTokenCommand() *cobra.Command {
Short: "Generate sample connection JWT for user",
Long: `Generate sample connection JWT for user`,
Run: func(cmd *cobra.Command, args []string) {
GenToken(cmd, genTokenConfigFile, genTokenUser, genTokenTTL, genTokenQuiet)
genToken(cmd, genTokenConfigFile, genTokenUser, genTokenTTL, genTokenQuiet)
},
}
genTokenCmd.Flags().StringVarP(&genTokenConfigFile, "config", "c", "config.json", "path to config file")
Expand All @@ -36,7 +36,7 @@ func GenTokenCommand() *cobra.Command {
return genTokenCmd
}

func GenToken(cmd *cobra.Command, genTokenConfigFile string, genTokenUser string, genTokenTTL int64, genTokenQuiet bool) {
func genToken(cmd *cobra.Command, genTokenConfigFile string, genTokenUser string, genTokenTTL int64, genTokenQuiet bool) {
cfg, _, err := config.GetConfig(cmd, genTokenConfigFile)
if err != nil {
fmt.Printf("error getting config: %v\n", err)
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
)

func ServeCommand() *cobra.Command {
func Serve() *cobra.Command {
var serveDir string
var servePort int
var serveAddr string
Expand All @@ -19,7 +19,7 @@ func ServeCommand() *cobra.Command {
Short: "Run static file server (for development only)",
Long: `Run static file server (for development only)`,
Run: func(cmd *cobra.Command, args []string) {
Serve(serveAddr, servePort, serveDir)
serve(serveAddr, servePort, serveDir)
},
}
serveCmd.Flags().StringVarP(&serveDir, "dir", "d", "./", "path to directory")
Expand All @@ -28,7 +28,7 @@ func ServeCommand() *cobra.Command {
return serveCmd
}

func Serve(serveAddr string, servePort int, serveDir string) {
func serve(serveAddr string, servePort int, serveDir string) {
address := net.JoinHostPort(serveAddr, strconv.Itoa(servePort))
fmt.Printf("start serving %s on %s\n", serveDir, address)
if err := http.ListenAndServe(address, http.FileServer(http.Dir(serveDir))); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"github.com/spf13/cobra"
)

func VersionCommand() *cobra.Command {
func Version() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Centrifugo version information",
Long: `Print the version information of Centrifugo`,
Run: func(cmd *cobra.Command, args []string) {
Version()
version()
},
}
}

func Version() {
func version() {
fmt.Printf("Centrifugo v%s (Go version: %s)\n", build.Version, runtime.Version())
}
17 changes: 5 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"reflect"
"regexp"
"strings"

"github.com/centrifugal/centrifugo/v5/internal/config/envconfig"
Expand All @@ -18,8 +19,6 @@ import (
)

type Config struct {
// PidFile is a path to write a file with Centrifugo process PID.
PidFile string `mapstructure:"pid_file" json:"pid_file" envconfig:"pid_file" toml:"pid_file" yaml:"pid_file"`
// HTTP is a configuration for Centrifugo HTTP server.
HTTP configtypes.HTTPServer `mapstructure:"http_server" json:"http_server" envconfig:"http_server" toml:"http_server" yaml:"http_server"`
// Log is a configuration for logging.
Expand Down Expand Up @@ -101,6 +100,8 @@ type Config struct {
// Shutdown is a configuration for graceful shutdown.
Shutdown configtypes.Shutdown `mapstructure:"shutdown" json:"shutdown" envconfig:"shutdown" toml:"shutdown" yaml:"shutdown"`

// PidFile is a path to write a file with Centrifugo process PID.
PidFile string `mapstructure:"pid_file" json:"pid_file" envconfig:"pid_file" toml:"pid_file" yaml:"pid_file"`
// EnableUnreleasedFeatures enables unreleased features. These features are not stable and may be removed even
// in minor release update. Evaluate and share feedback if you find some feature useful and want it to be stabilized.
EnableUnreleasedFeatures bool `mapstructure:"enable_unreleased_features" json:"enable_unreleased_features" envconfig:"enable_unreleased_features" toml:"enable_unreleased_features" yaml:"enable_unreleased_features"`
Expand Down Expand Up @@ -364,18 +365,10 @@ func checkEnvironmentVars(knownEnvVars map[string]envconfig.VarInfo) []string {
return unknownEnvs
}

var k8sPrefixes = []string{
"CENTRIFUGO_PORT_",
"CENTRIFUGO_SERVICE_",
}
var k8sEnvRegex = regexp.MustCompile(`^CENTRIFUGO(?:_[A-Z]+)?_(PORT|SERVICE_)`)

func isKubernetesEnvVar(envKey string) bool {
for _, k8sPrefix := range k8sPrefixes {
if strings.HasPrefix(envKey, k8sPrefix) {
return true
}
}
return false
return k8sEnvRegex.MatchString(envKey)
}

// DefaultConfig is a helper to be used in tests.
Expand Down
Loading

0 comments on commit 30d0443

Please sign in to comment.