nurse/config/env.go

62 lines
1.2 KiB
Go
Raw Normal View History

package config
import (
"os"
"path"
2022-09-27 20:19:27 +00:00
"strings"
)
const (
ServerKeyPrefix = "NURSE_SERVER"
EndpointKeyPrefix = "NURSE_ENDPOINT"
)
func ServersFromEnv() (map[string]Server, error) {
servers := make(map[string]Server)
for _, kv := range os.Environ() {
2022-09-27 20:19:27 +00:00
key, value, valid := strings.Cut(kv, "=")
if !valid {
continue
}
2022-09-27 20:19:27 +00:00
if !strings.HasPrefix(key, ServerKeyPrefix) {
continue
}
2022-09-27 20:19:27 +00:00
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() {
2022-09-27 20:19:27 +00:00
key, value, valid := strings.Cut(kv, "=")
if !valid {
continue
}
2022-09-27 20:19:27 +00:00
if !strings.HasPrefix(key, EndpointKeyPrefix) {
continue
}
2022-09-27 20:19:27 +00:00
endpointRoute := path.Join(strings.Split(strings.ToLower(strings.Trim(strings.Replace(key, EndpointKeyPrefix, "", -1), "_")), "_")...)
spec := EndpointSpec{}
2022-05-13 13:38:19 +00:00
if err := spec.Parse(value); err != nil {
return nil, err
}
endpoints[Route(endpointRoute)] = spec
}
return endpoints, nil
}