env.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Package env get env & app config, all the public field must after init()
  2. // finished and flag.Parse().
  3. package env
  4. import (
  5. "flag"
  6. "os"
  7. )
  8. // deploy env.
  9. const (
  10. DeployEnvDev = "dev"
  11. DeployEnvFat1 = "fat1"
  12. DeployEnvUat = "uat"
  13. DeployEnvPre = "pre"
  14. DeployEnvProd = "prod"
  15. )
  16. // env default value.
  17. const (
  18. // env
  19. _region = "sh"
  20. _zone = "sh001"
  21. _deployEnv = "dev"
  22. )
  23. // env configuration.
  24. var (
  25. // Region avaliable region where app at.
  26. Region string
  27. // Zone avaliable zone where app at.
  28. Zone string
  29. // Hostname machine hostname.
  30. Hostname string
  31. // DeployEnv deploy env where app at.
  32. DeployEnv string
  33. // IP FIXME(haoguanwei) #240
  34. IP = os.Getenv("POD_IP")
  35. // AppID is global unique application id, register by service tree.
  36. // such as main.arch.disocvery.
  37. AppID string
  38. // Color is the identification of different experimental group in one caster cluster.
  39. Color string
  40. )
  41. // app default value.
  42. const (
  43. _httpPort = "8000"
  44. _gorpcPort = "8099"
  45. _grpcPort = "9000"
  46. )
  47. // app configraution.
  48. var (
  49. // HTTPPort app listen http port.
  50. HTTPPort string
  51. // GORPCPort app listen gorpc port.
  52. GORPCPort string
  53. // GRPCPort app listen grpc port.
  54. GRPCPort string
  55. )
  56. func init() {
  57. var err error
  58. if Hostname, err = os.Hostname(); err != nil || Hostname == "" {
  59. Hostname = os.Getenv("HOSTNAME")
  60. }
  61. addFlag(flag.CommandLine)
  62. }
  63. func addFlag(fs *flag.FlagSet) {
  64. // env
  65. fs.StringVar(&Region, "region", defaultString("REGION", _region), "avaliable region. or use REGION env variable, value: sh etc.")
  66. fs.StringVar(&Zone, "zone", defaultString("ZONE", _zone), "avaliable zone. or use ZONE env variable, value: sh001/sh002 etc.")
  67. fs.StringVar(&DeployEnv, "deploy.env", defaultString("DEPLOY_ENV", _deployEnv), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
  68. fs.StringVar(&AppID, "appid", os.Getenv("APP_ID"), "appid is global unique application id, register by service tree. or use APP_ID env variable.")
  69. fs.StringVar(&Color, "deploy.color", os.Getenv("DEPLOY_COLOR"), "deploy.color is the identification of different experimental group.")
  70. // app
  71. fs.StringVar(&HTTPPort, "http.port", defaultString("DISCOVERY_HTTP_PORT", _httpPort), "app listen http port, default: 8000")
  72. fs.StringVar(&GORPCPort, "gorpc.port", defaultString("DISCOVERY_GORPC_PORT", _gorpcPort), "app listen gorpc port, default: 8099")
  73. fs.StringVar(&GRPCPort, "grpc.port", defaultString("DISCOVERY_GRPC_PORT", _grpcPort), "app listen grpc port, default: 9000")
  74. }
  75. func defaultString(env, value string) string {
  76. v := os.Getenv(env)
  77. if v == "" {
  78. return value
  79. }
  80. return v
  81. }