/
api.go
133 lines (118 loc) · 3.14 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package app
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/oknors/okno/app/jdb"
"github.com/oknors/okno/app/mod"
"github.com/oknors/okno/pkg/utl"
"net/http"
"sort"
"strconv"
"time"
)
// Post contains articles and pages used by the CMS
type Post struct {
ID string `schema:"id,required"`
Title string
Slug string
Date time.Time
Excerpt string
Active bool
Order int
}
type Posts []Post
func (p Posts) Len() int { return len(p) }
func (p Posts) Less(i, j int) bool { return p[i].Order < p[j].Order }
func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (o *OKNO) api(r *mux.Router) {
////////////////
// api
////////////////
a := r.Host("api.okno.rs").Subrouter()
a.StrictSlash(true)
a.HandleFunc("/", homeHandler)
a.HandleFunc("/{site}/{col}/{id}", o.viewPost).Methods("GET")
a.HandleFunc("/{site}/{col}", o.viewAllPosts).Methods("GET")
a.HandleFunc("/{site}/{col}/{per}/{page}/{truncate}", o.viewPosts).Methods("GET")
a.Headers("Access-Control-Allow-Origin", "*")
}
// HomeHandler handles a request for (?)
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("The Truth Is Out There..."))
}
func (o *OKNO) viewPosts(w http.ResponseWriter, r *http.Request) {
per, _ := strconv.Atoi(mux.Vars(r)["per"])
page, _ := strconv.Atoi(mux.Vars(r)["page"])
trn, _ := strconv.Atoi(mux.Vars(r)["truncate"])
site := mux.Vars(r)["site"]
col := mux.Vars(r)["col"]
posts := Posts{}
postsRaw, err := jdb.JDB.ReadAll("sites/" + site + "/jdb/" + col)
utl.ErrorLog(err)
for _, postInterface := range postsRaw {
var rawPost mod.Post
err := json.Unmarshal([]byte(postInterface), &rawPost)
utl.ErrorLog(err)
if !rawPost.IsDraft {
p := Post{
ID: rawPost.ID,
Title: rawPost.Title,
Slug: rawPost.Slug,
Date: rawPost.CreatedAt,
Order: rawPost.Order,
Excerpt: utl.Truncate(rawPost.Content, trn),
}
posts = append(posts, p)
}
}
sort.Sort(posts)
pn := len(posts)
lb := map[string]interface{}{
"currentPage": page,
"pageCount": int(pn) / per,
"posts": posts,
"postsNumber": pn,
}
out, err := json.Marshal(lb)
if err != nil {
fmt.Println("Error encoding JSON")
return
}
w.Write([]byte(out))
}
func (o *OKNO) viewAllPosts(w http.ResponseWriter, r *http.Request) {
site := mux.Vars(r)["site"]
col := mux.Vars(r)["col"]
posts := mod.Posts{}
postsRaw, err := jdb.JDB.ReadAll("sites/" + site + "/jdb/" + col)
utl.ErrorLog(err)
for _, postInterface := range postsRaw {
var rawPost mod.Post
err := json.Unmarshal([]byte(postInterface), &rawPost)
utl.ErrorLog(err)
if !rawPost.IsDraft {
posts = append(posts, rawPost)
}
}
sort.Sort(posts)
out, err := json.Marshal(posts)
if err != nil {
fmt.Println("Error encoding JSON")
return
}
w.Write([]byte(out))
}
func (o *OKNO) viewPost(w http.ResponseWriter, r *http.Request) {
site := mux.Vars(r)["site"]
col := mux.Vars(r)["col"]
id := mux.Vars(r)["id"]
post := mod.Post{}
err := jdb.JDB.Read("sites/"+site+"/jdb/"+col, id, &post)
out, err := json.Marshal(post)
if err != nil {
fmt.Println("Error encoding JSON")
return
}
w.Write([]byte(out))
}