You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.7 KiB

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