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 jResp, err := json.Marshal(user)
142 check(err)
143 fmt.Fprintln(w, string(jResp))
144}
145func NewPost(w http.ResponseWriter, r *http.Request) {
146 decoder := json.NewDecoder(r.Body)
147 var post Post
148 err := decoder.Decode(&post)
149 if err != nil {
150 panic(err)
151 }
152 defer r.Body.Close()
153
154 //get user by token
155 usertoken := r.Header.Get("Authorization")
156 user := User{}
157 err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
158 check(err)
159
160 //save the post.Content into an html file
161 err = ioutil.WriteFile(postsDir+"/"+"file.html", []byte(post.Content), 0644)
162 check(err)
163 //add the html file to ipfs
164 out, err := exec.Command("bash", "-c", "ipfs add "+postsDir+"/file.html").Output()
165 if err != nil {
166 log.Fatal(err)
167 }
168 fmt.Println(out)
169 hash := strings.Split(string(out), " ")[1]
170 color.Blue(hash)
171
172 //save the hash to the post.content
173 post.Content = hash
174
175 //add date to post
176 post.Date = time.Now()
177
178 //add user.id to post.user
179 post.User.ID = user.ID
180 err = postCollection.Insert(post)
181 check(err)
182
183 //get user
184 //user = User{}
185 err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
186 check(err)
187
188 jResp, err := json.Marshal(user)
189 if err != nil {
190 panic(err)
191 }
192 fmt.Fprintln(w, string(jResp))
193}
194
195func GetPosts(w http.ResponseWriter, r *http.Request) {
196 //get projects from mongodb
197 posts := []Post{}
198 err := postCollection.Find(bson.M{}).Limit(50).All(&posts)
199 check(err)
200
201 jResp, err := json.Marshal(posts)
202 check(err)
203 fmt.Fprintln(w, string(jResp))
204}
205func GetPost(w http.ResponseWriter, r *http.Request) {
206 vars := mux.Vars(r)
207 postid := vars["postid"]
208
209 //get projects from mongodb
210 post := Post{}
211 err := postCollection.Find(bson.M{"_id": bson.ObjectIdHex(postid)}).One(&post)
212 check(err)
213
214 err = userCollection.Find(bson.M{"_id": post.User.ID}).One(&post.User)
215 check(err)
216
217 jResp, err := json.Marshal(post)
218 check(err)
219 fmt.Fprintln(w, string(jResp))
220}