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.

220 lines
5.2 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. jResp, err := json.Marshal(user)
  122. check(err)
  123. fmt.Fprintln(w, string(jResp))
  124. }
  125. func NewPost(w http.ResponseWriter, r *http.Request) {
  126. decoder := json.NewDecoder(r.Body)
  127. var post Post
  128. err := decoder.Decode(&post)
  129. if err != nil {
  130. panic(err)
  131. }
  132. defer r.Body.Close()
  133. //get user by token
  134. usertoken := r.Header.Get("Authorization")
  135. user := User{}
  136. err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
  137. check(err)
  138. //save the post.Content into an html file
  139. err = ioutil.WriteFile(postsDir+"/"+"file.html", []byte(post.Content), 0644)
  140. check(err)
  141. //add the html file to ipfs
  142. out, err := exec.Command("bash", "-c", "ipfs add "+postsDir+"/file.html").Output()
  143. if err != nil {
  144. log.Fatal(err)
  145. }
  146. fmt.Println(out)
  147. hash := strings.Split(string(out), " ")[1]
  148. color.Blue(hash)
  149. //save the hash to the post.content
  150. post.Content = hash
  151. //add date to post
  152. post.Date = time.Now()
  153. //add user.id to post.user
  154. post.User.ID = user.ID
  155. err = postCollection.Insert(post)
  156. check(err)
  157. //get user
  158. //user = User{}
  159. err = userCollection.Find(bson.M{"token": usertoken}).One(&user)
  160. check(err)
  161. jResp, err := json.Marshal(user)
  162. if err != nil {
  163. panic(err)
  164. }
  165. fmt.Fprintln(w, string(jResp))
  166. }
  167. func GetPosts(w http.ResponseWriter, r *http.Request) {
  168. //get projects from mongodb
  169. posts := []Post{}
  170. err := postCollection.Find(bson.M{}).Limit(50).All(&posts)
  171. check(err)
  172. jResp, err := json.Marshal(posts)
  173. check(err)
  174. fmt.Fprintln(w, string(jResp))
  175. }
  176. func GetPost(w http.ResponseWriter, r *http.Request) {
  177. vars := mux.Vars(r)
  178. postid := vars["postid"]
  179. //get projects from mongodb
  180. post := Post{}
  181. err := postCollection.Find(bson.M{"_id": bson.ObjectIdHex(postid)}).One(&post)
  182. check(err)
  183. err = userCollection.Find(bson.M{"_id": post.User.ID}).One(&post.User)
  184. check(err)
  185. jResp, err := json.Marshal(post)
  186. check(err)
  187. fmt.Fprintln(w, string(jResp))
  188. }