From d6c4d06bd83f2feeb388cfb774a6011d447e0aab Mon Sep 17 00:00:00 2001 From: Eugene Date: Fri, 22 Dec 2017 14:16:01 -0600 Subject: [PATCH] dev mode integrated with rest --- app/rest/server.go | 20 ++++++++++++-------- app/store/store.go | 8 ++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/rest/server.go b/app/rest/server.go index c4325b45..78a37174 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -93,8 +93,19 @@ func (s *Server) createCommentCtrl(w http.ResponseWriter, r *http.Request) { return } + user, err := GetUserInfo(r) + if err != nil { + httpError(w, r, http.StatusUnauthorized, err, "can't get user info") + return + } + comment.User.IP = strings.Split(r.RemoteAddr, ":")[0] - comment.User.Name = template.HTMLEscapeString(comment.User.Name) + comment.User.ID = template.HTMLEscapeString(user.ID) + comment.User.Name = template.HTMLEscapeString(user.Name) + comment.User.Picture = template.HTMLEscapeString(user.Picture) + comment.User.Profile = template.HTMLEscapeString(user.Profile) + comment.User.Admin = user.Admin + comment.Text = template.HTMLEscapeString(comment.Text) log.Printf("[INFO] create comment %+v", comment) @@ -156,13 +167,6 @@ func (s *Server) getLastComments(w http.ResponseWriter, r *http.Request) { max = 0 } - uinfoData, err := GetUserInfo(r) - if err != nil { - http.Error(w, "login required", http.StatusUnauthorized) - return - } - log.Printf("[DEBUG] user: %+v", uinfoData) - comments, err := s.Store.Last(store.Locator{}, max) if err != nil { log.Printf("[WARN] can't get last comments, %s", err) diff --git a/app/store/store.go b/app/store/store.go index 778cccb4..6bc8e5ce 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -10,13 +10,13 @@ type Comment struct { User User `json:"user"` Locator Locator `json:"locator"` Score int `json:"score"` - Votes map[string]bool `json:"votes"` - Timestamp time.Time `json:"time"` + Votes map[string]bool `json:"votes,omitempty"` + Timestamp time.Time `json:"time,omitempty"` } // Locator keeps site and url of the post type Locator struct { - SiteID string `json:"site"` + SiteID string `json:"site,omitempty"` URL string `json:"url"` } @@ -26,7 +26,7 @@ type User struct { ID string `json:"id"` Picture string `json:"picture"` Profile string `json:"profile"` - Admin bool `json:"admin"` + Admin bool `json:"admin,omitempty"` IP string `json:"-"` }