summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2021-02-01 18:14:03 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2021-02-01 18:14:03 -0700
commit2772ede43c852fa50f3527dbd94ae747b6f64365 (patch)
tree902680d2fb6f56fe71baa0d7e6155985381465aa
parentc986110678458f032018ff931273f8b6e1921e38 (diff)
cmd: Add --force flag to reload command (close #4005)
Can be useful if user wants to reload manual certificates, for example.
-rw-r--r--cmd/commandfuncs.go16
-rw-r--r--cmd/commands.go1
2 files changed, 14 insertions, 3 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index 9640f4b..f3feb61 100644
--- a/cmd/commandfuncs.go
+++ b/cmd/commandfuncs.go
@@ -282,7 +282,7 @@ func cmdRun(fl Flags) (int, error) {
func cmdStop(fl Flags) (int, error) {
stopCmdAddrFlag := fl.String("address")
- err := apiRequest(stopCmdAddrFlag, http.MethodPost, "/stop", nil)
+ err := apiRequest(stopCmdAddrFlag, http.MethodPost, "/stop", nil, nil)
if err != nil {
caddy.Log().Warn("failed using API to stop instance", zap.Error(err))
return caddy.ExitCodeFailedStartup, err
@@ -295,6 +295,7 @@ func cmdReload(fl Flags) (int, error) {
reloadCmdConfigFlag := fl.String("config")
reloadCmdConfigAdapterFlag := fl.String("adapter")
reloadCmdAddrFlag := fl.String("address")
+ reloadCmdForceFlag := fl.Bool("force")
if err := NotifyReloading(); err != nil {
caddy.Log().Error("unable to notify reloading to service manager", zap.Error(err))
@@ -328,7 +329,13 @@ func cmdReload(fl Flags) (int, error) {
adminAddr = tmpStruct.Admin.Listen
}
- err = apiRequest(adminAddr, http.MethodPost, "/load", bytes.NewReader(config))
+ // optionally force a config reload
+ headers := make(http.Header)
+ if reloadCmdForceFlag {
+ headers.Set("Cache-Control", "must-revalidate")
+ }
+
+ err = apiRequest(adminAddr, http.MethodPost, "/load", headers, bytes.NewReader(config))
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("sending configuration to instance: %v", err)
}
@@ -827,7 +834,7 @@ func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
// apiRequest makes an API request to the endpoint adminAddr with the
// given HTTP method and request URI. If body is non-nil, it will be
// assumed to be Content-Type application/json.
-func apiRequest(adminAddr, method, uri string, body io.Reader) error {
+func apiRequest(adminAddr, method, uri string, headers http.Header, body io.Reader) error {
// parse the admin address
if adminAddr == "" {
adminAddr = caddy.DefaultAdminListen
@@ -867,6 +874,9 @@ func apiRequest(adminAddr, method, uri string, body io.Reader) error {
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
+ for k, v := range headers {
+ req.Header[k] = v
+ }
// make an HTTP client that dials our network type, since admin
// endpoints aren't always TCP, which is what the default transport
diff --git a/cmd/commands.go b/cmd/commands.go
index 1765194..a519896 100644
--- a/cmd/commands.go
+++ b/cmd/commands.go
@@ -178,6 +178,7 @@ config file; otherwise the default is assumed.`,
fs.String("config", "", "Configuration file (required)")
fs.String("adapter", "", "Name of config adapter to apply")
fs.String("address", "", "Address of the administration listener, if different from config")
+ fs.Bool("force", false, "Force config reload, even if it is the same")
return fs
}(),
})