Пример #1
0
/*
Get random items.

method:     GET
path:       /api/article/list/random

example:
    /api/article/list/random
    /api/article/list/random?n=20

You can control the number of results returned by add a "n" parameter, default number is 10.
*/
func RandomArticleList() martini.Handler {
	return func(w http.ResponseWriter, r *http.Request, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		r.ParseForm()
		num, err := strconv.Atoi(httphelper.QueryValue(r, "n"))
		if err != nil || num <= 0 {
			num = 10
		}

		list, err := model.GetRandomArticleList(num)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		for i, _ := range list.Articles {
			utils.SanitizeSelf(&list.Articles[i].Name)
			utils.SanitizeSelf(&list.Articles[i].Author)
			utils.SanitizeSelf(&list.Articles[i].Title)
		}

		result.Success = true
		result.Result = list
		result.Response(w)
	}
}
Пример #2
0
/*
Get one article.

method:     GET
path:       /api/article/{article id}
example:    /api/article/1
*/
func Article() martini.Handler {
	return func(w http.ResponseWriter, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		id, err := strconv.ParseInt(params["id"], 10, 64)
		if err != nil || id <= 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'id' is not correct.")
			}
			result.Response(w)
			return
		}

		article, ok, err := model.GetArticle(id)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if !ok {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf("Article of id:%d is not found.", id)
			result.Response(w)
			return
		}

		if article.Read == false {
			// read article read
			_, err = model.MarkArticleRead(id, true)
			if err != nil {
				result.Error = ErrQueryDB
				result.IntError = err
				result.Response(w)
				return
			}
		}
		article.Read = true

		utils.SanitizeSelf(&article.Name)
		utils.SanitizeSelf(&article.Author)
		utils.SanitizeSelf(&article.Title)
		utils.SanitizeSelf(&article.Content, true)

		result.Success = true
		result.Result = article
		result.Response(w)
	}
}
Пример #3
0
/* Get feedinfo.

method:     GET
path:       /api/feed/id/{id}
example:    /api/feed/id/1
*/
func FeedInfo() martini.Handler {
	return func(w http.ResponseWriter, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		id, err := strconv.ParseInt(params["id"], 10, 64)
		if err != nil || id <= 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'id' is not correct.")
			}
			result.Response(w)
			return
		}

		info, err := model.GetFeedInfo(id)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if info == nil {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf(ErrNoResultsFound.ErrMsg)
			result.Response(w)
			return
		}

		utils.SanitizeSelf(&info.Name)
		utils.SanitizeSelf(info.Alias)
		utils.SanitizeSelf(&info.Desc)
		utils.SanitizeSelf(info.Note)

		result.Success = true
		result.Result = info
		result.Response(w)
	}
}
Пример #4
0
/*
Get all feeds' info.

method:     GET
path:       /api/feed/feedlist

The output is like:
{"request_id":"a02d1d1b06cb38bcb4cc248da7d21ba3","success":true,"error":{"errcode":0,"errmsg":""},"result":[{"feed_id":1,"feed_name":"cnBeta.COM业界资讯","feed_feed_url":"http://cnbeta.feedsportal.com/c/34306/f/624776/index.rss","feed_url":"http://www.cnbeta.com","feed_desc":"cnBeta.COM - 简明IT新闻,网友媒体与言论平台","feed_type":"rss","feed_interval":0,"feed_last_fetch":"2015-03-15T19:07:16+08:00","feed_last_failed":"0001-01-01T08:00:00+08:00","feed_last_error":"","feed_max_number":0,"feed_filter":"","feed_use_proxy":false,"feed_note":"","unread":60},{"feed_id":2,"feed_name":"My*Candy","feed_feed_url":"http://mengqi.info/feed.xml","feed_url":"http://mengqi.info","feed_desc":"","feed_type":"atom","feed_interval":0,"feed_last_fetch":"2015-03-15T19:48:13+08:00","feed_last_failed":"0001-01-01T08:00:00+08:00","feed_last_error":"","feed_max_number":0,"feed_filter":"","feed_use_proxy":false,"feed_note":"","unread":0},{"feed_id":3,"feed_name":"Startup News","feed_feed_url":"http://news.dbanotes.net/rss","feed_url":"http://news.dbanotes.net/","feed_desc":"Startup News of China","feed_type":"rss","feed_interval":0,"feed_last_fetch":"2015-03-19T19:36:40+08:00","feed_last_failed":"0001-01-01T08:00:00+08:00","feed_last_error":"","feed_max_number":0,"feed_filter":"","feed_use_proxy":false,"feed_note":"","unread":395},{"feed_id":4,"feed_name":"于江水","feed_feed_url":"http://yujiangshui.com/atom.xml","feed_url":"http://yujiangshui.com/","feed_desc":"一入前端深似海。","feed_type":"atom","feed_interval":0,"feed_last_fetch":"2015-03-19T19:39:09+08:00","feed_last_failed":"0001-01-01T08:00:00+08:00","feed_last_error":"","feed_max_number":0,"feed_filter":"","feed_use_proxy":false,"feed_note":"","unread":20}]}

*/
func FeedList() martini.Handler {
	return func(w http.ResponseWriter, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		feedList, err := model.GetFeedListWithAmount(nil)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		for i, _ := range feedList {
			utils.SanitizeSelf(&feedList[i].Name)
			utils.SanitizeSelf(&feedList[i].Desc)
		}

		result.Success = true
		result.Result = feedList
		result.Response(w)
	}
}
Пример #5
0
/*
Get unread article list.

method:     GET

route:      unread
path:       /api/articles/unread/{limit}/{offset}
example:    /api/articles/unread/10/100

route:      fid
path:       /api/articles/fid/{fid}/{limit}/{offset}
example:    /api/articles/fid/12/10/100

route:      tag
path:       /api/articles/tag/{tag}/{limit}/{offset}
example:    /api/articles/tag/blog/10/100

route:      starred
path:       /api/articles/starred/{limit}/{offset}
example:    /api/articles/starred/10/100
*/
func ArticleList(route string) martini.Handler {
	return func(w http.ResponseWriter, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		limit, err := strconv.Atoi(params["limit"])
		if err != nil || limit <= 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'limit' is not correct.")
			}
			result.Response(w)
			return
		}

		offset, err := strconv.Atoi(params["offset"])
		if err != nil || offset < 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'offset' is not correct.")
			}
			result.Response(w)
			return
		}

		var list model.ArticleList

		if route == "unread" {
			list, err = model.GetArticleList(limit, offset)

		} else if route == "fid" {
			var fid int64
			fid, err = strconv.ParseInt(params["fid"], 10, 64)
			if err != nil || fid < 0 {
				result.Error = ErrBadRequest
				if err != nil {
					result.IntError = err
				} else {
					result.IntError = fmt.Errorf("Parameter 'fid' is not correct.")
				}
				result.Response(w)
				return
			}

			list, err = model.GetArticleListByFid(fid, limit, offset)

		} else if route == "tag" {
			list, err = model.GetArticleListByTag(params["tag"], limit, offset)

		} else if route == "starred" {
			list, err = model.GetStarredArticleList(limit, offset)

		} else {
			result.Error = ErrUnexpectedError
			result.IntError = fmt.Errorf(ErrUnexpectedError.ErrMsg)
			result.Response(w)
			return
		}

		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if len(list.Articles) == 0 {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf(ErrNoResultsFound.Error())
			result.Response(w)
			return
		}

		for i, _ := range list.Articles {
			utils.SanitizeSelf(&list.Articles[i].Name)
			utils.SanitizeSelf(&list.Articles[i].Author)
			utils.SanitizeSelf(&list.Articles[i].Title)
		}

		result.Success = true
		result.Result = list
		result.Response(w)
	}
}
Пример #6
0
/*
Article list of search result.

path:       /api/articles/search/{deflimit}?q={query}&page={page}
example:    /api/articles/search/10?q=num:20&page=10
*/
func SearchList() martini.Handler {

	return func(w http.ResponseWriter, r *http.Request, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		r.ParseForm()

		var limit, page int

		l, _ := strconv.ParseInt(params["deflimit"], 10, 0)
		p, _ := strconv.ParseInt(httphelper.QueryValue(r, "page", "0"), 10, 0)
		limit = int(l)
		page = int(p)

		if limit <= 0 {
			result.Error = ErrBadRequest
			result.IntError = fmt.Errorf("Parameter 'limit' not correct.")
			result.Response(w)
			return
		}

		if page <= 0 {
			page = 1
		}

		sq, err := model.Search(httphelper.QueryValue(r, "q"))
		if err != nil {
			result.Error = ErrSearchSyntaxError
			result.IntError = err
			result.Response(w)
			return
		}

		if sq.Num == nil {
			sq.Num = &limit
		}

		list, err := sq.List(page)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		if len(list.Articles) == 0 {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf(ErrNoResultsFound.Error())
			result.Response(w)
			return
		}

		for i, _ := range list.Articles {
			utils.SanitizeSelf(&list.Articles[i].Name)
			utils.SanitizeSelf(&list.Articles[i].Author)
			utils.SanitizeSelf(&list.Articles[i].Title)
		}

		var t struct {
			model.ArticleList
			Limit int `json:"limit"` // used for paging
		}
		t.ArticleList = list
		t.Limit = *sq.Num

		result.Success = true
		result.Result = t
		result.Response(w)
	}
}
Пример #7
0
/*
Get one article.

method:     GET
path:       /api/article/{article id}
example:    /api/article/1
*/
func Article() martini.Handler {
	return func(w http.ResponseWriter, params martini.Params, rid httphelper.RequestId) {
		var result Result
		result.RequestId = rid

		id, err := strconv.ParseInt(params["id"], 10, 64)
		if err != nil || id <= 0 {
			result.Error = ErrBadRequest
			if err != nil {
				result.IntError = err
			} else {
				result.IntError = fmt.Errorf("Parameter 'id' is not correct.")
			}
			result.Response(w)
			return
		}

		article, ok, err := model.GetArticle(id)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}
		if !ok {
			result.Error = ErrNoResultsFound
			result.IntError = fmt.Errorf("Article of id:%d is not found.", id)
			result.Response(w)
			return
		}

		if article.Read == false {
			// read article read
			_, err = model.MarkArticleRead(id, true)
			if err != nil {
				result.Error = ErrQueryDB
				result.IntError = err
				result.Response(w)
				return
			}
		}
		article.Read = true

		// Get five ralated articles
		related, err := model.GetRelatedArticles(id, 5)
		if err != nil {
			result.Error = ErrQueryDB
			result.IntError = err
			result.Response(w)
			return
		}

		utils.SanitizeSelf(&article.Name)
		utils.SanitizeSelf(&article.Author)
		utils.SanitizeSelf(&article.Title)
		utils.SanitizeSelf(&article.Content, true)

		for i := range related {
			utils.SanitizeSelf(&related[i].Name)
			utils.SanitizeSelf(&related[i].Author)
			utils.SanitizeSelf(&related[i].Title)
		}

		var t struct {
			Article *model.Article   `json:"article"`
			Related []*model.Article `json:"related"`
		}
		t.Article = article
		t.Related = related

		result.Success = true
		result.Result = t
		result.Response(w)
	}
}