nurse/config/env.go
Peter 59d2d26984
All checks were successful
concourse-ci/golangci-lint golangci-lint run
concourse-ci/gotestsum gotestsum
feat: add concourse pipeline
2022-12-30 21:41:30 +01:00

61 lines
1.2 KiB
Go

package config
import (
"os"
"path"
"strings"
)
const (
ServerKeyPrefix = "NURSE_SERVER"
EndpointKeyPrefix = "NURSE_ENDPOINT"
)
func ServersFromEnv() (map[string]Server, error) {
servers := make(map[string]Server)
for _, kv := range os.Environ() {
key, value, valid := strings.Cut(kv, "=")
if !valid {
continue
}
if !strings.HasPrefix(key, ServerKeyPrefix) {
continue
}
serverName := strings.ToLower(strings.Trim(strings.Replace(key, ServerKeyPrefix, "", -1), "_"))
srv := Server{}
if err := srv.UnmarshalURL(value); err != nil {
return nil, err
}
servers[serverName] = srv
}
return servers, nil
}
func EndpointsFromEnv() (map[Route]EndpointSpec, error) {
endpoints := make(map[Route]EndpointSpec)
for _, kv := range os.Environ() {
key, value, valid := strings.Cut(kv, "=")
if !valid {
continue
}
if !strings.HasPrefix(key, EndpointKeyPrefix) {
continue
}
endpointRoute := path.Join(strings.Split(strings.ToLower(strings.Trim(strings.Replace(key, EndpointKeyPrefix, "", -1), "_")), "_")...)
spec := EndpointSpec{}
if err := spec.Parse(value); err != nil {
return nil, err
}
endpoints[Route(endpointRoute)] = spec
}
return endpoints, nil
}