Initial release
This commit is contained in:
17
api/api.go
Normal file
17
api/api.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ApiError struct {
|
||||
Message string `json:"message"`
|
||||
OriginalError error
|
||||
Code int
|
||||
}
|
||||
|
||||
func (ae ApiError) Send(w http.ResponseWriter) error {
|
||||
w.WriteHeader(ae.Code)
|
||||
return json.NewEncoder(w).Encode(ae)
|
||||
}
|
||||
39
api/recent.go
Normal file
39
api/recent.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Recent(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var limit int64
|
||||
|
||||
query := r.URL.Query()
|
||||
if l := query.Get("limit"); l != "" {
|
||||
limit, err = strconv.ParseInt(l, 10, 64)
|
||||
if err != nil {
|
||||
ApiError{"invalid 'limit' parameter", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
limit = 20
|
||||
}
|
||||
|
||||
articles, err := database.GetDB().GetArticles("", database.ArticleQueryOptions{
|
||||
Limit: int(limit),
|
||||
})
|
||||
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []schema.ArticleSummary
|
||||
}{articleSummaries})
|
||||
}
|
||||
61
api/search.go
Normal file
61
api/search.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Search(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var q string
|
||||
var from, to time.Time
|
||||
var limit int64
|
||||
|
||||
query := r.URL.Query()
|
||||
q = query.Get("q")
|
||||
if f := query.Get("from"); f != "" {
|
||||
from, err = time.Parse(time.RFC3339, f)
|
||||
if err != nil {
|
||||
ApiError{"could not parse 'from' parameter as RFC3339 time", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
if t := query.Get("to"); t != "" {
|
||||
to, err = time.Parse(time.RFC3339, t)
|
||||
if err != nil {
|
||||
ApiError{"could not parse 'to' parameter as RFC3339 time", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
if l := query.Get("limit"); l != "" {
|
||||
limit, err = strconv.ParseInt(l, 10, 64)
|
||||
if err != nil {
|
||||
ApiError{"invalid 'limit' parameter", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
limit = 20
|
||||
}
|
||||
|
||||
articles, err := database.GetDB().GetArticles(q, database.ArticleQueryOptions{
|
||||
Title: true,
|
||||
Summary: true,
|
||||
From: from.Unix(),
|
||||
To: to.Unix(),
|
||||
Limit: int(limit),
|
||||
})
|
||||
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []schema.ArticleSummary
|
||||
}{articleSummaries})
|
||||
}
|
||||
Reference in New Issue
Block a user