コード例 #1
0
ファイル: main.go プロジェクト: cautio/switchboard
func main() {

	rootConfig, err := config.NewConfig(os.Args)

	logger := rootConfig.Logger

	err = rootConfig.Validate()
	if err != nil {
		logger.Fatal("Error validating config:", err, lager.Data{"config": rootConfig})
	}

	go func() {
		logger.Info(fmt.Sprintf("Starting profiling server on port %d", rootConfig.ProfilerPort))
		err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", rootConfig.ProfilerPort), nil)
		if err != nil {
			logger.Error("profiler failed with error", err)
		}
	}()

	if _, err := os.Stat(rootConfig.StaticDir); os.IsNotExist(err) {
		logger.Fatal(fmt.Sprintf("staticDir: %s does not exist", rootConfig.StaticDir), nil)
	}

	backends := domain.NewBackends(rootConfig.Proxy.Backends, logger)
	arpManager := domain.NewArmManager(logger)
	cluster := domain.NewCluster(
		backends,
		rootConfig.Proxy.HealthcheckTimeout(),
		logger,
		arpManager,
	)

	handler := api.NewHandler(backends, logger, rootConfig.API, rootConfig.StaticDir)

	members := grouper.Members{
		{
			Name:   "proxy",
			Runner: proxy.NewRunner(cluster, rootConfig.Proxy.Port, logger),
		},
		{
			Name:   "api",
			Runner: api.NewRunner(rootConfig.API.Port, handler, logger),
		},
	}

	if rootConfig.HealthPort != rootConfig.API.Port {
		members = append(members, grouper.Member{
			Name:   "health",
			Runner: health.NewRunner(rootConfig.HealthPort, logger),
		})
	}

	group := grouper.NewDynamic(os.Kill, len(members), len(members))
	process := ifrit.Invoke(group)
	inserter := group.Client().Inserter()
	for _, member := range members {
		inserter <- member
	}
	group.Client().Close()

	err = waitUntilReady(process, logger)
	if err != nil {
		logger.Fatal("Error starting switchboard", err, lager.Data{"proxyConfig": rootConfig.Proxy})
	}

	logger.Info("Proxy started", lager.Data{"proxyConfig": rootConfig.Proxy})

	err = ioutil.WriteFile(rootConfig.PidFile, []byte(strconv.Itoa(os.Getpid())), 0644)
	if err == nil {
		logger.Info(fmt.Sprintf("Wrote pidFile to %s", rootConfig.PidFile))
	} else {
		logger.Fatal("Cannot write pid to file", err, lager.Data{"pidFile": rootConfig.PidFile})
	}

	err = <-process.Wait()
	if err != nil {
		logger.Fatal("Switchboard exited unexpectedly", err, lager.Data{"proxyConfig": rootConfig.Proxy})
	}
}
コード例 #2
0
ファイル: runner_test.go プロジェクト: cautio/switchboard
	"net"
	"os"

	"github.com/cloudfoundry-incubator/switchboard/api"
	"github.com/cloudfoundry-incubator/switchboard/config"
	"github.com/cloudfoundry-incubator/switchboard/domain/fakes"
	"github.com/pivotal-golang/lager/lagertest"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/tedsuo/ifrit"
)

var _ = Describe("APIRunner", func() {
	It("shuts down gracefully when signalled", func() {
		apiPort := 10000 + GinkgoParallelNode()
		backends := &fakes.FakeBackends{}
		logger := lagertest.NewTestLogger("APIRunner Test")
		config := config.API{}
		staticDir := ""
		handler := api.NewHandler(backends, logger, config, staticDir)
		apiRunner := api.NewRunner(uint(apiPort), handler, logger)
		apiProcess := ifrit.Invoke(apiRunner)
		apiProcess.Signal(os.Kill)
		Eventually(apiProcess.Wait()).Should(Receive())

		_, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", apiPort))
		Expect(err).To(HaveOccurred())
	})
})