@marcel91200 / caddy Mirror · read-only / commits / 2772ede4
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

cmd: Add --force flag to reload command (close #4005)

Can be useful if user wants to reload manual certificates, for example.

Matthew Holt committed Feb 1, 2021 at 18:14 UTC 2772ede43c852fa50f3527dbd94ae747b6f64365
2 files changed +14 -3
cmd/commandfuncs.go
+13 -3
@@ -282,7 +282,7 @@ func cmdRun(fl Flags) (int, error) {
282 func cmdStop(fl Flags) (int, error) {
283 stopCmdAddrFlag := fl.String("address")
284
285 - err := apiRequest(stopCmdAddrFlag, http.MethodPost, "/stop", nil)
285 + err := apiRequest(stopCmdAddrFlag, http.MethodPost, "/stop", nil, nil)
286 if err != nil {
287 caddy.Log().Warn("failed using API to stop instance", zap.Error(err))
288 return caddy.ExitCodeFailedStartup, err
@@ -295,6 +295,7 @@ func cmdReload(fl Flags) (int, error) {
295 reloadCmdConfigFlag := fl.String("config")
296 reloadCmdConfigAdapterFlag := fl.String("adapter")
297 reloadCmdAddrFlag := fl.String("address")
298 + reloadCmdForceFlag := fl.Bool("force")
299
300 if err := NotifyReloading(); err != nil {
301 caddy.Log().Error("unable to notify reloading to service manager", zap.Error(err))
@@ -328,7 +329,13 @@ func cmdReload(fl Flags) (int, error) {
329 adminAddr = tmpStruct.Admin.Listen
330 }
331
331 - err = apiRequest(adminAddr, http.MethodPost, "/load", bytes.NewReader(config))
332 + // optionally force a config reload
333 + headers := make(http.Header)
334 + if reloadCmdForceFlag {
335 + headers.Set("Cache-Control", "must-revalidate")
336 + }
337 +
338 + err = apiRequest(adminAddr, http.MethodPost, "/load", headers, bytes.NewReader(config))
339 if err != nil {
340 return caddy.ExitCodeFailedStartup, fmt.Errorf("sending configuration to instance: %v", err)
341 }
@@ -827,7 +834,7 @@ func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
834 // apiRequest makes an API request to the endpoint adminAddr with the
835 // given HTTP method and request URI. If body is non-nil, it will be
836 // assumed to be Content-Type application/json.
830 -func apiRequest(adminAddr, method, uri string, body io.Reader) error {
837 +func apiRequest(adminAddr, method, uri string, headers http.Header, body io.Reader) error {
838 // parse the admin address
839 if adminAddr == "" {
840 adminAddr = caddy.DefaultAdminListen
@@ -867,6 +874,9 @@ func apiRequest(adminAddr, method, uri string, body io.Reader) error {
874 if body != nil {
875 req.Header.Set("Content-Type", "application/json")
876 }
877 + for k, v := range headers {
878 + req.Header[k] = v
879 + }
880
881 // make an HTTP client that dials our network type, since admin
882 // endpoints aren't always TCP, which is what the default transport
cmd/commands.go
+1
@@ -178,6 +178,7 @@ config file; otherwise the default is assumed.`,
178 fs.String("config", "", "Configuration file (required)")
179 fs.String("adapter", "", "Name of config adapter to apply")
180 fs.String("address", "", "Address of the administration listener, if different from config")
181 + fs.Bool("force", false, "Force config reload, even if it is the same")
182 return fs
183 }(),
184 })