Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

37
library/conf/env/BUILD vendored Normal file
View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["env.go"],
importpath = "go-common/library/conf/env",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["env_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)

92
library/conf/env/env.go vendored Normal file
View File

@@ -0,0 +1,92 @@
// Package env get env & app config, all the public field must after init()
// finished and flag.Parse().
package env
import (
"flag"
"os"
)
// deploy env.
const (
DeployEnvDev = "dev"
DeployEnvFat1 = "fat1"
DeployEnvUat = "uat"
DeployEnvPre = "pre"
DeployEnvProd = "prod"
)
// env default value.
const (
// env
_region = "sh"
_zone = "sh001"
_deployEnv = "dev"
)
// env configuration.
var (
// Region avaliable region where app at.
Region string
// Zone avaliable zone where app at.
Zone string
// Hostname machine hostname.
Hostname string
// DeployEnv deploy env where app at.
DeployEnv string
// IP FIXME(haoguanwei) #240
IP = os.Getenv("POD_IP")
// AppID is global unique application id, register by service tree.
// such as main.arch.disocvery.
AppID string
// Color is the identification of different experimental group in one caster cluster.
Color string
)
// app default value.
const (
_httpPort = "8000"
_gorpcPort = "8099"
_grpcPort = "9000"
)
// app configraution.
var (
// HTTPPort app listen http port.
HTTPPort string
// GORPCPort app listen gorpc port.
GORPCPort string
// GRPCPort app listen grpc port.
GRPCPort string
)
func init() {
var err error
if Hostname, err = os.Hostname(); err != nil || Hostname == "" {
Hostname = os.Getenv("HOSTNAME")
}
addFlag(flag.CommandLine)
}
func addFlag(fs *flag.FlagSet) {
// env
fs.StringVar(&Region, "region", defaultString("REGION", _region), "avaliable region. or use REGION env variable, value: sh etc.")
fs.StringVar(&Zone, "zone", defaultString("ZONE", _zone), "avaliable zone. or use ZONE env variable, value: sh001/sh002 etc.")
fs.StringVar(&DeployEnv, "deploy.env", defaultString("DEPLOY_ENV", _deployEnv), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
fs.StringVar(&AppID, "appid", os.Getenv("APP_ID"), "appid is global unique application id, register by service tree. or use APP_ID env variable.")
fs.StringVar(&Color, "deploy.color", os.Getenv("DEPLOY_COLOR"), "deploy.color is the identification of different experimental group.")
// app
fs.StringVar(&HTTPPort, "http.port", defaultString("DISCOVERY_HTTP_PORT", _httpPort), "app listen http port, default: 8000")
fs.StringVar(&GORPCPort, "gorpc.port", defaultString("DISCOVERY_GORPC_PORT", _gorpcPort), "app listen gorpc port, default: 8099")
fs.StringVar(&GRPCPort, "grpc.port", defaultString("DISCOVERY_GRPC_PORT", _grpcPort), "app listen grpc port, default: 9000")
}
func defaultString(env, value string) string {
v := os.Getenv(env)
if v == "" {
return value
}
return v
}

122
library/conf/env/env_test.go vendored Normal file
View File

@@ -0,0 +1,122 @@
package env
import (
"flag"
"fmt"
"os"
"testing"
)
func TestDefaultString(t *testing.T) {
v := defaultString("a", "test")
if v != "test" {
t.Fatal("v must be test")
}
if err := os.Setenv("a", "test1"); err != nil {
t.Fatal(err)
}
v = defaultString("a", "test")
if v != "test1" {
t.Fatal("v must be test1")
}
}
func TestEnv(t *testing.T) {
tests := []struct {
flag string
env string
def string
val *string
}{
{
"region",
"REGION",
_region,
&Region,
},
{
"zone",
"ZONE",
_zone,
&Zone,
},
{
"deploy.env",
"DEPLOY_ENV",
_deployEnv,
&DeployEnv,
},
{
"appid",
"APP_ID",
"",
&AppID,
},
{
"http.port",
"DISCOVERY_HTTP_PORT",
_httpPort,
&HTTPPort,
},
{
"gorpc.port",
"DISCOVERY_GORPC_PORT",
_gorpcPort,
&GORPCPort,
},
{
"grpc.port",
"DISCOVERY_GRPC_PORT",
_grpcPort,
&GRPCPort,
},
{
"deploy.color",
"DEPLOY_COLOR",
"",
&Color,
},
}
for _, test := range tests {
// flag set value
t.Run(fmt.Sprintf("%s: flag set", test.env), func(t *testing.T) {
fs := flag.NewFlagSet("", flag.ContinueOnError)
addFlag(fs)
err := fs.Parse([]string{fmt.Sprintf("-%s=%s", test.flag, "test")})
if err != nil {
t.Fatal(err)
}
if *test.val != "test" {
t.Fatal("val must be test")
}
})
// flag not set, env set
t.Run(fmt.Sprintf("%s: flag not set, env set", test.env), func(t *testing.T) {
*test.val = ""
os.Setenv(test.env, "test2")
fs := flag.NewFlagSet("", flag.ContinueOnError)
addFlag(fs)
err := fs.Parse([]string{})
if err != nil {
t.Fatal(err)
}
if *test.val != "test2" {
t.Fatal("val must be test")
}
})
// flag not set, env not set
t.Run(fmt.Sprintf("%s: flag not set, env not set", test.env), func(t *testing.T) {
*test.val = ""
os.Setenv(test.env, "")
fs := flag.NewFlagSet("", flag.ContinueOnError)
addFlag(fs)
err := fs.Parse([]string{})
if err != nil {
t.Fatal(err)
}
if *test.val != test.def {
t.Fatal("val must be test")
}
})
}
}