diff options
author | Matthew Holt <[email protected]> | 2021-02-01 18:14:03 -0700 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2021-02-01 18:14:03 -0700 |
commit | 2772ede43c852fa50f3527dbd94ae747b6f64365 (patch) | |
tree | 902680d2fb6f56fe71baa0d7e6155985381465aa /cmd/commandfuncs.go | |
parent | c986110678458f032018ff931273f8b6e1921e38 (diff) | |
download | caddy-2772ede43c852fa50f3527dbd94ae747b6f64365.tar.gz caddy-2772ede43c852fa50f3527dbd94ae747b6f64365.zip |
cmd: Add --force flag to reload command (close #4005)
Can be useful if user wants to reload manual certificates, for example.
Diffstat (limited to 'cmd/commandfuncs.go')
-rw-r--r-- | cmd/commandfuncs.go | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index 9640f4bc3..f3feb6105 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 |