From c034a84c6bc35f10840067f2d6fc656b08b4e3b3 Mon Sep 17 00:00:00 2001 From: Eugene Date: Fri, 22 Dec 2017 14:42:15 -0600 Subject: [PATCH] working local dev config --- .gitignore | 1 + Dockerfile | 6 +++--- app/rest/auth/auth.go | 4 ++-- app/rest/server.go | 3 +-- app/store/bolt.go | 15 ++++++++------- app/store/bolt_test.go | 3 +++ app/store/store.go | 6 +++--- docker-compose.yml | 12 ++++++++++++ 8 files changed, 33 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index d40bc9ce..675772cf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target /logs/ /target/ +/var/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index ef3ccac4..40302e7c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,18 +9,18 @@ RUN gometalinter --disable-all --deadline=300s --vendor --enable=vet --enable=ve --enable=staticcheck --enable=ineffassign --enable=goconst --enable=errcheck --enable=unconvert \ --enable=deadcode --enable=gosimple --enable=gas --exclude=test ./... -RUN /script/checkvendor.sh +#RUN /script/checkvendor.sh RUN mkdir -p target && /script/coverage.sh RUN go build -o remark -ldflags "-X main.revision=$(git rev-parse --abbrev-ref HEAD)-$(git describe --abbrev=7 --always --tags)-$(date +%Y%m%d-%H:%M:%S)" ./app -# Run + FROM umputun/baseimage:micro-latest RUN apk add --update ca-certificates && update-ca-certificates COPY --from=build /go/src/github.com/umputun/remark/remark /srv/ - +COPY --from=build /go/src/github.com/umputun/remark/web /srv/web RUN chown -R umputun:umputun /srv USER umputun diff --git a/app/rest/auth/auth.go b/app/rest/auth/auth.go index 38b470be..3e02d25c 100644 --- a/app/rest/auth/auth.go +++ b/app/rest/auth/auth.go @@ -140,8 +140,8 @@ func (p Provider) AuthHandler(w http.ResponseWriter, r *http.Request) { log.Printf("[DEBUG] %+v", jData) // redirect to back url if presented - if fromUrl, ok := session.Values["from"]; ok { - http.Redirect(w, r, fromUrl.(string), http.StatusTemporaryRedirect) + if fromURL, ok := session.Values["from"]; ok { + http.Redirect(w, r, fromURL.(string), http.StatusTemporaryRedirect) return } diff --git a/app/rest/server.go b/app/rest/server.go index 78a37174..43e79944 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -4,12 +4,11 @@ import ( "html/template" "log" "net/http" + "path/filepath" "strconv" "strings" "time" - "path/filepath" - "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/go-chi/render" diff --git a/app/store/bolt.go b/app/store/bolt.go index 4ec44283..e2be2ad8 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -39,7 +39,7 @@ func (b *BoltDB) Create(comment Comment) (int64, error) { comment.ID = time.Now().UnixNano() comment.Timestamp = time.Now() - comment.Votes = map[string]bool{} + comment.Votes = make(map[string]bool) err := b.Update(func(tx *bolt.Tx) error { bucket, e := tx.CreateBucketIfNotExists([]byte(comment.Locator.URL)) @@ -197,7 +197,7 @@ func (b *BoltDB) Last(locator Locator, max int) (result []Comment, err error) { return result, err } -// Get comment by id +// Vote for comment by id and locator func (b *BoltDB) Vote(locator Locator, commentID int64, userID string, val bool) (comment Comment, err error) { err = b.Update(func(tx *bolt.Tx) error { @@ -230,12 +230,12 @@ func (b *BoltDB) Vote(locator Locator, commentID int64, userID string, val bool) } else { comment.Score-- } - data, err := json.Marshal(&comment) - if err != nil { - return errors.Wrap(err, "can't marshal comment with updated votes") + data, e := json.Marshal(&comment) + if e != nil { + return errors.Wrap(e, "can't marshal comment with updated votes") } - if err = bucket.Put(b.keyFromValue(commentID), data); err != nil { - return errors.Wrap(err, "failed to save comment with updated votes") + if e = bucket.Put(b.keyFromValue(commentID), data); e != nil { + return errors.Wrap(e, "failed to save comment with updated votes") } return nil }) @@ -243,6 +243,7 @@ func (b *BoltDB) Vote(locator Locator, commentID int64, userID string, val bool) return comment, err } +// Count returns number of comments for locator func (b *BoltDB) Count(locator Locator) (count int, err error) { err = b.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(locator.URL)) diff --git a/app/store/bolt_test.go b/app/store/bolt_test.go index 7c55641b..a0a0666e 100644 --- a/app/store/bolt_test.go +++ b/app/store/bolt_test.go @@ -76,6 +76,7 @@ func TestBoltDB_Vote(t *testing.T) { b := prep(t) res, err := b.Last(Locator{URL: "https://radio-t.com"}, 0) + t.Logf("%+v", res[0]) assert.Nil(t, err) assert.Equal(t, 2, len(res)) assert.Equal(t, 0, res[0].Score) @@ -106,6 +107,8 @@ func TestBoltDB_Count(t *testing.T) { // makes new boltdb, put two records func prep(t *testing.T) *BoltDB { + os.Remove(testDb) + b, err := NewBoltDB(testDb) assert.Nil(t, err) diff --git a/app/store/store.go b/app/store/store.go index 6bc8e5ce..afd451da 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -10,8 +10,8 @@ type Comment struct { User User `json:"user"` Locator Locator `json:"locator"` Score int `json:"score"` - Votes map[string]bool `json:"votes,omitempty"` - Timestamp time.Time `json:"time,omitempty"` + Votes map[string]bool `json:"votes"` + Timestamp time.Time `json:"time"` } // Locator keeps site and url of the post @@ -26,7 +26,7 @@ type User struct { ID string `json:"id"` Picture string `json:"picture"` Profile string `json:"profile"` - Admin bool `json:"admin,omitempty"` + Admin bool `json:"admin"` IP string `json:"-"` } diff --git a/docker-compose.yml b/docker-compose.yml index 9ff14f76..4d50d283 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,3 +18,15 @@ services: environment: - MHOST=${MHOST} + ports: + - "8080:8080" + + environment: + - BOLTDB_FILE=/srv/var/remark.db + - SESSION_STORE=/srv/var + - DEV=true + - DEBUG=true + + volumes: + - ./var:/srv/var +