func (h *Server) DeleteProcesses(ctx context.Context, w http.ResponseWriter, r *http.Request) error { vars := httpx.Vars(ctx) pid := vars["pid"] if vars["ptype"] != "" { return errNotImplemented("Restarting a process type is currently not implemented.") } a, err := findApp(ctx, h) if err != nil { return err } m, err := findMessage(r) if err != nil { return err } if err := h.Restart(ctx, empire.RestartOpts{ User: auth.UserFromContext(ctx), App: a, PID: pid, Message: m, }); err != nil { return err } return NoContent(w) }
func newDeployOpts(ctx context.Context, w http.ResponseWriter, req *http.Request) (*empire.DeployOpts, error) { var form PostDeployForm if err := Decode(req, &form); err != nil { return nil, err } m, err := findMessage(req) if err != nil { return nil, err } w.Header().Set("Content-Type", "application/json; boundary=NL") if form.Image.Tag == "" && form.Image.Digest == "" { form.Image.Tag = "latest" } opts := empire.DeployOpts{ User: auth.UserFromContext(ctx), Image: form.Image, Output: empire.NewDeploymentStream(streamhttp.StreamingResponseWriter(w)), Message: m, Stream: form.Stream, } return &opts, nil }
func (h *Server) PostReleases(ctx context.Context, w http.ResponseWriter, r *http.Request) error { var form PostReleasesForm if err := Decode(r, &form); err != nil { return err } version, err := form.ReleaseVersion() if err != nil { return err } app, err := findApp(ctx, h) if err != nil { return err } m, err := findMessage(r) if err != nil { return err } release, err := h.Rollback(ctx, empire.RollbackOpts{ User: auth.UserFromContext(ctx), App: app, Version: version, Message: m, }) if err != nil { return err } w.WriteHeader(200) return Encode(w, newRelease(release)) }
func (h *Server) PatchConfigs(ctx context.Context, w http.ResponseWriter, r *http.Request) error { var configVars empire.Vars if err := Decode(r, &configVars); err != nil { return err } a, err := findApp(ctx, h) if err != nil { return err } m, err := findMessage(r) if err != nil { return err } // Update the config c, err := h.Set(ctx, empire.SetOpts{ User: auth.UserFromContext(ctx), App: a, Vars: configVars, Message: m, }) if err != nil { return err } w.WriteHeader(200) return Encode(w, c.Vars) }
func (h *Server) PostAuthorizations(ctx context.Context, w http.ResponseWriter, r *http.Request) error { at, err := h.AccessTokensCreate(&AccessToken{ User: auth.UserFromContext(ctx), }) if err != nil { return err } return Encode(w, newAuthorization(at)) }
func withMetrics(handlerName string, h httpx.HandlerFunc) httpx.Handler { return httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { tags := []string{ fmt.Sprintf("handler:%s", handlerName), fmt.Sprintf("user:%s", auth.UserFromContext(ctx).Name), } start := time.Now() err := h(ctx, w, r) d := time.Since(start) stats.Timing(ctx, fmt.Sprintf("heroku.request"), d, 1.0, tags) stats.Timing(ctx, fmt.Sprintf("heroku.request.%s", handlerName), d, 1.0, tags) return err }) }
func (h *Server) PatchFormation(ctx context.Context, w http.ResponseWriter, r *http.Request) error { var form PatchFormationForm if err := Decode(r, &form); err != nil { return err } app, err := findApp(ctx, h) if err != nil { return err } m, err := findMessage(r) if err != nil { return err } var updates []*empire.ProcessUpdate for _, up := range form.Updates { updates = append(updates, &empire.ProcessUpdate{ Process: up.Process, Quantity: up.Quantity, Constraints: up.Size, }) } ps, err := h.Scale(ctx, empire.ScaleOpts{ User: auth.UserFromContext(ctx), App: app, Updates: updates, Message: m, }) if err != nil { return err } var resp []*Formation for i, p := range ps { up := updates[i] resp = append(resp, &Formation{ Type: up.Process, Quantity: p.Quantity, Size: p.Constraints().String(), }) } w.WriteHeader(200) return Encode(w, resp) }
func (h *Server) DeleteApp(ctx context.Context, w http.ResponseWriter, r *http.Request) error { a, err := findApp(ctx, h) if err != nil { return err } m, err := findMessage(r) if err != nil { return err } if err := h.Destroy(ctx, empire.DestroyOpts{ User: auth.UserFromContext(ctx), App: a, Message: m, }); err != nil { return err } return NoContent(w) }
func (h *Server) PostApps(ctx context.Context, w http.ResponseWriter, r *http.Request) error { var form PostAppsForm if err := Decode(r, &form); err != nil { return err } m, err := findMessage(r) if err != nil { return err } a, err := h.Create(ctx, empire.CreateOpts{ User: auth.UserFromContext(ctx), Name: form.Name, Message: m, }) if err != nil { return err } w.WriteHeader(201) return Encode(w, newApp(a)) }
// ensureUserInContext returns and httpx.Handler that raises an error if the // user isn't set in the context. func ensureUserInContext(t testing.TB) httpx.Handler { return httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { auth.UserFromContext(ctx) // Panics if user is not set. return nil }) }
func (h *Server) PostProcess(ctx context.Context, w http.ResponseWriter, r *http.Request) error { var form PostProcessForm a, err := findApp(ctx, h) if err != nil { return err } m, err := findMessage(r) if err != nil { return err } if err := Decode(r, &form); err != nil { return err } command, err := empire.ParseCommand(form.Command) if err != nil { return err } opts := empire.RunOpts{ User: auth.UserFromContext(ctx), App: a, Command: command, Env: form.Env, Constraints: form.Size, Message: m, } if form.Attach { header := http.Header{} header.Set("Content-Type", "application/vnd.empire.raw-stream") stream := &hijack.HijackReadWriter{ Response: w, Header: header, } defer stream.Close() // Prevent the ELB idle connection timeout to close the connection. defer close(streamhttp.Heartbeat(stream, 10*time.Second)) opts.Input = stream opts.Output = stream if err := h.Run(ctx, opts); err != nil { if stream.Hijacked { fmt.Fprintf(stream, "%v\r", err) return nil } return err } } else { if err := h.Run(ctx, opts); err != nil { return err } dyno := &heroku.Dyno{ Name: "run", Command: form.Command, CreatedAt: timex.Now(), } w.WriteHeader(201) return Encode(w, dyno) } return nil }