func APIHandler(conf *Config) http.Handler { api := &API{ conf: conf, } if conf.Cache { if err := api.cacheDashboardJS(); err != nil { panic(err) } } router := httprouter.New() router2 := httprouter.New() prefixPath := func(p string) string { return path.Join(conf.PathPrefix, p) } router.HandlerFunc("GET", status.Path, status.HealthyHandler.ServeHTTP) router.POST(prefixPath("/user/sessions"), api.WrapHandler(api.Login)) router.DELETE(prefixPath("/user/session"), api.WrapHandler(api.Logout)) router.GET(prefixPath("/config"), api.WrapHandler(api.GetConfig)) router.NotFound = router2.ServeHTTP router2.GET(prefixPath("/*path"), api.WrapHandler(api.ServeAsset)) return httphelper.ContextInjector("dashboard", httphelper.NewRequestLogger( api.CorsHandler(router))) }
func main() { defer shutdown.Exit() db := postgres.Wait(&postgres.Conf{ Service: serviceName, User: "******", Password: os.Getenv("PGPASSWORD"), Database: "postgres", }, nil) api := &pgAPI{db} router := httprouter.New() router.POST("/databases", httphelper.WrapHandler(api.createDatabase)) router.DELETE("/databases", httphelper.WrapHandler(api.dropDatabase)) router.GET("/ping", httphelper.WrapHandler(api.ping)) port := os.Getenv("PORT") if port == "" { port = "3000" } addr := ":" + port hb, err := discoverd.AddServiceAndRegister(serviceName+"-api", addr) if err != nil { shutdown.Fatal(err) } shutdown.BeforeExit(func() { hb.Close() }) handler := httphelper.ContextInjector(serviceName+"-api", httphelper.NewRequestLogger(router)) shutdown.Fatal(http.ListenAndServe(addr, handler)) }
// NewHandler returns a new instance of Handler. func NewHandler() *Handler { r := httprouter.New() h := &Handler{Handler: r} if os.Getenv("DEBUG") != "" { h.Handler = hh.ContextInjector("discoverd", hh.NewRequestLogger(h.Handler)) } r.HandlerFunc("GET", status.Path, status.HealthyHandler.ServeHTTP) r.PUT("/services/:service", h.servePutService) r.DELETE("/services/:service", h.serveDeleteService) r.GET("/services/:service", h.serveGetService) r.PUT("/services/:service/meta", h.servePutServiceMeta) r.GET("/services/:service/meta", h.serveGetServiceMeta) r.PUT("/services/:service/instances/:instance_id", h.servePutInstance) r.DELETE("/services/:service/instances/:instance_id", h.serveDeleteInstance) r.GET("/services/:service/instances", h.serveGetInstances) r.PUT("/services/:service/leader", h.servePutLeader) r.GET("/services/:service/leader", h.serveGetLeader) r.GET("/raft/leader", h.serveGetRaftLeader) r.POST("/raft/nodes", h.servePostRaftNodes) r.DELETE("/raft/nodes", h.serveDeleteRaftNodes) r.GET("/ping", h.servePing) return h }
func main() { defer shutdown.Exit() api := &API{} router := httprouter.New() router.POST("/databases", api.createDatabase) router.DELETE("/databases", api.dropDatabase) router.GET("/ping", api.ping) port := os.Getenv("PORT") if port == "" { port = "3000" } addr := ":" + port hb, err := discoverd.AddServiceAndRegister(serviceName+"-api", addr) if err != nil { shutdown.Fatal(err) } shutdown.BeforeExit(func() { hb.Close() }) handler := httphelper.ContextInjector(serviceName+"-api", httphelper.NewRequestLogger(router)) shutdown.Fatal(http.ListenAndServe(addr, handler)) }
func main() { defer shutdown.Exit() dsn := &mariadb.DSN{ Host: serviceHost + ":3306", User: "******", Password: os.Getenv("MYSQL_PWD"), Database: "mysql", } db, err := sql.Open("mysql", dsn.String()) api := &API{db} router := httprouter.New() router.POST("/databases", httphelper.WrapHandler(api.createDatabase)) router.DELETE("/databases", httphelper.WrapHandler(api.dropDatabase)) router.GET("/ping", httphelper.WrapHandler(api.ping)) port := os.Getenv("PORT") if port == "" { port = "3000" } addr := ":" + port hb, err := discoverd.AddServiceAndRegister(serviceName+"-api", addr) if err != nil { shutdown.Fatal(err) } shutdown.BeforeExit(func() { hb.Close() }) handler := httphelper.ContextInjector(serviceName+"-api", httphelper.NewRequestLogger(router)) shutdown.Fatal(http.ListenAndServe(addr, handler)) }
func runServer(_ *docopt.Args) error { addr := ":" + os.Getenv("PORT") db := postgres.Wait(nil, nil) if err := dbMigrations.Migrate(db); err != nil { return fmt.Errorf("error running DB migrations: %s", err) } mux := http.NewServeMux() repo, err := data.NewFileRepoFromEnv(db) if err != nil { return err } hb, err := discoverd.AddServiceAndRegister("blobstore", addr) if err != nil { return err } shutdown.BeforeExit(func() { hb.Close() }) log.Println("Blobstore serving files on " + addr) mux.Handle("/", handler(repo)) mux.Handle(status.Path, status.Handler(func() status.Status { if err := db.Exec("SELECT 1"); err != nil { return status.Unhealthy } return status.Healthy })) h := httphelper.ContextInjector("blobstore", httphelper.NewRequestLogger(mux)) return http.ListenAndServe(addr, h) }
func apiHandler(agg *Aggregator) http.Handler { api := aggregatorAPI{agg: agg} r := httprouter.New() r.GET("/log/:channel_id", httphelper.WrapHandler(api.GetLog)) return httphelper.ContextInjector( "logaggregator-api", httphelper.NewRequestLogger(r), ) }
func main() { key := os.Getenv("CONTROLLER_KEY") if key == "" { log.Fatal("missing CONTROLLER_KEY env var") } cc, err := controller.NewClient("", key) if err != nil { log.Fatalln("Unable to connect to controller:", err) } log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), httphelper.ContextInjector("gitreceive", httphelper.NewRequestLogger(newGitHandler(cc, []byte(key)))))) }
func apiHandler(agg *Aggregator, cursors *HostCursors) http.Handler { api := aggregatorAPI{agg, cursors} r := httprouter.New() r.Handler("GET", status.Path, status.HealthyHandler) r.GET("/log/:channel_id", httphelper.WrapHandler(api.GetLog)) r.GET("/cursors", httphelper.WrapHandler(api.GetCursors)) return httphelper.ContextInjector( "logaggregator-api", httphelper.NewRequestLogger(r), ) }
func (h *Host) ServeHTTP() { r := httprouter.New() r.POST("/attach", (&attachHandler{state: h.state, backend: h.backend}).ServeHTTP) jobAPI := &jobAPI{host: h} jobAPI.RegisterRoutes(r) volAPI := volumeapi.NewHTTPAPI(cluster.NewClient(), h.vman) volAPI.RegisterRoutes(r) go http.Serve(h.listener, httphelper.ContextInjector("host", httphelper.NewRequestLogger(r))) }
func (h *Host) ServeHTTP() { r := httprouter.New() r.POST("/attach", newAttachHandler(h.state, h.backend, h.log).ServeHTTP) jobAPI := &jobAPI{ host: h, addJobRateLimitBucket: NewRateLimitBucket(h.maxJobConcurrency), } jobAPI.RegisterRoutes(r) h.volAPI.RegisterRoutes(r) go http.Serve(h.listener, httphelper.ContextInjector("host", httphelper.NewRequestLogger(r))) }
func (h *Host) ServeHTTP() { r := httprouter.New() r.POST("/attach", newAttachHandler(h.state, h.backend, h.log).ServeHTTP) jobAPI := &jobAPI{ host: h, addJobRatelimitBucket: make(chan struct{}, h.maxJobConcurrency), } jobAPI.RegisterRoutes(r) volAPI := volumeapi.NewHTTPAPI(cluster.NewClient(), h.vman) volAPI.RegisterRoutes(r) go http.Serve(h.listener, httphelper.ContextInjector("host", httphelper.NewRequestLogger(r))) }
func apiHandler(rtr *Router) http.Handler { api := &API{router: rtr} r := httprouter.New() r.HandlerFunc("GET", status.Path, status.HealthyHandler.ServeHTTP) r.POST("/routes", httphelper.WrapHandler(api.CreateRoute)) r.PUT("/routes/:route_type/:id", httphelper.WrapHandler(api.UpdateRoute)) r.GET("/routes", httphelper.WrapHandler(api.GetRoutes)) r.GET("/routes/:route_type/:id", httphelper.WrapHandler(api.GetRoute)) r.DELETE("/routes/:route_type/:id", httphelper.WrapHandler(api.DeleteRoute)) r.GET("/events", httphelper.WrapHandler(api.StreamEvents)) r.HandlerFunc("GET", "/debug/*path", pprof.Handler.ServeHTTP) return httphelper.ContextInjector("router", httphelper.NewRequestLogger(r)) }
func main() { defer shutdown.Exit() flag.Parse() addr := os.Getenv("PORT") if addr == "" { addr = *listenPort } addr = ":" + addr var fs Filesystem var storageDesc string if *storageDir != "" { fs = NewOSFilesystem(*storageDir) storageDesc = *storageDir } else { var err error db := postgres.Wait(nil, nil) fs, err = NewPostgresFilesystem(db) if err != nil { shutdown.Fatal(err) } storageDesc = "Postgres" } if *serviceDiscovery { hb, err := discoverd.AddServiceAndRegister("blobstore", addr) if err != nil { shutdown.Fatal(err) } shutdown.BeforeExit(func() { hb.Close() }) } log.Println("Blobstore serving files on " + addr + " from " + storageDesc) mux := http.NewServeMux() mux.Handle("/", handler(fs)) mux.Handle(status.Path, status.Handler(fs.Status)) h := httphelper.ContextInjector("blobstore", httphelper.NewRequestLogger(mux)) shutdown.Fatal(http.ListenAndServe(addr, h)) }
// NewHandler returns a new instance of Handler. func NewHandler(proxy bool, peers []string) *Handler { r := httprouter.New() h := &Handler{Handler: r} h.Shutdown.Store(false) h.Peers = peers h.Proxy.Store(proxy) if os.Getenv("DEBUG") != "" { h.Handler = hh.ContextInjector("discoverd", hh.NewRequestLoggerCustom(h.Handler, loggerFn)) } r.HandlerFunc("GET", status.Path, status.HealthyHandler.ServeHTTP) r.PUT("/services/:service", h.servePutService) r.DELETE("/services/:service", h.serveDeleteService) r.GET("/services/:service", h.serveGetService) r.PUT("/services/:service/meta", h.servePutServiceMeta) r.GET("/services/:service/meta", h.serveGetServiceMeta) r.PUT("/services/:service/instances/:instance_id", h.servePutInstance) r.DELETE("/services/:service/instances/:instance_id", h.serveDeleteInstance) r.GET("/services/:service/instances", h.serveGetInstances) r.PUT("/services/:service/leader", h.servePutLeader) r.GET("/services/:service/leader", h.serveGetLeader) r.GET("/raft/leader", h.serveGetRaftLeader) r.GET("/raft/peers", h.serveGetRaftPeers) r.PUT("/raft/peers/:peer", h.servePutRaftPeer) r.DELETE("/raft/peers/:peer", h.serveDeleteRaftPeer) r.POST("/raft/promote", h.servePromote) r.POST("/raft/demote", h.serveDemote) r.GET("/ping", h.servePing) r.POST("/shutdown", h.serveShutdown) return h }
// Run executes the program. func (m *Main) Run() error { // Open HTTP port. ln, err := net.Listen("tcp", m.Addr) if err != nil { return err } m.ln = ln // Initialize handler logger. m.Handler.Logger = m.Logger.New("component", "http") // Register service with discoverd. hb, err := discoverd.AddServiceAndRegister(m.ServiceName+"-api", m.Addr) if err != nil { return err } shutdown.BeforeExit(func() { hb.Close() }) h := httphelper.ContextInjector(m.ServiceName+"-api", httphelper.NewRequestLogger(m.Handler)) go func() { http.Serve(ln, h) }() return nil }
func serveHTTP(h *Host, addr string, attach *attachHandler, clus *cluster.Client, vman *volumemanager.Manager, connectDiscoverd func(string) error) error { l, err := net.Listen("tcp", addr) if err != nil { return err } shutdown.BeforeExit(func() { l.Close() }) r := httprouter.New() r.POST("/attach", attach.ServeHTTP) jobAPI := &jobAPI{ host: h, connectDiscoverd: connectDiscoverd, } jobAPI.RegisterRoutes(r) volAPI := volumeapi.NewHTTPAPI(clus, vman) volAPI.RegisterRoutes(r) go http.Serve(l, httphelper.ContextInjector("host", httphelper.NewRequestLogger(r))) return nil }
func appHandler(c handlerConfig) http.Handler { err := schema.Load(schemaRoot) if err != nil { shutdown.Fatal(err) } q := que.NewClient(c.pgxpool) providerRepo := NewProviderRepo(c.db) keyRepo := NewKeyRepo(c.db) resourceRepo := NewResourceRepo(c.db) appRepo := NewAppRepo(c.db, os.Getenv("DEFAULT_ROUTE_DOMAIN"), c.rc) artifactRepo := NewArtifactRepo(c.db) releaseRepo := NewReleaseRepo(c.db) jobRepo := NewJobRepo(c.db) formationRepo := NewFormationRepo(c.db, appRepo, releaseRepo, artifactRepo) deploymentRepo := NewDeploymentRepo(c.db, c.pgxpool) api := controllerAPI{ appRepo: appRepo, releaseRepo: releaseRepo, providerRepo: providerRepo, formationRepo: formationRepo, artifactRepo: artifactRepo, jobRepo: jobRepo, resourceRepo: resourceRepo, deploymentRepo: deploymentRepo, clusterClient: c.cc, logaggc: c.lc, routerc: c.rc, que: q, } httpRouter := httprouter.New() crud(httpRouter, "apps", ct.App{}, appRepo) crud(httpRouter, "releases", ct.Release{}, releaseRepo) crud(httpRouter, "providers", ct.Provider{}, providerRepo) crud(httpRouter, "artifacts", ct.Artifact{}, artifactRepo) crud(httpRouter, "keys", ct.Key{}, keyRepo) httpRouter.POST("/apps/:apps_id", httphelper.WrapHandler(api.UpdateApp)) httpRouter.GET("/apps/:apps_id/log", httphelper.WrapHandler(api.appLookup(api.AppLog))) httpRouter.GET("/apps/:apps_id/events", httphelper.WrapHandler(api.appLookup(api.AppEvents))) httpRouter.DELETE("/apps/:apps_id", httphelper.WrapHandler(api.appLookup(api.DeleteApp))) httpRouter.PUT("/apps/:apps_id/formations/:releases_id", httphelper.WrapHandler(api.appLookup(api.PutFormation))) httpRouter.GET("/apps/:apps_id/formations/:releases_id", httphelper.WrapHandler(api.appLookup(api.GetFormation))) httpRouter.DELETE("/apps/:apps_id/formations/:releases_id", httphelper.WrapHandler(api.appLookup(api.DeleteFormation))) httpRouter.GET("/apps/:apps_id/formations", httphelper.WrapHandler(api.appLookup(api.ListFormations))) httpRouter.GET("/formations", httphelper.WrapHandler(api.GetFormations)) httpRouter.POST("/apps/:apps_id/jobs", httphelper.WrapHandler(api.appLookup(api.RunJob))) httpRouter.GET("/apps/:apps_id/jobs/:jobs_id", httphelper.WrapHandler(api.appLookup(api.GetJob))) httpRouter.PUT("/apps/:apps_id/jobs/:jobs_id", httphelper.WrapHandler(api.appLookup(api.PutJob))) httpRouter.GET("/apps/:apps_id/jobs", httphelper.WrapHandler(api.appLookup(api.ListJobs))) httpRouter.DELETE("/apps/:apps_id/jobs/:jobs_id", httphelper.WrapHandler(api.appLookup(api.KillJob))) httpRouter.POST("/apps/:apps_id/deploy", httphelper.WrapHandler(api.appLookup(api.CreateDeployment))) httpRouter.GET("/deployments/:deployment_id", httphelper.WrapHandler(api.GetDeployment)) httpRouter.PUT("/apps/:apps_id/release", httphelper.WrapHandler(api.appLookup(api.SetAppRelease))) httpRouter.GET("/apps/:apps_id/release", httphelper.WrapHandler(api.appLookup(api.GetAppRelease))) httpRouter.GET("/apps/:apps_id/releases", httphelper.WrapHandler(api.appLookup(api.GetAppReleases))) httpRouter.POST("/providers/:providers_id/resources", httphelper.WrapHandler(api.ProvisionResource)) httpRouter.GET("/providers/:providers_id/resources", httphelper.WrapHandler(api.GetProviderResources)) httpRouter.GET("/providers/:providers_id/resources/:resources_id", httphelper.WrapHandler(api.GetResource)) httpRouter.PUT("/providers/:providers_id/resources/:resources_id", httphelper.WrapHandler(api.PutResource)) httpRouter.DELETE("/providers/:providers_id/resources/:resources_id", httphelper.WrapHandler(api.DeleteResource)) httpRouter.GET("/apps/:apps_id/resources", httphelper.WrapHandler(api.appLookup(api.GetAppResources))) httpRouter.POST("/apps/:apps_id/routes", httphelper.WrapHandler(api.appLookup(api.CreateRoute))) httpRouter.GET("/apps/:apps_id/routes", httphelper.WrapHandler(api.appLookup(api.GetRouteList))) httpRouter.GET("/apps/:apps_id/routes/:routes_type/:routes_id", httphelper.WrapHandler(api.appLookup(api.GetRoute))) httpRouter.DELETE("/apps/:apps_id/routes/:routes_type/:routes_id", httphelper.WrapHandler(api.appLookup(api.DeleteRoute))) httpRouter.POST("/apps/:apps_id/meta", httphelper.WrapHandler(api.appLookup(api.UpdateAppMeta))) return httphelper.ContextInjector("controller", httphelper.NewRequestLogger(muxHandler(httpRouter, c.keys))) }