本文整理匯總了Golang中github.com/flynn/flynn/pkg/httphelper.DecodeJSON函數的典型用法代碼示例。如果您正苦於以下問題:Golang DecodeJSON函數的具體用法?Golang DecodeJSON怎麽用?Golang DecodeJSON使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了DecodeJSON函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: servePutLeader
// servePutLeader sets the leader for a service.
func (h *Handler) servePutLeader(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
// Retrieve path parameters.
service := params.ByName("service")
// Check if the service allows manual leader election.
config := h.Store.Config(service)
if config == nil || config.LeaderType != discoverd.LeaderTypeManual {
hh.ValidationError(w, "", "service leader election type is not manual")
return
}
// Read instance from the request.
inst := &discoverd.Instance{}
if err := hh.DecodeJSON(r, inst); err != nil {
hh.Error(w, err)
return
}
// Manually set the leader on the service.
if err := h.Store.SetServiceLeader(service, inst.ID); err == ErrNotLeader {
h.redirectToLeader(w, r)
return
} else if err != nil {
hh.Error(w, err)
return
}
}
示例2: Update
func (h *jobAPI) Update(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log := h.host.log.New("fn", "Update")
log.Info("decoding command")
var cmd host.Command
if err := httphelper.DecodeJSON(req, &cmd); err != nil {
log.Error("error decoding command", "err", err)
httphelper.Error(w, err)
return
}
log.Info("updating host")
err := h.host.Update(&cmd)
if err != nil {
httphelper.Error(w, err)
return
}
// send an ok response and then shutdown after 1s to give the response
// chance to reach the client.
httphelper.JSON(w, http.StatusOK, cmd)
log.Info("shutting down in 1s")
time.AfterFunc(time.Second, func() {
log.Info("exiting")
os.Exit(0)
})
}
示例3: ResourceCheck
func (h *jobAPI) ResourceCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var req host.ResourceCheck
if err := httphelper.DecodeJSON(r, &req); err != nil {
httphelper.Error(w, err)
return
}
var conflicts []host.Port
for _, p := range req.Ports {
if p.Proto == "" {
p.Proto = "tcp"
}
if !checkPort(p) {
conflicts = append(conflicts, p)
}
}
if len(conflicts) > 0 {
resp := host.ResourceCheck{Ports: conflicts}
detail, err := json.Marshal(resp)
if err != nil {
httphelper.Error(w, err)
return
}
httphelper.JSON(w, 409, &httphelper.JSONError{
Code: httphelper.ConflictErrorCode,
Message: "Conflicting resources found",
Detail: detail,
})
return
}
httphelper.JSON(w, 200, struct{}{})
}
示例4: Pull
func (api *HTTPAPI) Pull(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
pull := &volume.PullCoordinate{}
if err := httphelper.DecodeJSON(r, &pull); err != nil {
httphelper.Error(w, err)
return
}
hostClient, err := api.cluster.Host(pull.HostID)
if err != nil {
httphelper.Error(w, err)
return
}
haves, err := api.vman.ListHaves(volumeID)
if err != nil {
httphelper.Error(w, err)
return
}
reader, err := hostClient.SendSnapshot(pull.SnapshotID, haves)
if err != nil {
httphelper.Error(w, err)
return
}
snap, err := api.vman.ReceiveSnapshot(volumeID, reader)
if err != nil {
httphelper.Error(w, err)
return
}
httphelper.JSON(w, 200, snap.Info())
}
示例5: servePutService
// servePutService creates a service.
func (h *Handler) servePutService(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
// Retrieve the path parameter.
service := params.ByName("service")
if err := ValidServiceName(service); err != nil {
hh.ValidationError(w, "", err.Error())
return
}
// Read config from the request.
config := &discoverd.ServiceConfig{}
if err := hh.DecodeJSON(r, config); err != nil {
hh.Error(w, err)
return
}
// Add the service to the store.
if err := h.Store.AddService(service, config); err == ErrNotLeader {
h.redirectToLeader(w, r)
return
} else if IsServiceExists(err) {
hh.ObjectExistsError(w, err.Error())
return
} else if err != nil {
hh.Error(w, err)
return
}
}
示例6: PutResource
func (c *controllerAPI) PutResource(ctx context.Context, w http.ResponseWriter, req *http.Request) {
params, _ := ctxhelper.ParamsFromContext(ctx)
p, err := c.getProvider(ctx)
if err != nil {
respondWithError(w, err)
return
}
var resource ct.Resource
if err = httphelper.DecodeJSON(req, &resource); err != nil {
respondWithError(w, err)
return
}
resource.ID = params.ByName("resources_id")
resource.ProviderID = p.ID
if err := schema.Validate(resource); err != nil {
respondWithError(w, err)
return
}
if err := c.resourceRepo.Add(&resource); err != nil {
respondWithError(w, err)
return
}
httphelper.JSON(w, 200, &resource)
}
示例7: NewCredential
func (api *httpAPI) NewCredential(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
creds := &Credential{}
if err := httphelper.DecodeJSON(req, &creds); err != nil {
httphelper.Error(w, err)
return
}
if creds.Type == "azure" {
oauthCreds := make([]*OAuthCredential, 0, 2)
for _, resource := range []string{azure.JSONAPIResource, azure.XMLAPIResource} {
token, err := azure.OAuth2Config(creds.ID, creds.Endpoint, resource).Exchange(oauth2.NoContext, creds.Secret)
if err != nil {
httphelper.Error(w, err)
return
}
oauthCreds = append(oauthCreds, &OAuthCredential{
ClientID: creds.ID,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
ExpiresAt: &token.Expiry,
Scope: resource,
})
}
creds.Secret = ""
creds.OAuthCreds = oauthCreds
}
if err := api.Installer.SaveCredentials(creds); err != nil {
if err == credentialExistsError {
httphelper.ObjectExistsError(w, err.Error())
return
}
httphelper.Error(w, err)
return
}
w.WriteHeader(200)
}
示例8: PutFormation
func (c *controllerAPI) PutFormation(ctx context.Context, w http.ResponseWriter, req *http.Request) {
app := c.getApp(ctx)
release, err := c.getRelease(ctx)
if err != nil {
respondWithError(w, err)
return
}
var formation ct.Formation
if err = httphelper.DecodeJSON(req, &formation); err != nil {
respondWithError(w, err)
return
}
if release.ArtifactID == "" {
respondWithError(w, ct.ValidationError{Message: "release is not deployable"})
return
}
formation.AppID = app.ID
formation.ReleaseID = release.ID
if err = schema.Validate(formation); err != nil {
respondWithError(w, err)
return
}
if err = c.formationRepo.Add(&formation); err != nil {
respondWithError(w, err)
return
}
httphelper.JSON(w, 200, &formation)
}
示例9: ConfigureNetworking
func (h *jobAPI) ConfigureNetworking(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log := h.host.log.New("fn", "ConfigureNetworking")
log.Info("decoding config")
config := &host.NetworkConfig{}
if err := httphelper.DecodeJSON(r, config); err != nil {
log.Error("error decoding config", "err", err)
shutdown.Fatal(err)
}
// configure the network before returning a response in case the
// network coordinator requires the bridge to be created (e.g.
// when using flannel with the "alloc" backend)
h.host.networkOnce.Do(func() {
log.Info("configuring network", "subnet", config.Subnet, "mtu", config.MTU, "resolvers", config.Resolvers)
if err := h.host.backend.ConfigureNetworking(config); err != nil {
log.Error("error configuring network", "err", err)
shutdown.Fatal(err)
}
h.host.statusMtx.Lock()
h.host.status.Network = config
h.host.statusMtx.Unlock()
})
}
示例10: CreateProvider
func (api *HTTPAPI) CreateProvider(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
pspec := &volume.ProviderSpec{}
if err := httphelper.DecodeJSON(r, &pspec); err != nil {
httphelper.Error(w, err)
return
}
if pspec.ID == "" {
pspec.ID = random.UUID()
}
if pspec.Kind == "" {
httphelper.ValidationError(w, "kind", "must not be blank")
return
}
var provider volume.Provider
provider, err := volumemanager.NewProvider(pspec)
if err == volume.UnknownProviderKind {
httphelper.ValidationError(w, "kind", fmt.Sprintf("%q is not known", pspec.Kind))
return
}
if err := api.vman.AddProvider(pspec.ID, provider); err != nil {
switch err {
case volumemanager.ErrProviderExists:
httphelper.ObjectExistsError(w, fmt.Sprintf("provider %q already exists", pspec.ID))
return
default:
httphelper.Error(w, err)
return
}
}
httphelper.JSON(w, 200, pspec)
}
示例11: Send
func (api *HTTPAPI) Send(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
if !strings.Contains(r.Header.Get("Accept"), snapshotContentType) {
httphelper.ValidationError(w, "", fmt.Sprintf("must be prepared to accept a content type of %q", snapshotContentType))
return
}
w.Header().Set("Content-Type", snapshotContentType)
var haves []json.RawMessage
if err := httphelper.DecodeJSON(r, &haves); err != nil {
httphelper.Error(w, err)
return
}
err := api.vman.SendSnapshot(volumeID, haves, w)
if err != nil {
switch err {
case volume.ErrNoSuchVolume:
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume with id %q", volumeID))
return
default:
httphelper.Error(w, err)
return
}
}
}
示例12: SetAppRelease
func (c *controllerAPI) SetAppRelease(ctx context.Context, w http.ResponseWriter, req *http.Request) {
var rid releaseID
if err := httphelper.DecodeJSON(req, &rid); err != nil {
respondWithError(w, err)
return
}
rel, err := c.releaseRepo.Get(rid.ID)
if err != nil {
if err == ErrNotFound {
err = ct.ValidationError{
Message: fmt.Sprintf("could not find release with ID %s", rid.ID),
}
}
respondWithError(w, err)
return
}
release := rel.(*ct.Release)
if err := schema.Validate(release); err != nil {
respondWithError(w, err)
return
}
app := c.getApp(ctx)
c.appRepo.SetRelease(app, release.ID)
httphelper.JSON(w, 200, release)
}
示例13: UpdateApp
func (c *controllerAPI) UpdateApp(ctx context.Context, rw http.ResponseWriter, req *http.Request) {
params, _ := ctxhelper.ParamsFromContext(ctx)
var data appUpdate
if err := httphelper.DecodeJSON(req, &data); err != nil {
respondWithError(rw, err)
return
}
if v, ok := data["meta"]; ok && v == nil {
// handle {"meta": null}
delete(data, "meta")
}
if err := schema.Validate(data); err != nil {
respondWithError(rw, err)
return
}
app, err := c.appRepo.Update(params.ByName("apps_id"), data)
if err != nil {
respondWithError(rw, err)
return
}
httphelper.JSON(rw, 200, app)
}
示例14: UpdateAppMeta
func (c *controllerAPI) UpdateAppMeta(ctx context.Context, rw http.ResponseWriter, req *http.Request) {
params, _ := ctxhelper.ParamsFromContext(ctx)
var data appUpdate
if err := httphelper.DecodeJSON(req, &data); err != nil {
respondWithError(rw, err)
return
}
if err := schema.Validate(data); err != nil {
respondWithError(rw, err)
return
}
if data["meta"] == nil {
data["meta"] = make(map[string]interface{})
}
app, err := c.appRepo.Update(params.ByName("apps_id"), data)
if err != nil {
respondWithError(rw, err)
return
}
httphelper.JSON(rw, 200, app)
}
示例15: ConfigureDiscoverd
func (h *jobAPI) ConfigureDiscoverd(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log := h.host.log.New("fn", "ConfigureDiscoverd")
log.Info("decoding config")
var config host.DiscoverdConfig
if err := httphelper.DecodeJSON(r, &config); err != nil {
log.Error("error decoding config", "err", err)
httphelper.Error(w, err)
return
}
log.Info("config decoded", "url", config.URL, "dns", config.DNS)
h.host.statusMtx.Lock()
h.host.status.Discoverd = &config
h.host.statusMtx.Unlock()
if config.URL != "" && config.DNS != "" {
go h.host.discoverdOnce.Do(func() {
log.Info("connecting to service discovery", "url", config.URL)
if err := h.host.discMan.ConnectLocal(config.URL); err != nil {
log.Error("error connecting to service discovery", "err", err)
shutdown.Fatal(err)
}
})
}
}