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: New reload command
Matthew Holt committed
Jul 5, 2019 at 09:59 UTC
9429c843c854c0a8d9f9e5710dd6903afabeefb3
3 files changed
+64
-2
admin.go
+5
-1
@@ -44,7 +44,7 @@ type AdminConfig struct {
44
// DefaultAdminConfig is the default configuration
45
// for the administration endpoint.
46
var DefaultAdminConfig = &AdminConfig{
47
- Listen: "localhost:2019",
47
+ Listen: DefaultAdminListen,
48
}
49
50
// StartAdmin starts Caddy's administration endpoint,
@@ -192,6 +192,10 @@ func Load(r io.Reader) error {
192
return nil
193
}
194
195
+// DefaultAdminListen is the address for the admin
196
+// listener, if none is specified at startup.
197
+var DefaultAdminListen = "localhost:2019"
198
+
199
var bufPool = sync.Pool{
200
New: func() interface{} {
201
return new(bytes.Buffer)
cmd/commands.go
+57
@@ -15,12 +15,16 @@
15
package caddycmd
16
17
import (
18
+ "bytes"
19
"crypto/rand"
20
+ "encoding/json"
21
"flag"
22
"fmt"
23
+ "io"
24
"io/ioutil"
25
"log"
26
"net"
27
+ "net/http"
28
"os"
29
"os/exec"
30
"path/filepath"
@@ -200,6 +204,59 @@ func cmdStop() (int, error) {
204
return 0, nil
205
}
206
207
+func cmdReload() (int, error) {
208
+ reloadCmd := flag.NewFlagSet("load", flag.ExitOnError)
209
+ reloadCmdConfigFlag := reloadCmd.String("config", "", "Configuration file")
210
+ reloadCmdAddrFlag := reloadCmd.String("address", "", "Address of the administration listener, if different from config")
211
+ reloadCmd.Parse(os.Args[2:])
212
+
213
+ // a configuration is required
214
+ if *reloadCmdConfigFlag == "" {
215
+ return 1, fmt.Errorf("no configuration to load (use --config)")
216
+ }
217
+
218
+ // load the configuration file
219
+ config, err := ioutil.ReadFile(*reloadCmdConfigFlag)
220
+ if err != nil {
221
+ return 1, fmt.Errorf("reading config file: %v", err)
222
+ }
223
+
224
+ // get the address of the admin listener and craft endpoint URL
225
+ adminAddr := *reloadCmdAddrFlag
226
+ if adminAddr == "" {
227
+ var tmpStruct struct {
228
+ Admin caddy.AdminConfig `json:"admin"`
229
+ }
230
+ err = json.Unmarshal(config, &tmpStruct)
231
+ if err != nil {
232
+ return 1, fmt.Errorf("unmarshaling admin listener address from config: %v", err)
233
+ }
234
+ adminAddr = tmpStruct.Admin.Listen
235
+ }
236
+ if adminAddr == "" {
237
+ adminAddr = caddy.DefaultAdminListen
238
+ }
239
+ adminEndpoint := fmt.Sprintf("http://%s/load", adminAddr)
240
+
241
+ // send the configuration to the instance
242
+ resp, err := http.Post(adminEndpoint, "application/json", bytes.NewReader(config))
243
+ if err != nil {
244
+ return 1, fmt.Errorf("sending configuration to instance: %v", err)
245
+ }
246
+ defer resp.Body.Close()
247
+
248
+ // if it didn't work, let the user know
249
+ if resp.StatusCode >= 400 {
250
+ respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10))
251
+ if err != nil {
252
+ return 1, fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
253
+ }
254
+ return 1, fmt.Errorf("caddy responded with error: HTTP %d: %s", resp.StatusCode, respBody)
255
+ }
256
+
257
+ return 0, nil
258
+}
259
+
260
func cmdVersion() (int, error) {
261
goModule := caddy.GoModule()
262
if goModule.Sum != "" {
cmd/main.go
+2
-1
@@ -51,8 +51,9 @@ type commandFunc func() (int, error)
51
52
var commands = map[string]commandFunc{
53
"start": cmdStart,
54
- "stop": cmdStop,
54
"run": cmdRun,
55
+ "stop": cmdStop,
56
+ "reload": cmdReload,
57
"version": cmdVersion,
58
"list-modules": cmdListModules,
59
"environ": cmdEnviron,