Esempio n. 1
0
// Serve starts HTTP and Git APIs.
func Serve(port string, hostAPI *api.API, conf *config.Config, logger *log.Logger) error {
	defaultStack = append(
		[]rest.Middleware{
			&rest.AccessLogApacheMiddleware{
				Logger: logger,
				Format: logFormat,
			},
		}, defaultStack...)

	auth := func(user string, password string) bool {
		return password == conf.HostKey
	}

	setupGit(auth, conf)
	setupHTTP(auth, hostAPI)

	// Create server
	listener, err := net.Listen("tcp", ":"+port)
	if err != nil {
		log.Fatal(err)
	}
	shutdown.BeforeExit(func() { listener.Close() })

	log.Println("HTTP API listening on " + port)
	return http.Serve(listener, nil)
}
Esempio n. 2
0
func startDaemon(c *cli.Context) {
	defer shutdown.Exit()

	confPath := c.GlobalString("config")
	port := c.String("port")
	dev := c.Bool("dev")
	logger := setupLogger(c.String("log-dir"))

	conf, err := config.Ensure(confPath, c.String("host-key"), c.String("home"))
	if err != nil {
		if os.IsExist(err) {
			log.Fatal("Host is already running")
		}

		log.Fatal(err)
	}

	client, err := docker.NewClientFromEnv()
	if err != nil {
		log.Fatal(err)
	}

	hostAPI := api.New(client, conf, logger)

	shutdown.BeforeExit(func() {
		log.Println("Cleaning up...")
		if err := hostAPI.StopDB(); err != nil {
			log.Printf("Error stopping host DB: %v", err)
		}

		if err := conf.Cleanup(); err != nil {
			log.Printf("Error cleaning config: %v", err)
		}
	})

	if dev {
		log.Printf("Starting host, dev mode\n")
	} else {
		log.Printf("Starting host\n")
	}

	if err := hostAPI.StartDB(dev); err != nil {
		log.Printf("Error starting host DB: %v", err)
		return
	}

	if err := http.Serve(port, hostAPI, conf, logger); err != nil {
		log.Printf("HTTP API error: %v", err)
	}
}