Explorar o código

Separate a) initialization of the http api and b) actually serving the api into 2 distinct jobs

Solomon Hykes %!s(int64=11) %!d(string=hai) anos
pai
achega
433c8e9c7d
Modificáronse 3 ficheiros con 21 adicións e 20 borrados
  1. 0 2
      config.go
  2. 6 2
      docker/docker.go
  3. 15 16
      server.go

+ 0 - 2
config.go

@@ -9,7 +9,6 @@ import (
 type DaemonConfig struct {
 type DaemonConfig struct {
 	Pidfile                     string
 	Pidfile                     string
 	Root                        string
 	Root                        string
-	ProtoAddresses              []string
 	AutoRestart                 bool
 	AutoRestart                 bool
 	EnableCors                  bool
 	EnableCors                  bool
 	Dns                         []string
 	Dns                         []string
@@ -36,7 +35,6 @@ func ConfigFromJob(job *engine.Job) *DaemonConfig {
 	} else {
 	} else {
 		config.BridgeIface = DefaultNetworkBridge
 		config.BridgeIface = DefaultNetworkBridge
 	}
 	}
-	config.ProtoAddresses = job.GetenvList("ProtoAddresses")
 	config.DefaultIp = net.ParseIP(job.Getenv("DefaultIp"))
 	config.DefaultIp = net.ParseIP(job.Getenv("DefaultIp"))
 	config.InterContainerCommunication = job.GetenvBool("InterContainerCommunication")
 	config.InterContainerCommunication = job.GetenvBool("InterContainerCommunication")
 	return &config
 	return &config

+ 6 - 2
docker/docker.go

@@ -71,7 +71,8 @@ func main() {
 		if err != nil {
 		if err != nil {
 			log.Fatal(err)
 			log.Fatal(err)
 		}
 		}
-		job := eng.Job("serveapi")
+		// Load plugin: httpapi
+		job := eng.Job("initapi")
 		job.Setenv("Pidfile", *pidfile)
 		job.Setenv("Pidfile", *pidfile)
 		job.Setenv("Root", *flRoot)
 		job.Setenv("Root", *flRoot)
 		job.SetenvBool("AutoRestart", *flAutoRestart)
 		job.SetenvBool("AutoRestart", *flAutoRestart)
@@ -79,12 +80,15 @@ func main() {
 		job.Setenv("Dns", *flDns)
 		job.Setenv("Dns", *flDns)
 		job.SetenvBool("EnableIptables", *flEnableIptables)
 		job.SetenvBool("EnableIptables", *flEnableIptables)
 		job.Setenv("BridgeIface", *bridgeName)
 		job.Setenv("BridgeIface", *bridgeName)
-		job.SetenvList("ProtoAddresses", flHosts)
 		job.Setenv("DefaultIp", *flDefaultIp)
 		job.Setenv("DefaultIp", *flDefaultIp)
 		job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
 		job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
 		if err := job.Run(); err != nil {
 		if err := job.Run(); err != nil {
 			log.Fatal(err)
 			log.Fatal(err)
 		}
 		}
+		// Serve api
+		if err := eng.Job("serveapi", flHosts...).Run(); err != nil {
+			log.Fatal(err)
+		}
 	} else {
 	} else {
 		if len(flHosts) > 1 {
 		if len(flHosts) > 1 {
 			log.Fatal("Please specify only one -H")
 			log.Fatal("Please specify only one -H")

+ 15 - 16
server.go

@@ -33,30 +33,20 @@ func (srv *Server) Close() error {
 }
 }
 
 
 func init() {
 func init() {
-	engine.Register("serveapi", JobServeApi)
+	engine.Register("initapi", jobInitApi)
 }
 }
 
 
-func JobServeApi(job *engine.Job) string {
+// jobInitApi runs the remote api server `srv` as a daemon,
+// Only one api server can run at the same time - this is enforced by a pidfile.
+// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup.
+func jobInitApi(job *engine.Job) string {
 	srv, err := NewServer(ConfigFromJob(job))
 	srv, err := NewServer(ConfigFromJob(job))
 	if err != nil {
 	if err != nil {
 		return err.Error()
 		return err.Error()
 	}
 	}
-	defer srv.Close()
-	if err := srv.Daemon(); err != nil {
-		return err.Error()
-	}
-	return "0"
-}
-
-// Daemon runs the remote api server `srv` as a daemon,
-// Only one api server can run at the same time - this is enforced by a pidfile.
-// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup.
-func (srv *Server) Daemon() error {
 	if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil {
 	if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
 	}
 	}
-	defer utils.RemovePidFile(srv.runtime.config.Pidfile)
-
 	c := make(chan os.Signal, 1)
 	c := make(chan os.Signal, 1)
 	signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
 	signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
 	go func() {
 	go func() {
@@ -66,8 +56,17 @@ func (srv *Server) Daemon() error {
 		srv.Close()
 		srv.Close()
 		os.Exit(0)
 		os.Exit(0)
 	}()
 	}()
+	err = engine.Register("serveapi", func(job *engine.Job) string {
+		return srv.ListenAndServe(job.Args...).Error()
+	})
+	if err != nil {
+		return err.Error()
+	}
+	return "0"
+}
+
 
 
-	protoAddrs := srv.runtime.config.ProtoAddresses
+func (srv *Server) ListenAndServe(protoAddrs ...string) error {
 	chErrors := make(chan error, len(protoAddrs))
 	chErrors := make(chan error, len(protoAddrs))
 	for _, protoAddr := range protoAddrs {
 	for _, protoAddr := range protoAddrs {
 		protoAddrParts := strings.SplitN(protoAddr, "://", 2)
 		protoAddrParts := strings.SplitN(protoAddr, "://", 2)