示例#1
0
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)
}
示例#2
0
func createUser(c *echo.Context) error {
	u := &user{
		ID: seq,
	}
	if err := c.Bind(u); err != nil {
		return err
	}
	users[u.ID] = u
	seq++
	return c.JSON(http.StatusCreated, u)
}
示例#3
0
func errorHandler(err error, c *echo.Context) {
	code := http.StatusInternalServerError
	msg := http.StatusText(code)
	if he, ok := err.(*echo.HTTPError); ok {
		code = he.Code()
		msg = he.Error()
	}
	c.Response().Header().Set("Content-Type", "text/plain; charset=utf-8")
	c.Response().WriteHeader(code)
	c.Response().Write([]byte(msg))
}
示例#4
0
//
// 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,
	})
}
示例#5
0
//
// DELETE /library/casts/:id
//
func removeCast(c *echo.Context) error {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	user := c.Get("user").(*User)
	user, err = store.RemoveSubscription(user.ID, id)
	authCache.set(c.Get("token").(string), user)
	return err
}
示例#6
0
//
// 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),
	})
}
示例#7
0
//
// DELETE /account/settings/:id
//
func removeSetting(c *echo.Context) error {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	user := c.Get("user").(*User)
	err = store.RemoveSetting(id, user.ID)
	if err == ErrSettingNotFound {
		return c.String(404, "Setting not found")
	}

	return err
}
示例#8
0
//
// 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))
}
示例#9
0
//
// 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)
}
示例#10
0
//
// DELETE /library/labels/:id
//
func removeLabel(c *echo.Context) error {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	user := c.Get("user").(*User)
	return store.RemoveLabel(id, user.ID)
}
示例#11
0
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])
}
示例#12
0
//
// POST /library/events
//
func addEvents(c *echo.Context) error {
	data := form(c, "json")
	if data == "" {
		return c.NoContent(400)
	}

	events := []Event{}
	err := json.Unmarshal([]byte(data), &events)
	if err != nil {
		return c.NoContent(400)
	}

	user := c.Get("user").(*User)
	uuid := c.Get("uuid").(string)
	return store.AddEvents(events, user.ID, uuid)
}
示例#13
0
//
// POST /account/settings
//
func setSettings(c *echo.Context) error {
	data := form(c, "json")
	if data == "" {
		return c.NoContent(400)
	}

	settings := []Setting{}
	err := json.Unmarshal([]byte(data), &settings)
	if err != nil {
		return c.NoContent(400)
	}

	user := c.Get("user").(*User)
	uuid := c.Get("uuid").(string)
	return store.SaveSettings(settings, user.ID, uuid)
}
示例#14
0
//
// 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)
}
示例#15
0
//
// PUT /library/labels/:id
//
func updateLabel(c *echo.Context) error {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	label := store.GetLabel(id)
	if label == nil {
		return c.String(404, "Label not found")
	}

	if name := form(c, "name"); name != "" {
		label.Name = name
	}
	if content := form(c, "content"); content != "" {
		label.Content = content
	}
	if expanded := form(c, "expanded"); expanded != "" {
		label.Expanded = expanded == "true"
	}

	user := c.Get("user").(*User)
	return store.SaveLabel(label, user.ID)
}
示例#16
0
//
// PUT /library/casts/:id
//
func renameCast(c *echo.Context) error {
	id, err := strconv.ParseUint(c.Param("id"), 10, 64)
	if err != nil {
		return c.NoContent(400)
	}

	cast := store.GetCast(id)
	if cast == nil {
		return c.String(404, "Cast not found")
	}

	prev := cast.Name
	cast.Name = form(c, "name")
	if cast.Name != prev {
		return store.SaveCast(cast)
	}

	return nil
}
示例#17
0
func getUser(c *echo.Context) error {
	id, _ := strconv.Atoi(c.Param("id"))
	return c.JSON(http.StatusOK, users[id])
}
示例#18
0
//
// GET /library/casts
//
func getCasts(c *echo.Context) error {
	user := c.Get("user").(*User)
	return c.JSON(200, store.GetCastsByID(user.Subscriptions))
}
示例#19
0
// Handler
func hello(c *echo.Context) error {
	return c.String(http.StatusOK, "Hello, World!\n")
}
示例#20
0
func form(c *echo.Context, key string) string {
	return c.Request().FormValue(key)
}
示例#21
0
func welcome(c *echo.Context) error {
	return c.Render(http.StatusOK, "welcome", "Joe")
}
示例#22
0
//
// 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))
}
示例#23
0
func getUsers(c *echo.Context) error {
	return c.JSON(http.StatusOK, users)
}
示例#24
0
func deleteUser(c *echo.Context) error {
	id, _ := strconv.Atoi(c.Param("id"))
	delete(users, id)
	return c.NoContent(http.StatusNoContent)
}
示例#25
0
//
// 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),
	})
}
示例#26
0
func getUser(c *echo.Context) error {
	return c.JSON(http.StatusOK, users[c.P(0)])
}
示例#27
0
//
// GET /library/labels
//
func getLabels(c *echo.Context) error {
	user := c.Get("user").(*User)
	return c.JSON(200, store.GetLabels(user.ID))
}