当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.JSON方法代码示例

本文整理汇总了Golang中github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo.Context.JSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.JSON方法的具体用法?Golang Context.JSON怎么用?Golang Context.JSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo.Context的用法示例。


在下文中一共展示了Context.JSON方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: addLabel

//
// POST /library/labels
//
func addLabel(c *echo.Context) error {
	name := strings.TrimPrefix(form(c, "name"), "label/")
	if name == "" {
		return c.String(400, "Name too short")
	}

	label := Label{
		Name:     name,
		Content:  form(c, "content"),
		Expanded: form(c, "expanded") == "true",
	}

	user := c.Get("user").(*User)
	err := store.SaveLabel(&label, user.ID)
	if err == ErrLabelExists {
		return c.String(409, "Existing label")
	} else if err != nil {
		return err
	}

	return c.JSON(200, addedLabel{
		ID:   label.ID,
		Name: label.Name,
	})
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:28,代码来源:labels.go

示例2: createUser

func createUser(c *echo.Context) error {
	u := new(user)
	if err := c.Bind(u); err != nil {
		return err
	}
	users[u.ID] = *u
	return c.JSON(http.StatusCreated, u)
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:8,代码来源:server.go

示例3: getEpisode

//
// GET /library/episode/:id
//
func getEpisode(c *echo.Context) error {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	return c.JSON(200, store.GetEpisode(id))
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:11,代码来源:episodes.go

示例4: updateUser

func updateUser(c *echo.Context) error {
	u := new(user)
	if err := c.Bind(u); err != nil {
		return err
	}
	id, _ := strconv.Atoi(c.Param("id"))
	users[id].Name = u.Name
	return c.JSON(http.StatusOK, users[id])
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:9,代码来源:server.go

示例5: addCast

//
// POST /library/casts
//
func addCast(c *echo.Context) error {
	user := c.Get("user").(*User)
	url := form(c, "feedurl")
	cast := store.GetCastByURL(url)
	if cast == nil {
		cast = <-crawl.fetch(url)
		if cast == nil {
			return c.String(500, "Could not fetch feed")
		}
	}

	user, err := store.AddSubscription(user.ID, cast.ID)
	if err != nil && err != ErrSubscriptionExists {
		return err
	}

	authCache.set(c.Get("token").(string), user)

	return c.JSON(200, cast)
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:23,代码来源:casts.go

示例6: getNewEpisodes

//
// GET /library/newepisodes
//
func getNewEpisodes(c *echo.Context) error {
	now := time.Now().Unix()
	user := c.Get("user").(*User)
	since := c.Request().URL.Query().Get("since")
	if since == "" {
		return c.JSON(200, newEpisodes{
			Timestamp: now,
			Episodes:  store.GetEpisodesSince(0, user.Subscriptions),
		})
	}

	ts, err := strconv.ParseInt(since, 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	return c.JSON(200, newEpisodes{
		Timestamp: now,
		Episodes:  store.GetEpisodesSince(ts, user.Subscriptions),
	})
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:24,代码来源:episodes.go

示例7: login

//
// POST /account/login
//
func login(c *echo.Context) error {
	if !formContains(c, "username", "password", "uuid", "clientname") {
		return echo.NewHTTPError(400)
	}

	user := store.GetUser(form(c, "username"))
	if user == nil {
		return echo.NewHTTPError(401)
	}

	if correctPassword(user.Password, form(c, "password")) {
		uuid := form(c, "uuid")
		for _, client := range user.Clients {
			if uuid == client.UUID {
				return c.JSON(200, token{client.Token})
			}
		}

		t, err := createToken(32)
		if err != nil {
			return err
		}

		err = store.AddClient(user.ID, &Client{
			Token: t,
			UUID:  uuid,
			Name:  form(c, "clientname"),
		})
		if err != nil {
			return err
		}

		return c.JSON(200, token{t})
	}

	return echo.NewHTTPError(401)
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:40,代码来源:account.go

示例8: getEvents

//
// GET /library/events
//
func getEvents(c *echo.Context) error {
	now := time.Now().Unix()
	var err error
	var ts uint64
	since := c.Request().URL.Query().Get("since")
	if since != "" {
		ts, err = strconv.ParseUint(since, 10, 64)
		if err != nil {
			return c.NoContent(400)
		}
	}

	user := c.Get("user").(*User)
	uuid := ""
	excludeSelf := c.Request().URL.Query().Get("exclude_self")
	if excludeSelf == "true" {
		uuid = c.Get("uuid").(string)
	}

	return c.JSON(200, events{
		Timestamp: now,
		Events:    store.GetEvents(user.ID, ts, uuid),
	})
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:27,代码来源:events.go

示例9: getCasts

//
// GET /library/casts
//
func getCasts(c *echo.Context) error {
	user := c.Get("user").(*User)
	return c.JSON(200, store.GetCastsByID(user.Subscriptions))
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:7,代码来源:casts.go

示例10: getSettings

//
// GET /account/settings
//
func getSettings(c *echo.Context) error {
	user := c.Get("user").(*User)
	uuid := c.Get("uuid").(string)
	return c.JSON(200, store.GetSettings(user.ID, uuid))
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:8,代码来源:account.go

示例11: getUser

func getUser(c *echo.Context) error {
	id, _ := strconv.Atoi(c.Param("id"))
	return c.JSON(http.StatusOK, users[id])
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:4,代码来源:server.go

示例12: getUsers

func getUsers(c *echo.Context) error {
	return c.JSON(http.StatusOK, users)
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:3,代码来源:server.go

示例13: getLabels

//
// GET /library/labels
//
func getLabels(c *echo.Context) error {
	user := c.Get("user").(*User)
	return c.JSON(200, store.GetLabels(user.ID))
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:7,代码来源:labels.go


注:本文中的github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo.Context.JSON方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。