示例#1
0
// Serve sets everything up and runs the docker-builder server
func Serve(context *cli.Context) {
	// set vars
	setVarsFromContext(context)

	// set up auth functions
	if shouldBasicAuth {
		basicAuthFunc = auth.Basic(un, pwd)
	} else {
		basicAuthFunc = func(http.ResponseWriter, *http.Request) {}
	}
	if shouldTravisAuth {
		travisAuthFunc = vauth.TravisCI(travisToken)
	}
	if shouldGitHubAuth {
		githubAuthFunc = vauth.GitHub(githubSecret)
	}

	// configure webhooks
	webhook.Logger(logger)
	webhook.APIToken(apiToken)

	server = setupServer()

	if shouldTravis {
		server.Post(TravisRoute, travisAuthFunc, webhook.Travis)
	}
	if shouldGitHub {
		server.Post(GitHubRoute, githubAuthFunc, webhook.Github)
	}

	// base routes
	server.Get(HealthRoute, func() (int, string) { return 200, "200 OK" })
	server.Post(BuildRoute, basicAuthFunc, webhook.DockerBuild)

	// job control routes
	server.Group(JobRoute, func(r martini.Router) {
		r.Get("/:id", job.Get)
		r.Get("/:id/tail", job.TailN)
		r.Post("", webhook.DockerBuild)
		r.Get("", job.GetAll)
	}, basicAuthFunc)

	// start server
	http.ListenAndServe(portString, server)
}
func init() {
	r := martini.NewRouter()
	m := martini.New()
	m.Use(martini.Recovery())
	m.Use(martini.Static("public"))
	m.MapTo(r, (*martini.Routes)(nil))
	m.Action(r.Handle)
	testServer = &martini.ClassicMartini{m, r}
	l := &logrus.Logger{
		Level:     logrus.PanicLevel,
		Formatter: &logrus.JSONFormatter{},
	}
	Logger(l)
	webhook.Logger(l)

	// job control routes
	testServer.Group("/jobs", func(r martini.Router) {
		r.Get("/:id", Get)
		r.Get("/:id/tail", TailN)
		r.Post("", webhook.DockerBuild)
		r.Get("", GetAll)
	})
}