diff --git a/config/depenv/context.go b/config/depenv/context.go new file mode 100644 index 0000000..af27373 --- /dev/null +++ b/config/depenv/context.go @@ -0,0 +1,18 @@ +package depenv + +import "context" + +type contextKey struct{} + +// NewContext adds the deployment environment to the context +func NewContext(ctx context.Context, d DeploymentEnvironment) context.Context { + return context.WithValue(ctx, contextKey{}, d) +} + +// FromContext retrieves the deployment environment from the context +func FromContext(ctx context.Context) DeploymentEnvironment { + if d, ok := ctx.Value(contextKey{}).(DeploymentEnvironment); ok { + return d + } + return DeployUndefined +} diff --git a/config/depenv/depenv.go b/config/depenv/depenv.go new file mode 100644 index 0000000..9c60f53 --- /dev/null +++ b/config/depenv/depenv.go @@ -0,0 +1,67 @@ +package depenv + +import "os" + +var manageServers = map[DeploymentEnvironment]string{ + DeployDevel: "https://manage.askdev.grundclock.com", + DeployTest: "https://manage.beta.grundclock.com", + DeployProd: "https://manage.ntppool.org", +} + +var apiServers = map[DeploymentEnvironment]string{ + DeployDevel: "https://dev-api.ntppool.dev", + DeployTest: "https://beta-api.ntppool.dev", + DeployProd: "https://api.ntppool.dev", +} + +// var validationServers = map[DeploymentEnvironment]string{ +// DeployDevel: "https://v.ntp.dev/d/", +// DeployTest: "https://v.ntp.dev/b/", +// DeployProd: "https://v.ntp.dev/p/", +// } + +const ( + DeployUndefined DeploymentEnvironment = iota + DeployDevel + DeployTest + DeployProd +) + +type DeploymentEnvironment uint8 + +func DeploymentEnvironmentFromString(s string) DeploymentEnvironment { + switch s { + case "devel", "dev": + return DeployDevel + case "test", "beta": + return DeployTest + case "prod": + return DeployProd + default: + return DeployUndefined + } +} + +func (d DeploymentEnvironment) String() string { + switch d { + case DeployProd: + return "prod" + case DeployTest: + return "test" + case DeployDevel: + return "devel" + default: + panic("invalid DeploymentEnvironment") + } +} + +func (d DeploymentEnvironment) APIHost() string { + if apiHost := os.Getenv("API_HOST"); apiHost != "" { + return apiHost + } + return apiServers[d] +} + +func (d DeploymentEnvironment) ManageURL(path string) string { + return manageServers[d] + path +} diff --git a/config/depenv/monitor_names.go b/config/depenv/monitor_names.go new file mode 100644 index 0000000..a2089b8 --- /dev/null +++ b/config/depenv/monitor_names.go @@ -0,0 +1,40 @@ +package depenv + +import ( + "fmt" + "strings" +) + +var monitorApiServers = map[DeploymentEnvironment]string{ + DeployDevel: "https://api.devel.mon.ntppool.dev", + DeployTest: "https://api.test.mon.ntppool.dev", + DeployProd: "https://api.mon.ntppool.dev", +} + +func (d DeploymentEnvironment) MonitorAPIHost() string { + return monitorApiServers[d] +} + +func GetDeploymentEnvironmentFromName(clientName string) (DeploymentEnvironment, error) { + clientName = strings.ToLower(clientName) + + if !strings.HasSuffix(clientName, ".mon.ntppool.dev") { + return DeployUndefined, fmt.Errorf("invalid client name %s", clientName) + } + + if clientName == "api.mon.ntppool.dev" { + return DeployProd, nil + } + + prefix := clientName[:strings.Index(clientName, ".mon.ntppool.dev")] + parts := strings.Split(prefix, ".") + if len(parts) != 2 { + return DeployUndefined, fmt.Errorf("invalid client name %s", clientName) + } + + if d := DeploymentEnvironmentFromString(parts[1]); d != DeployUndefined { + return d, nil + } + + return DeployUndefined, fmt.Errorf("invalid client name %s (unknown environment %s)", clientName, parts[1]) +} diff --git a/go.mod b/go.mod index f6d229b..01d2412 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module go.ntppool.org/common -go 1.23 - -toolchain go1.23.4 +go 1.23.5 require ( github.com/abh/certman v0.4.0 diff --git a/scripts/run-goreleaser b/scripts/run-goreleaser index 911d55e..7df3862 100755 --- a/scripts/run-goreleaser +++ b/scripts/run-goreleaser @@ -2,7 +2,7 @@ set -euo pipefail -go install github.com/goreleaser/goreleaser/v2@v2.5.0 +go install github.com/goreleaser/goreleaser/v2@v2.5.1 if [ ! -z "${harbor_username:-}" ]; then DOCKER_FILE=~/.docker/config.json