DetermineAdminAPIAddress determines which admin API endpoint address should be used based on the inputs. By priority: if `address` is specified, then it is returned; if `config` is specified, then that config will be used for finding the admin address; if `configFile` (and `configAdapter`) are speci
(address string, config []byte, configFile, configAdapter string)
| 860 | // then that config will be loaded to find the admin address; otherwise, the |
| 861 | // default admin listen address will be returned. |
| 862 | func DetermineAdminAPIAddress(address string, config []byte, configFile, configAdapter string) (string, error) { |
| 863 | // Prefer the address if specified and non-empty |
| 864 | if address != "" { |
| 865 | return address, nil |
| 866 | } |
| 867 | |
| 868 | // Try to load the config from file if specified, with the given adapter name |
| 869 | if configFile != "" { |
| 870 | var loadedConfigFile string |
| 871 | var err error |
| 872 | |
| 873 | // use the provided loaded config if non-empty |
| 874 | // otherwise, load it from the specified file/adapter |
| 875 | loadedConfig := config |
| 876 | if len(loadedConfig) == 0 { |
| 877 | // get the config in caddy's native format |
| 878 | loadedConfig, loadedConfigFile, _, err = LoadConfig(configFile, configAdapter) |
| 879 | if err != nil { |
| 880 | return "", err |
| 881 | } |
| 882 | if loadedConfigFile == "" { |
| 883 | return "", fmt.Errorf("no config file to load; either use --config flag or ensure Caddyfile exists in current directory") |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // get the address of the admin listener from the config |
| 888 | if len(loadedConfig) > 0 { |
| 889 | var tmpStruct struct { |
| 890 | Admin caddy.AdminConfig `json:"admin"` |
| 891 | } |
| 892 | err := json.Unmarshal(loadedConfig, &tmpStruct) |
| 893 | if err != nil { |
| 894 | return "", fmt.Errorf("unmarshaling admin listener address from config: %v", err) |
| 895 | } |
| 896 | if tmpStruct.Admin.Listen != "" { |
| 897 | return tmpStruct.Admin.Listen, nil |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // Fallback to the default listen address otherwise |
| 903 | return caddy.DefaultAdminListen, nil |
| 904 | } |
| 905 | |
| 906 | // configFileWithRespectToDefault returns the filename to use for loading the config, based |
| 907 | // on whether a config file is already specified and a supported default config file exists. |
no test coverage detected