2022-05-08 09:00:22 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-09-27 20:19:27 +00:00
|
|
|
"strings"
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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, "=")
|
2022-05-08 09:00:22 +00:00
|
|
|
if !valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:19:27 +00:00
|
|
|
if !strings.HasPrefix(key, ServerKeyPrefix) {
|
2022-05-08 09:00:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:19:27 +00:00
|
|
|
serverName := strings.ToLower(strings.Trim(strings.Replace(key, ServerKeyPrefix, "", -1), "_"))
|
2022-05-08 09:00:22 +00:00
|
|
|
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, "=")
|
2022-05-08 09:00:22 +00:00
|
|
|
if !valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:19:27 +00:00
|
|
|
if !strings.HasPrefix(key, EndpointKeyPrefix) {
|
2022-05-08 09:00:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:19:27 +00:00
|
|
|
endpointRoute := path.Join(strings.Split(strings.ToLower(strings.Trim(strings.Replace(key, EndpointKeyPrefix, "", -1), "_")), "_")...)
|
2022-05-08 09:00:22 +00:00
|
|
|
spec := EndpointSpec{}
|
2022-05-13 13:38:19 +00:00
|
|
|
if err := spec.Parse(value); err != nil {
|
2022-05-08 09:00:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints[Route(endpointRoute)] = spec
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoints, nil
|
|
|
|
}
|