From 67baf50b6e641c9910296c067ed63e246f7881d0 Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 8 Jun 2018 14:18:09 -0500 Subject: [PATCH] lint: minor warn --- app/main.go | 1 + app/main_test.go | 2 +- app/migrator/disqus.go | 4 ++-- app/rest/api/admin.go | 1 - app/rest/auth/provider.go | 29 +++++++++++++++++------------ app/rest/cache/cache.go | 13 +++++++++++++ app/rest/cache/options.go | 18 ------------------ app/rest/proxy/avatar.go | 5 ----- app/store/comment.go | 2 +- app/store/user.go | 2 +- 10 files changed, 36 insertions(+), 41 deletions(-) diff --git a/app/main.go b/app/main.go index 7cdae9c9..0cf81ea3 100644 --- a/app/main.go +++ b/app/main.go @@ -27,6 +27,7 @@ import ( ) // Opts with command line flags and env +// nolint:maligned type Opts struct { BoltPath string `long:"bolt" env:"BOLTDB_PATH" default:"./var" description:"parent dir for bolt files"` Sites []string `long:"site" env:"SITE" default:"remark" description:"site names" env-delim:","` diff --git a/app/main_test.go b/app/main_test.go index da4b1bbd..ec091327 100644 --- a/app/main_test.go +++ b/app/main_test.go @@ -19,7 +19,7 @@ import ( func TestApplication(t *testing.T) { app, ctx := prepApp(t, 18080, 500*time.Millisecond) - go app.Run(ctx) + go func() { _ = app.Run(ctx) }() time.Sleep(100 * time.Millisecond) // let server start // send ping diff --git a/app/migrator/disqus.go b/app/migrator/disqus.go index ded23bd4..98fc43bf 100644 --- a/app/migrator/disqus.go +++ b/app/migrator/disqus.go @@ -26,8 +26,8 @@ type disqusThread struct { CreateAt time.Time `xml:"createdAt"` AuthorName string `xml:"author>name"` AuthorEmail string `xml:"author>email"` - Anonymous bool `xml:"author>isAnonymous"` IP string `xml:"ipAddress"` + Anonymous bool `xml:"author>isAnonymous"` Closed bool `xml:"isClosed"` Deleted bool `xml:"isDeleted"` } @@ -37,13 +37,13 @@ type disqusComment struct { ID string `xml:"id"` Message string `xml:"message"` CreatedAt time.Time `xml:"createdAt"` - IsSpam bool `xml:"isSpam"` AuthorEmail string `xml:"author>email"` AuthorName string `xml:"author>name"` AuthorUserName string `xml:"author>username"` IP string `xml:"ipAddress"` Tid uid `xml:"thread"` Pid uid `xml:"parent"` + IsSpam bool `xml:"isSpam"` } type uid struct { diff --git a/app/rest/api/admin.go b/app/rest/api/admin.go index 619834f5..45a03f5e 100644 --- a/app/rest/api/admin.go +++ b/app/rest/api/admin.go @@ -24,7 +24,6 @@ type admin struct { dataService service.DataStore exporter migrator.Exporter cache cache.LoadingCache - defAvatarURL string authenticator auth.Authenticator } diff --git a/app/rest/auth/provider.go b/app/rest/auth/provider.go index e3748e96..fa55f23a 100644 --- a/app/rest/auth/provider.go +++ b/app/rest/auth/provider.go @@ -163,18 +163,7 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { log.Printf("[DEBUG] got raw user info %+v", jData) u := p.MapUser(jData, data) - if p.AvatarProxy != nil { - if avatarURL, e := p.AvatarProxy.Put(u); e == nil { - u.Picture = avatarURL - } else { - log.Printf("[WARN] failed to proxy avatar, %s", e) - } - } - - u.Admin = isAdmin(u.ID, p.Admins) - if p.IsVerifiedFn != nil { - u.Verified = p.IsVerifiedFn(oauthClaims.SiteID, u.ID) - } + u = p.alterUser(u, oauthClaims) authClaims := &CustomClaims{ User: &u, @@ -200,6 +189,22 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, &u) } +// alterUser sets fileds not handled by provider's MapUser, things like avatar, admin, verified +func (p Provider) alterUser(u store.User, oauthClaims *CustomClaims) store.User { + if p.AvatarProxy != nil { + if avatarURL, e := p.AvatarProxy.Put(u); e == nil { + u.Picture = avatarURL + } else { + log.Printf("[WARN] failed to proxy avatar, %s", e) + } + } + u.Admin = isAdmin(u.ID, p.Admins) + if p.IsVerifiedFn != nil { + u.Verified = p.IsVerifiedFn(oauthClaims.SiteID, u.ID) + } + return u +} + // LogoutHandler - GET /logout func (p Provider) LogoutHandler(w http.ResponseWriter, r *http.Request) { p.JwtService.Reset(w) diff --git a/app/rest/cache/cache.go b/app/rest/cache/cache.go index 7e02947e..8ac138c1 100644 --- a/app/rest/cache/cache.go +++ b/app/rest/cache/cache.go @@ -2,11 +2,13 @@ package cache import ( "log" + "net/http" "strings" "sync/atomic" "github.com/hashicorp/golang-lru" "github.com/pkg/errors" + "github.com/umputun/remark/app/rest" ) // LoadingCache defines interface for caching @@ -142,3 +144,14 @@ func (lc *loadingCache) allowed(data []byte) bool { } return true } + +// URLKey gets url from request to use it as cache key +// admins will have different keys in order to prevent leak of admin-only data to regular users +func URLKey(r *http.Request) string { + adminPrefix := "admin!!" + key := strings.TrimPrefix(r.URL.String(), adminPrefix) // prevents attach with fake url to get admin view + if user, err := rest.GetUserInfo(r); err == nil && user.Admin { // make separate cache key for admins + key = adminPrefix + key + } + return key +} diff --git a/app/rest/cache/options.go b/app/rest/cache/options.go index 64986e72..e077cf8f 100644 --- a/app/rest/cache/options.go +++ b/app/rest/cache/options.go @@ -1,12 +1,5 @@ package cache -import ( - "net/http" - "strings" - - "github.com/umputun/remark/app/rest" -) - // Option func type type Option func(lc *loadingCache) error @@ -44,14 +37,3 @@ func PostFlushFn(postFlushFn func()) Option { return nil } } - -// URLKey gets url from request to use it as cache key -// admins will have different keys in order to prevent leak of admin-only data to regular users -func URLKey(r *http.Request) string { - adminPrefix := "admin!!" - key := strings.TrimPrefix(r.URL.String(), adminPrefix) // prevents attach with fake url to get admin view - if user, err := rest.GetUserInfo(r); err == nil && user.Admin { // make separate cache key for admins - key = adminPrefix + key - } - return key -} diff --git a/app/rest/proxy/avatar.go b/app/rest/proxy/avatar.go index 0c085a33..c07205f1 100644 --- a/app/rest/proxy/avatar.go +++ b/app/rest/proxy/avatar.go @@ -1,13 +1,11 @@ package proxy import ( - "hash/crc64" "io" "log" "net/http" "strconv" "strings" - "sync" "time" "github.com/go-chi/chi" @@ -23,9 +21,6 @@ type Avatar struct { Store AvatarStore RoutePath string RemarkURL string - - once sync.Once - ctcTable *crc64.Table } const imgSfx = ".image" diff --git a/app/store/comment.go b/app/store/comment.go index 2e607327..0d2f5e51 100644 --- a/app/store/comment.go +++ b/app/store/comment.go @@ -19,8 +19,8 @@ type Comment struct { Score int `json:"score"` Votes map[string]bool `json:"votes"` Timestamp time.Time `json:"time"` - Pin bool `json:"pin,omitempty"` Edit *Edit `json:"edit,omitempty"` // pointer to have empty default in json response + Pin bool `json:"pin,omitempty"` Deleted bool `json:"delete,omitempty"` } diff --git a/app/store/user.go b/app/store/user.go index 4fab9a29..a1d032ed 100644 --- a/app/store/user.go +++ b/app/store/user.go @@ -16,9 +16,9 @@ type User struct { Name string `json:"name"` ID string `json:"id"` Picture string `json:"picture"` + IP string `json:"ip,omitempty"` Admin bool `json:"admin"` Blocked bool `json:"block,omitempty"` - IP string `json:"ip,omitempty"` Verified bool `json:"verified,omitempty"` }