Beispiel #1
0
func NewHandler(
	logger lager.Logger,
	wrapper wrappa.Wrappa,
	clientFactory web.ClientFactory,
) (http.Handler, error) {
	tfuncs := &templateFuncs{
		assetIDs: map[string]string{},
	}

	funcs := template.FuncMap{
		"url":          tfuncs.url,
		"asset":        tfuncs.asset,
		"withRedirect": tfuncs.withRedirect,
	}

	indexTemplate, err := loadTemplate("index.html", funcs)
	if err != nil {
		return nil, err
	}

	pipelineTemplate, err := loadTemplateWithPipeline("pipeline.html", funcs)
	if err != nil {
		return nil, err
	}

	buildTemplate, err := loadTemplateWithPipeline("build.html", funcs)
	if err != nil {
		return nil, err
	}

	oldBuildTemplate, err := loadTemplateWithPipeline("old-build.html", funcs)
	if err != nil {
		return nil, err
	}

	buildsTemplate, err := loadTemplateWithoutPipeline("builds/index.html", funcs)
	if err != nil {
		return nil, err
	}

	joblessBuildTemplate, err := loadTemplateWithoutPipeline("builds/show.html", funcs)
	if err != nil {
		return nil, err
	}

	oldJoblessBuildTemplate, err := loadTemplateWithoutPipeline("builds/old-show.html", funcs)
	if err != nil {
		return nil, err
	}

	resourceTemplate, err := loadTemplateWithPipeline("resource.html", funcs)
	if err != nil {
		return nil, err
	}

	jobTemplate, err := loadTemplateWithPipeline("job.html", funcs)
	if err != nil {
		return nil, err
	}

	logInTemplate, err := loadTemplateWithoutPipeline("login.html", funcs)
	if err != nil {
		return nil, err
	}

	publicFS := &assetfs.AssetFS{
		Asset:     web.Asset,
		AssetDir:  web.AssetDir,
		AssetInfo: web.AssetInfo,
	}

	pipelineHandler := pipeline.NewHandler(logger, clientFactory, pipelineTemplate)

	handlers := map[string]http.Handler{
		web.Index:           authredirect.Handler{index.NewHandler(logger, clientFactory, pipelineHandler, indexTemplate)},
		web.Pipeline:        authredirect.Handler{pipelineHandler},
		web.Public:          CacheNearlyForever(http.FileServer(publicFS)),
		web.GetJob:          authredirect.Handler{getjob.NewHandler(logger, clientFactory, jobTemplate)},
		web.GetResource:     authredirect.Handler{getresource.NewHandler(logger, clientFactory, resourceTemplate)},
		web.GetBuild:        authredirect.Handler{getbuild.NewHandler(logger, clientFactory, buildTemplate, oldBuildTemplate)},
		web.GetBuilds:       authredirect.Handler{getbuilds.NewHandler(logger, clientFactory, buildsTemplate)},
		web.GetJoblessBuild: authredirect.Handler{getjoblessbuild.NewHandler(logger, clientFactory, joblessBuildTemplate, oldJoblessBuildTemplate)},
		web.TriggerBuild:    authredirect.Handler{triggerbuild.NewHandler(logger, clientFactory)},
		web.LogIn:           login.NewHandler(logger, clientFactory, logInTemplate),
		web.BasicAuth:       login.NewBasicAuthHandler(logger),
	}

	handler, err := rata.NewRouter(web.Routes, wrapper.Wrap(handlers))
	if err != nil {
		return nil, err
	}

	return authredirect.Tracker{
		Handler: handler,
	}, nil
}
Beispiel #2
0
func NewHandler(
	logger lager.Logger,
	validator auth.Validator,
	radarSchedulerFactory pipelines.RadarSchedulerFactory,
	db WebDB,
	pipelineDBFactory db.PipelineDBFactory,
	configDB db.ConfigDB,
	templatesDir, publicDir string,
	engine engine.Engine,
) (http.Handler, error) {
	tfuncs := &templateFuncs{
		assetsDir: publicDir,
		assetIDs:  map[string]string{},
	}

	funcs := template.FuncMap{
		"url":   tfuncs.url,
		"asset": tfuncs.asset,
	}

	pipelineHandlerFactory := pipelines.NewHandlerFactory(pipelineDBFactory)

	indexTemplate, err := template.New("index.html").Funcs(funcs).ParseFiles(filepath.Join(templatesDir, "index.html"))
	if err != nil {
		return nil, err
	}

	pipelineTemplate, err := loadTemplateWithPipeline(templatesDir, "pipeline.html", funcs)
	if err != nil {
		return nil, err
	}

	buildTemplate, err := loadTemplateWithPipeline(templatesDir, "build.html", funcs)
	if err != nil {
		return nil, err
	}

	buildsTemplate, err := loadTemplateWithoutPipeline(templatesDir, filepath.Join("builds", "index.html"), funcs)
	if err != nil {
		return nil, err
	}

	joblessBuildTemplate, err := loadTemplateWithoutPipeline(templatesDir, filepath.Join("builds", "show.html"), funcs)
	if err != nil {
		return nil, err
	}

	resourceTemplate, err := loadTemplateWithPipeline(templatesDir, "resource.html", funcs)
	if err != nil {
		return nil, err
	}

	jobTemplate, err := loadTemplateWithPipeline(templatesDir, "job.html", funcs)
	if err != nil {
		return nil, err
	}

	absPublicDir, err := filepath.Abs(publicDir)
	if err != nil {
		return nil, err
	}

	jobServer := getjob.NewServer(logger, jobTemplate)
	resourceServer := getresource.NewServer(logger, resourceTemplate, validator)
	pipelineServer := pipeline.NewServer(logger, pipelineTemplate)
	buildServer := getbuild.NewServer(logger, buildTemplate)
	triggerBuildServer := triggerbuild.NewServer(logger, radarSchedulerFactory)

	handlers := map[string]http.Handler{
		// public
		routes.Index:    index.NewHandler(logger, pipelineDBFactory, pipelineServer.GetPipeline, indexTemplate),
		routes.Pipeline: pipelineHandlerFactory.HandlerFor(pipelineServer.GetPipeline),
		routes.Public:   http.FileServer(http.Dir(filepath.Dir(absPublicDir))),

		routes.GetJob: pipelineHandlerFactory.HandlerFor(jobServer.GetJob),

		routes.GetResource:     pipelineHandlerFactory.HandlerFor(resourceServer.GetResource),
		routes.GetBuild:        pipelineHandlerFactory.HandlerFor(buildServer.GetBuild),
		routes.GetBuilds:       getbuilds.NewHandler(logger, db, configDB, buildsTemplate),
		routes.GetJoblessBuild: getjoblessbuild.NewHandler(logger, db, configDB, joblessBuildTemplate),

		// private
		routes.LogIn: auth.Handler{
			Handler:   login.NewHandler(logger),
			Validator: validator,
		},

		routes.TriggerBuild: auth.Handler{
			Handler:   pipelineHandlerFactory.HandlerFor(triggerBuildServer.TriggerBuild),
			Validator: validator,
		},
	}

	return rata.NewRouter(routes.Routes, handlers)
}