decentralized-blogging-platform/RESTfunctions.go
RESTfunctions.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "net/http"
9 "os/exec"
10 "strings"
11 "time"
12
13 "github.com/fatih/color"
14 "github.com/gorilla/mux"
15 "gopkg.in/mgo.v2/bson"
16)
17
18type Post struct {
19 ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
20 Title string `json:"title"`
21 Summary string `json:"summary"`
22 Date time.Time `json:"date"`
23 ThumbImg string `json:"thumbimg"`
24 Content string `json:"content"`
25 User User `json:"user"`
26}
27type User struct {
28 ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
29 Username string `json:"username"`
30 Name string `json:"name"`
31 LastName string `json:"lastname"`
32 Twitter string `json:"twitter" bson:",omitempty"`
33 Description string `json:"description" bson:",omitempty"`
34 Email string `json:"email" bson:",omitempty"`
35 Password string `json:"password" bson:",omitempty"`
36 Token string `json:"token" bson:",omitempty"`
37 Posts []Post `json:"posts" bson:",omitempty"`
38}
39
40func Index(w http.ResponseWriter, r *http.Request) {
41 fmt.Fprintln(w, "clientApp")
42}
43
44func Signup(w http.ResponseWriter, r *http.Request) {
45 decoder := json.NewDecoder(r.Body)
46 var user User
47 err := decoder.Decode(&user)
48 if err != nil {
49 panic(err)
50 }
51 defer r.Body.Close()
52
53 fmt.Print("user signup: ")
54 fmt.Println(user.Username)
55
56 //save the new project to mongodb
57 rUser := User{}
58 err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
59 if err != nil {
60 //user not exists
61 err = userCollection.Insert(user)
62 err = userCollection.Find(bson.M{"email": user.Email}).One(&user)
63 } else {
64 //user exists
65 http.Error(w, "user already registered", http.StatusConflict)
66 return
67 }
68
69 fmt.Println(user.Username)
70 jResp, err := json.Marshal(user)
71 if err != nil {
72 panic(err)
73 }
74 fmt.Fprintln(w, string(jResp))
75}
76
77func Login(w http.ResponseWriter, r *http.Request) {
78
79 decoder := json.NewDecoder(r.Body)
80 var user User
81 err := decoder.Decode(&user)
82 if err != nil {
83 panic(err)
84 }
85 defer r.Body.Close()
86 //TODO check if the user password exists in the database
87
88 fmt.Print("user login: ")
89 fmt.Println(user)
90
91 //save the new project to mongodb
92 rUser := User{}
93 err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser)
94 if err != nil {
95 http.Error(w, "error login, email not foun", http.StatusConflict)
96 return
97 }
98 //user exists, check password
99 if user.Password != rUser.Password {
100 http.Error(w, "error login, password not match", http.StatusConflict)
101 return
102 }
103
104 token, err := newToken()
105 check(err)
106 rUser.Token = token
107
108 //update with the token
109 err = userCollection.Update(bson.M{"_id": rUser.ID}, rUser)
110 check(err)
111
112 jResp, err := json.Marshal(rUser)
113 if err != nil {
114 panic(err)
115 }
116 fmt.Fprintln(w, string(jResp))
117}
118
119func GetUsers(w http.ResponseWriter, r *http.Request) {
120 //get projects from mongodb
121 users := []User{}
122 err := userCollection.Find(bson.M{}).Limit(50).All(&users)
123 check(err)
124
125 jResp, err := json.Marshal(users)
126 check(err)
127 fmt.Fprintln(w, string(jResp))
128}
129func GetUser(w http.ResponseWriter, r *http.Request) {
130 vars := mux.Vars(r)
131 userid := vars["userid"]
132
133 //get projects from mongodb
134 user := User{}
135 err := userCollection.Find(bson.M{"_id": bson.ObjectIdHex(userid)}).One(&user)
136 check(err)
137
138 err = postCollection.Find(bson.M{"user._id": bson.ObjectIdHex(userid)}).Limit(50).All(&user.Posts)
139 check(err)
140
141 //for each post, get the user
142 for i, _ := range user.Posts {
143 err = userCollection.Find(bson.M{"_id": user.Posts[i].User.ID}).One(&user.Posts[i].User)
144 check(err)
145 //TODO don't return the user.Token, password, etc only the Name, LastName, Username, img
146 }
147
148 jResp, err := json.Marshal(user)
149 check(err)
150 fmt.Fprintln(w, string(jResp))
151}
152func NewPost(w http.ResponseWriter, r *http.Request) {
153 decoder := json.NewDecoder(r.Body)
154 var post Post
155 err := decoder.Decode(&post)
156 if err != nil {
157 panic(err)
158 }
159 defer r.Body.Close()
160
161 //get user by token
162 usertoken := r.Header.Get("Authorization")
163 user := User{}
164 err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
165 check(err)
166
167 //save the post.Content into an html file
168 err = ioutil.WriteFile(postsDir+"/"+"file.html", []byte(post.Content), 0644)
169 check(err)
170 //add the html file to ipfs
171 out, err := exec.Command("bash", "-c", "ipfs add "+postsDir+"/file.html").Output()
172 if err != nil {
173 log.Fatal(err)
174 }
175 fmt.Println(out)
176 hash := strings.Split(string(out), " ")[1]
177 color.Blue(hash)
178
179 //save the hash to the post.content
180 post.Content = hash
181
182 //add date to post
183 post.Date = time.Now()
184
185 //add user.id to post.user
186 post.User.ID = user.ID
187 err = postCollection.Insert(post)
188 check(err)
189
190 //get user
191 //user = User{}
192 err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
193 check(err)
194
195 jResp, err := json.Marshal(user)
196 if err != nil {
197 panic(err)
198 }
199 fmt.Fprintln(w, string(jResp))
200}
201
202func GetPosts(w http.ResponseWriter, r *http.Request) {
203 //get projects from mongodb
204 posts := []Post{}
205 err := postCollection.Find(bson.M{}).Limit(50).All(&posts)
206 check(err)
207
208 //for each post, get the user
209 for i, _ := range posts {
210 fmt.Println(posts[i].User.ID)
211 err = userCollection.Find(bson.M{"_id": posts[i].User.ID}).One(&posts[i].User)
212 check(err)
213 //TODO don't return the user.Token, password, etc only the Name, LastName, Username, img
214 }
215 jResp, err := json.Marshal(posts)
216 check(err)
217 fmt.Fprintln(w, string(jResp))
218}
219func GetPost(w http.ResponseWriter, r *http.Request) {
220 vars := mux.Vars(r)
221 postid := vars["postid"]
222
223 //get projects from mongodb
224 post := Post{}
225 err := postCollection.Find(bson.M{"_id": bson.ObjectIdHex(postid)}).One(&post)
226 check(err)
227
228 err = userCollection.Find(bson.M{"_id": post.User.ID}).One(&post.User)
229 check(err)
230
231 jResp, err := json.Marshal(post)
232 check(err)
233 fmt.Fprintln(w, string(jResp))
234}