From d7d90eeb4e939f90f33ee0b7d1713126fa7e914b Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 20:56:52 -0500 Subject: [PATCH] lint: bunch of minor warnings --- app/main.go | 10 ++++++---- app/rest/api/import.go | 5 ++++- app/rest/api/import_test.go | 1 + app/rest/api/rest.go | 5 ++++- app/rest/auth/auth_test.go | 8 ++++++++ app/rest/auth/provider_test.go | 1 + app/rest/cache_test.go | 1 + app/rest/proxy/image_test.go | 4 ++-- 8 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/main.go b/app/main.go index 152cbe8b..0e18510a 100644 --- a/app/main.go +++ b/app/main.go @@ -25,6 +25,7 @@ import ( "github.com/umputun/remark/app/rest/proxy" ) +// Opts with command line flags and env 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:","` @@ -62,7 +63,8 @@ type Opts struct { var opts Opts var revision = "unknown" -type application struct { +// Application holds all active objects +type Application struct { Opts srv api.Rest importer api.Import @@ -96,7 +98,7 @@ func main() { } // Run all application objects -func Run(ctx context.Context, a *application) error { +func Run(ctx context.Context, a *Application) error { if a.DevPasswd != "" { log.Printf("[WARN] running in dev mode") } @@ -115,7 +117,7 @@ func Run(ctx context.Context, a *application) error { // Setup prepares application and return all active parts // doesn't start anything -func Setup(opts Opts) (*application, error) { +func Setup(opts Opts) (*Application, error) { setupLog(opts.Dbg) if err := makeDirs(opts.BoltPath, opts.BackupLocation, opts.AvatarStore); err != nil { @@ -166,7 +168,7 @@ func Setup(opts Opts) (*application, error) { Cache: cache, } srv.ScoreThresholds.Low, srv.ScoreThresholds.Critical = opts.LowScore, opts.CriticalScore - return &application{srv: srv, importer: importer, exporter: exporter, Opts: opts}, nil + return &Application{srv: srv, importer: importer, exporter: exporter, Opts: opts}, nil } // activateBackup runs background backups for each site diff --git a/app/rest/api/import.go b/app/rest/api/import.go index a69d18f9..c7318d3e 100644 --- a/app/rest/api/import.go +++ b/app/rest/api/import.go @@ -39,11 +39,14 @@ func (s *Import) Run(port int) { log.Printf("[WARN] http server terminated, %s", err) } +// Shutdown import http server func (s *Import) Shutdown() { log.Print("[WARN] shutdown import server") ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - s.httpServer.Shutdown(ctx) + if err := s.httpServer.Shutdown(ctx); err != nil { + log.Printf("[DEBUG] importer shutdown error, %s", err) + } log.Print("[DEBUG] shutdown import server completed") } diff --git a/app/rest/api/import_test.go b/app/rest/api/import_test.go index 6d6e06e4..8fd59994 100644 --- a/app/rest/api/import_test.go +++ b/app/rest/api/import_test.go @@ -30,6 +30,7 @@ func TestImport(t *testing.T) { req, err := http.NewRequest("POST", ts.URL+"/api/v1/admin/import?site=radio-t&provider=native&secret=123456", r) assert.Nil(t, err) resp, err := client.Do(req) + assert.Nil(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index a36f9e8c..23356638 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -75,11 +75,14 @@ func (s *Rest) Run(port int) { log.Printf("[WARN] http server terminated, %s", err) } +// Shutdown rest http server func (s *Rest) Shutdown() { log.Print("[WARN] shutdown rest server") ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - s.httpServer.Shutdown(ctx) + if err := s.httpServer.Shutdown(ctx); err != nil { + log.Printf("[DEBUG] rest shutdown error, %s", err) + } log.Print("[DEBUG] shutdown rest server completed") } diff --git a/app/rest/auth/auth_test.go b/app/rest/auth/auth_test.go index b6df5ac2..ddfc2f92 100644 --- a/app/rest/auth/auth_test.go +++ b/app/rest/auth/auth_test.go @@ -87,17 +87,20 @@ func TestAuthRequired(t *testing.T) { client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) req = withBasicAuth(req, "dev", "123456") resp, err := client.Do(req) require.NoError(t, err) assert.Equal(t, 201, resp.StatusCode, "valid auth user") req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) resp, err = client.Do(req) require.NoError(t, err) assert.Equal(t, 401, resp.StatusCode, "no auth user") req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) req = withBasicAuth(req, "dev", "xyz") resp, err = client.Do(req) require.NoError(t, err) @@ -115,17 +118,20 @@ func TestAuthNotRequired(t *testing.T) { client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) req = withBasicAuth(req, "dev", "123456") resp, err := client.Do(req) require.NoError(t, err) assert.Equal(t, 201, resp.StatusCode, "valid auth user") req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) resp, err = client.Do(req) require.NoError(t, err) assert.Equal(t, 201, resp.StatusCode, "no auth user") req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) req = withBasicAuth(req, "dev", "ZZZZ123456") resp, err = client.Do(req) require.NoError(t, err) @@ -143,6 +149,7 @@ func TestAdminRequired(t *testing.T) { client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) req = withBasicAuth(req, "dev", "123456") resp, err := client.Do(req) require.NoError(t, err) @@ -150,6 +157,7 @@ func TestAdminRequired(t *testing.T) { devUser.Admin = false req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.NoError(t, err) req = withBasicAuth(req, "dev", "123456") resp, err = client.Do(req) require.NoError(t, err) diff --git a/app/rest/auth/provider_test.go b/app/rest/auth/provider_test.go index e6f6dcd6..f238043b 100644 --- a/app/rest/auth/provider_test.go +++ b/app/rest/auth/provider_test.go @@ -27,6 +27,7 @@ func TestLogin(t *testing.T) { }() jar, err := cookiejar.New(nil) + require.Nil(t, err) client := &http.Client{Jar: jar, Timeout: 5 * time.Second} resp, err := client.Get("http://localhost:8981/login") assert.Nil(t, err) diff --git a/app/rest/cache_test.go b/app/rest/cache_test.go index 645feb47..cd057dc3 100644 --- a/app/rest/cache_test.go +++ b/app/rest/cache_test.go @@ -157,6 +157,7 @@ func TestLoadingCache_Parallel(t *testing.T) { wg := sync.WaitGroup{} for i := 0; i < 1000; i++ { wg.Add(1) + i := i go func() { defer wg.Done() res, err := lc.Get("key", time.Minute, func() ([]byte, error) { diff --git a/app/rest/proxy/image_test.go b/app/rest/proxy/image_test.go index 31d48498..28f444be 100644 --- a/app/rest/proxy/image_test.go +++ b/app/rest/proxy/image_test.go @@ -62,7 +62,7 @@ func TestImage_Routes(t *testing.T) { img := Image{Enabled: true, RemarkURL: "https://demo.remark42.com", RoutePath: "/api/v1/proxy"} router := img.Routes() - httpSrv := imgHttpServer(t) + httpSrv := imgHTTPServer(t) defer httpSrv.Close() ts := httptest.NewServer(router) defer ts.Close() @@ -88,7 +88,7 @@ func TestPicture_Convert(t *testing.T) { assert.Equal(t, ` xyz `, r) } -func imgHttpServer(t *testing.T) *httptest.Server { +func imgHTTPServer(t *testing.T) *httptest.Server { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/image/img1.png" { t.Log("http img request", r.URL)