Moved away from local files to complete database driven articles
This commit is contained in:
17
api/api.go
17
api/api.go
@@ -1,17 +0,0 @@
|
||||
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)
|
||||
}
|
||||
21
api/error.go
Normal file
21
api/error.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
DatabaseError = ApiError{Message: "internal database error", Code: http.StatusInternalServerError}
|
||||
InvalidJson = ApiError{Message: "invalid json", Code: http.StatusUnprocessableEntity}
|
||||
)
|
||||
|
||||
type ApiError struct {
|
||||
Message string `json:"message"`
|
||||
Code int
|
||||
}
|
||||
|
||||
func (ae ApiError) Send(w http.ResponseWriter) error {
|
||||
w.WriteHeader(ae.Code)
|
||||
return json.NewEncoder(w).Encode(ae)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -16,7 +15,7 @@ func Recent(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
ApiError{"invalid 'limit' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -27,13 +26,13 @@ func Recent(w http.ResponseWriter, r *http.Request) {
|
||||
Limit: int(limit),
|
||||
})
|
||||
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
var articleSummaries []database.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []schema.ArticleSummary
|
||||
Articles []database.ArticleSummary
|
||||
}{articleSummaries})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -20,21 +19,21 @@ func Search(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
ApiError{"could not parse 'from' parameter as RFC3339 time", 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)
|
||||
ApiError{"could not parse 'to' parameter as RFC3339 time", 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)
|
||||
ApiError{"invalid 'limit' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -49,13 +48,13 @@ func Search(w http.ResponseWriter, r *http.Request) {
|
||||
Limit: int(limit),
|
||||
})
|
||||
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
var articleSummaries []database.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []schema.ArticleSummary
|
||||
Articles []database.ArticleSummary `json:"articles"`
|
||||
}{articleSummaries})
|
||||
}
|
||||
|
||||
60
api/upload.go
Normal file
60
api/upload.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/parse"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type uploadRequest struct {
|
||||
Name string
|
||||
Author string
|
||||
Title string
|
||||
Summary string
|
||||
Image string
|
||||
Tags []string
|
||||
Content string
|
||||
}
|
||||
|
||||
func Upload(w http.ResponseWriter, r *http.Request) {
|
||||
var request uploadRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
InvalidJson.Send(w)
|
||||
return
|
||||
}
|
||||
|
||||
rawMarkdown, err := base64.StdEncoding.DecodeString(request.Content)
|
||||
if err != nil {
|
||||
zap.S().Warnf("Cannot decode base64")
|
||||
ApiError{Message: "invalid base64 content", Code: http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
|
||||
db := database.GetDB()
|
||||
|
||||
tags, err := db.AddOrGetTags(request.Tags)
|
||||
if err != nil {
|
||||
zap.S().Error("Failed to add or get tag to / from database: %v", err)
|
||||
DatabaseError.Send(w)
|
||||
return
|
||||
}
|
||||
if err = db.AddArticle(database.Article{
|
||||
Name: request.Name,
|
||||
Title: request.Title,
|
||||
Summary: request.Summary,
|
||||
Image: request.Image,
|
||||
Added: time.Now().Unix(),
|
||||
Markdown: request.Content,
|
||||
Html: string(parse.ParseToHtml(rawMarkdown)),
|
||||
}, tags); err != nil {
|
||||
zap.S().Errorf("Failed to add article to database: %v", err)
|
||||
DatabaseError.Send(w)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user