github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/compat/containers_restart.go (about)

     1  package compat
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/containers/podman/v2/libpod"
     7  	"github.com/containers/podman/v2/pkg/api/handlers/utils"
     8  	"github.com/gorilla/schema"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func RestartContainer(w http.ResponseWriter, r *http.Request) {
    13  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    14  	decoder := r.Context().Value("decoder").(*schema.Decoder)
    15  	// /{version}/containers/(name)/restart
    16  	query := struct {
    17  		Timeout int `schema:"t"`
    18  	}{
    19  		// Override golang default values for types
    20  	}
    21  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    22  		utils.BadRequest(w, "url", r.URL.String(), errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    23  		return
    24  	}
    25  
    26  	name := utils.GetName(r)
    27  	con, err := runtime.LookupContainer(name)
    28  	if err != nil {
    29  		utils.ContainerNotFound(w, name, err)
    30  		return
    31  	}
    32  
    33  	timeout := con.StopTimeout()
    34  	if _, found := r.URL.Query()["t"]; found {
    35  		timeout = uint(query.Timeout)
    36  	}
    37  
    38  	if err := con.RestartWithTimeout(r.Context(), timeout); err != nil {
    39  		utils.InternalServerError(w, err)
    40  		return
    41  	}
    42  
    43  	// Success
    44  	utils.WriteResponse(w, http.StatusNoContent, "")
    45  }