|
@ -1,6 +1,7 @@ |
|
|
package caddy2 |
|
|
package caddy2 |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
"context" |
|
|
"context" |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
@ -8,8 +9,10 @@ import ( |
|
|
"log" |
|
|
"log" |
|
|
"net" |
|
|
"net" |
|
|
"net/http" |
|
|
"net/http" |
|
|
|
|
|
"net/http/pprof" |
|
|
"strings" |
|
|
"strings" |
|
|
"sync" |
|
|
"sync" |
|
|
|
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var ( |
|
|
var ( |
|
@ -30,6 +33,14 @@ func StartAdmin(addr string) error { |
|
|
mux := http.NewServeMux() |
|
|
mux := http.NewServeMux() |
|
|
mux.HandleFunc("/load", handleLoadConfig) |
|
|
mux.HandleFunc("/load", handleLoadConfig) |
|
|
|
|
|
|
|
|
|
|
|
///// BEGIN PPROF STUFF //////
|
|
|
|
|
|
mux.HandleFunc("/debug/pprof/", pprof.Index) |
|
|
|
|
|
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) |
|
|
|
|
|
mux.HandleFunc("/debug/pprof/profile", pprof.Profile) |
|
|
|
|
|
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) |
|
|
|
|
|
mux.HandleFunc("/debug/pprof/trace", pprof.Trace) |
|
|
|
|
|
///// END PPROF STUFF //////
|
|
|
|
|
|
|
|
|
for _, m := range GetModules("admin") { |
|
|
for _, m := range GetModules("admin") { |
|
|
moduleValue, err := m.New() |
|
|
moduleValue, err := m.New() |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -40,7 +51,11 @@ func StartAdmin(addr string) error { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
cfgEndptSrv = &http.Server{ |
|
|
cfgEndptSrv = &http.Server{ |
|
|
Handler: mux, |
|
|
Handler: mux, |
|
|
|
|
|
ReadTimeout: 5 * time.Second, |
|
|
|
|
|
ReadHeaderTimeout: 5 * time.Second, |
|
|
|
|
|
IdleTimeout: 5 * time.Second, |
|
|
|
|
|
MaxHeaderBytes: 1024 * 256, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
go cfgEndptSrv.Serve(ln) |
|
|
go cfgEndptSrv.Serve(ln) |
|
@ -74,6 +89,7 @@ type AdminRoute struct { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func handleLoadConfig(w http.ResponseWriter, r *http.Request) { |
|
|
func handleLoadConfig(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
r.Close = true |
|
|
if r.Method != "POST" { |
|
|
if r.Method != "POST" { |
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) |
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) |
|
|
return |
|
|
return |
|
@ -94,14 +110,31 @@ func handleLoadConfig(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
// Load loads and starts a configuration.
|
|
|
// Load loads and starts a configuration.
|
|
|
func Load(r io.Reader) error { |
|
|
func Load(r io.Reader) error { |
|
|
|
|
|
buf := bufPool.Get().(*bytes.Buffer) |
|
|
|
|
|
buf.Reset() |
|
|
|
|
|
defer bufPool.Put(buf) |
|
|
|
|
|
|
|
|
|
|
|
_, err := io.Copy(buf, io.LimitReader(r, 1024*1024)) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var cfg Config |
|
|
var cfg Config |
|
|
err := json.NewDecoder(r).Decode(&cfg) |
|
|
err = json.Unmarshal(buf.Bytes(), &cfg) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return fmt.Errorf("decoding config: %v", err) |
|
|
return fmt.Errorf("decoding config: %v", err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = Start(cfg) |
|
|
err = Start(cfg) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return fmt.Errorf("starting: %v", err) |
|
|
return fmt.Errorf("starting: %v", err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var bufPool = sync.Pool{ |
|
|
|
|
|
New: func() interface{} { |
|
|
|
|
|
return new(bytes.Buffer) |
|
|
|
|
|
}, |
|
|
|
|
|
} |
|
|