Ejemplo n.º 1
0
func main() {
	flag.Parse()
	log.Printf("Starting Goship...")

	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	auth.Initialize(auth.User{Name: *defaultUser, Avatar: *defaultAvatar}, []byte(*cookieSessionHash))

	gcl, err := newGithubClient()
	if err != nil {
		log.Panicf("Failed to build github client: %v", err)
	}

	ac := acl.Null
	if auth.Enabled() {
		ac = acl.NewGithub(gcl)
	}

	if err := os.Mkdir(*dataPath, 0777); err != nil && !os.IsExist(err) {
		log.Fatal("could not create data dir: ", err)
	}

	hub := notification.NewHub(ctx)
	ecl := etcd.NewClient([]string{*ETCDServer})

	assets := helpers.New(*staticFilePath)

	http.Handle("/", auth.Authenticate(HomeHandler{ac: ac, ecl: ecl, assets: assets}))
	http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, r.URL.Path[1:])
	})

	dph, err := deploypage.New(assets, fmt.Sprintf("ws://%s/web_push", *bindAddress))
	if err != nil {
		log.Fatal(err)
	}
	http.Handle("/deploy", auth.Authenticate(dph))
	http.Handle("/web_push", websocket.Handler(hub.AcceptConnection))

	dlh := DeployLogHandler{assets: assets}
	http.Handle("/deployLog/", auth.AuthenticateFunc(extractDeployLogHandler(ac, ecl, dlh.ServeHTTP)))
	http.Handle("/output/", auth.AuthenticateFunc(extractOutputHandler(DeployOutputHandler)))

	pch := ProjCommitsHandler{ac: ac, gcl: gcl, ecl: ecl}
	http.Handle("/commits/", auth.AuthenticateFunc(extractCommitHandler(pch.ServeHTTP)))
	http.Handle("/deploy_handler", auth.Authenticate(DeployHandler{ecl: ecl, hub: hub}))
	http.Handle("/lock", auth.Authenticate(lock.NewLock(ecl)))
	http.Handle("/unlock", auth.Authenticate(lock.NewUnlock(ecl)))
	http.Handle("/comment", auth.Authenticate(comment.New(ecl)))
	http.HandleFunc("/auth/github/login", auth.LoginHandler)
	http.HandleFunc("/auth/github/callback", auth.CallbackHandler)
	fmt.Printf("Running on %s\n", *bindAddress)
	log.Fatal(http.ListenAndServe(*bindAddress, nil))
}
Ejemplo n.º 2
0
func buildHandler(ctx context.Context) (http.Handler, error) {
	gcl, err := newGithubClient()
	if err != nil {
		glog.Errorf("Failed to build github client: %v", err)
		return nil, err
	}

	ac := acl.Null
	if auth.Enabled() {
		ac = acl.NewGithub(gcl)
	}

	if err := os.Mkdir(*dataPath, 0777); err != nil && !os.IsExist(err) {
		glog.Errorf("could not create data dir: %v", err)
		return nil, err
	}

	hub := notification.NewHub(ctx)
	ecl := etcd.NewClient([]string{*ETCDServer})
	assets := helpers.New(*staticFilePath)

	mux := http.NewServeMux()
	mux.Handle("/", auth.Authenticate(HomeHandler{ac: ac, ecl: ecl, assets: assets}))
	mux.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, r.URL.Path[1:])
	})

	dph, err := deploypage.New(assets, fmt.Sprintf("ws://%s/web_push", *bindAddress))
	if err != nil {
		glog.Errorf("Failed to build deploy page handler: %v", err)
		return nil, err
	}
	mux.Handle("/deploy", auth.Authenticate(dph))
	mux.Handle("/web_push", websocket.Handler(hub.AcceptConnection))

	dlh := DeployLogHandler{assets: assets}
	mux.Handle("/deployLog/", auth.AuthenticateFunc(extractDeployLogHandler(ac, ecl, dlh.ServeHTTP)))
	mux.Handle("/output/", auth.AuthenticateFunc(extractOutputHandler(DeployOutputHandler)))
	mux.Handle("/commits/", auth.Authenticate(commits.New(ac, ecl, gcl, *keyPath)))
	mux.Handle("/deploy_handler", auth.Authenticate(DeployHandler{ecl: ecl, hub: hub}))
	mux.Handle("/lock", auth.Authenticate(lock.NewLock(ecl)))
	mux.Handle("/unlock", auth.Authenticate(lock.NewUnlock(ecl)))
	mux.Handle("/comment", auth.Authenticate(comment.New(ecl)))
	mux.HandleFunc("/auth/github/login", auth.LoginHandler)
	mux.HandleFunc("/auth/github/callback", auth.CallbackHandler)

	return mux, nil
}