2019-03-27 02:00:54 +08:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgEndptSrv *http.Server
|
|
|
|
cfgEndptSrvMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// StartAdmin starts Caddy's administration endpoint.
|
|
|
|
func StartAdmin(addr string) error {
|
2019-03-27 02:00:54 +08:00
|
|
|
cfgEndptSrvMu.Lock()
|
|
|
|
defer cfgEndptSrvMu.Unlock()
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/load", handleLoadConfig)
|
|
|
|
|
|
|
|
for _, m := range GetModules("admin") {
|
|
|
|
moduleValue, err := m.New()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing module '%s': %v", m.Name, err)
|
|
|
|
}
|
|
|
|
route := moduleValue.(AdminRoute)
|
|
|
|
mux.Handle(route.Pattern, route)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgEndptSrv = &http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
go cfgEndptSrv.Serve(ln)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// StopAdmin stops the API endpoint.
|
|
|
|
func StopAdmin() error {
|
2019-03-27 02:00:54 +08:00
|
|
|
cfgEndptSrvMu.Lock()
|
|
|
|
defer cfgEndptSrvMu.Unlock()
|
|
|
|
|
|
|
|
if cfgEndptSrv == nil {
|
|
|
|
return fmt.Errorf("no server")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cfgEndptSrv.Shutdown(context.Background()) // TODO
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("shutting down server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgEndptSrv = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// AdminRoute represents a route for the admin endpoint.
|
|
|
|
type AdminRoute struct {
|
|
|
|
http.Handler
|
|
|
|
Pattern string
|
|
|
|
}
|
|
|
|
|
2019-03-27 02:00:54 +08:00
|
|
|
func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(r.Header.Get("Content-Type"), "/json") {
|
|
|
|
http.Error(w, "unacceptable Content-Type", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Load(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ADMIN][ERROR] loading config: %v", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// Load loads and starts a configuration.
|
2019-03-27 02:00:54 +08:00
|
|
|
func Load(r io.Reader) error {
|
2019-03-27 05:45:51 +08:00
|
|
|
var cfg Config
|
|
|
|
err := json.NewDecoder(r).Decode(&cfg)
|
2019-03-27 02:00:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decoding config: %v", err)
|
|
|
|
}
|
2019-03-27 05:45:51 +08:00
|
|
|
err = Start(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("starting: %v", err)
|
2019-03-27 02:00:54 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|