// RunServer runs the app
func RunServer() error {
	cnf, db, err := initConfigDB(true, true)
	if err != nil {
		return err
	}
	defer db.Close()
	if err := initServices(cnf, db); err != nil {
		return err
	}

	// Start a classic negroni app
	app := negroni.New()
	app.Use(negroni.NewRecovery())
	app.Use(negroni.NewLogger())
	app.Use(gzip.Gzip(gzip.DefaultCompression))
	app.Use(negroni.NewStatic(http.Dir("public")))

	// Create a router instance
	router := mux.NewRouter()

	// Add routes
	healthService.RegisterRoutes(router, "/v1")
	oauthService.RegisterRoutes(router, "/v1/oauth")
	webService.RegisterRoutes(router, "/web")

	// Set the router
	app.UseHandler(router)

	// Run the server on port 8080, gracefully stop on SIGTERM signal
	graceful.Run(":8080", 5*time.Second, app)

	return nil
}
Beispiel #2
0
func (r *Reactor) Serve(bind string) {
	handlers := append(r.handlers, negroni.NewStatic(public.AssetFS()))
	n := negroni.New(handlers...)

	router := httprouter.New()
	router.HandlerFunc("GET", "/ws", core.NewReactorHandler(func(uc chan *core.DisplayUpdate, ue chan *core.UserEvent, req *http.Request, id string) http.Header {

		go func() {

			path := "/"

		mainLoop:
			for {
				if path == "" {
					path = "/"
				}
				// TODO lock etc.

				screenFactory, params := r.findScreenFactoryForPath(path)

				ctx := ScreenContext{
					Path:         path,
					ConnectionID: id,
					Params:       params,
					UpdateScreen: func(upd *core.DisplayUpdate) {
						uc <- upd
					},
				}

				currentScreen := screenFactory(ctx)

				if currentScreen != nil {

					currentScreen.Mount()

					for evt := range ue {
						if evt.Type == "popstate" {
							newPath := strings.TrimPrefix(evt.Value, "#")
							if newPath != path {
								path = newPath
								currentScreen.Unmount()
								continue mainLoop
							}
						}
						currentScreen.OnUserEvent(evt)
					}
					currentScreen.Unmount()
					return
				}

			}
		}()

		return nil
	}))
	n.UseHandler(router)
	n.Run(bind)
}
// RunServer runs the app
func RunServer() error {
	cnf, db, err := initConfigDB(true, true)
	if err != nil {
		return err
	}
	defer db.Close()

	// Initialise the health service
	healthService := health.NewService(db)

	// Initialise the oauth service
	oauthService := oauth.NewService(cnf, db)

	// Initialise the web service
	webService := web.NewService(cnf, oauthService)

	// Start a classic negroni app
	app := negroni.New()
	app.Use(negroni.NewRecovery())
	app.Use(negroni.NewLogger())
	app.Use(gzip.Gzip(gzip.DefaultCompression))
	app.Use(negroni.NewStatic(http.Dir("public")))

	// Create a router instance
	router := mux.NewRouter()

	// Add routes for the health service (healthcheck endpoint)
	health.RegisterRoutes(router, healthService)

	// Add routes for the oauth service (REST tokens endpoint)
	oauth.RegisterRoutes(router, oauthService)

	// Add routes for the web package (register, login authorize web pages)
	web.RegisterRoutes(router, webService)

	// Set the router
	app.UseHandler(router)

	// Run the server on port 8080
	app.Run(":8080")

	return nil
}
Beispiel #4
0
func startHttpServer(apparatchick *core.Apparatchik, dockerClient *docker.Client) {
	api := &API{
		apparatchick: apparatchick,
		dockerClient: dockerClient,
	}
	router := httprouter.New()

	router.PUT("/api/v1.0/applications/:applicationName", api.CreateApplication)
	router.DELETE("/api/v1.0/applications/:applicationName", api.DeleteApplication)

	router.GET("/api/v1.0/applications", api.GetApplications)
	router.GET("/api/v1.0/applications/:applicationName", api.GetApplication)

	router.GET("/api/v1.0/applications/:applicationName/goals/:goalName/logs", api.GetGoalLogs)
	router.GET("/api/v1.0/applications/:applicationName/goals/:goalName/transition_log", api.GetGoalTransitionLog)
	router.GET("/api/v1.0/applications/:applicationName/goals/:goalName/stats", api.GetGoalStats)
	router.GET("/api/v1.0/applications/:applicationName/goals/:goalName/current_stats", api.GetGoalCurrentStats)
	router.GET("/api/v1.0/applications/:applicationName/goals/:goalName/inspect", api.GetGoalInspect)
	router.GET("/api/v1.0/applications/:applicationName/goals/:goalName/exec", api.ExecSocket)

	reactor := reactor.New(NewAuthHandler(), negroni.NewStatic(public.AssetFS()), &negroniHTTPRouter{router})

	bnd := ":8080"

	port := os.Getenv("PORT")

	if port != "" {
		bnd = ":" + port
	}

	reactor.AddScreen("/", ui.IndexFactory)
	reactor.AddScreen("/add_application", ui.AddApplicationFactory)
	reactor.AddScreen("/apps/:application", ui.ApplicationFactory)
	reactor.AddScreen("/apps/:application/:goal/xterm", ui.XTermFactory)
	reactor.AddScreen("/apps/:application/:goal", ui.GoalFactory)
	reactor.Serve(bnd)

}