diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..af037e0 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,99 @@ +--- +run: + concurrency: 6 + deadline: 5m + skip-files: + - ".*_test\\.go" + - "lib/.*" +linters: + disable-all: true + enable: + - deadcode + - depguard + - dupl + - goconst + - gocritic + - gofmt + - goimports + - golint + - govet + - ineffassign + - misspell + - staticcheck + - structcheck + - typecheck + - unconvert + - unparam + - varcheck + - gocyclo +linters-settings: + gocritic: + enabled-checks: + # Diagnostic + - argOrder + - badCond + - caseOrder + - codegenComment + - commentedOutCode + - deprecatedComment + - dupArg + - dupBranchBody + - dupCase + - dupSubExpr + - exitAfterDefer + - flagDeref + - flagName + - nilValReturn + - offBy1 + - weakCond + #- appendAssign + - octalLiteral + - sloppyReassign + + # Performance + - equalFold + #- hugeParam + - indexAlloc + - rangeExprCopy + #- rangeValCopy + - appendCombine + + # Style + - assignOp + - boolExprSimplify + - captLocal + - commentFormatting + - commentedOutImport + - defaultCaseOrder + - docStub + - elseif + - emptyFallthrough + - emptyStringTest + - hexLiteral + #- ifElseChain + - methodExprCall + - regexpMust + - singleCaseSwitch + - sloppyLen + - stringXbytes + - switchTrue + - typeAssertChain + - typeSwitchVar + - underef + - unlabelStmt + - unlambda + - unslice + - valSwap + - yodaStyleExpr + - wrapperFunc + + # Opinionated + - initClause + - nestingReduce + - ptrToRefParam + - typeUnparen + - unnecessaryBlock + #- builtinShadow + #- importShadow + - paramTypeCombine + #- unnamedResult diff --git a/cmd/escapepod/serve.go b/cmd/escapepod/serve.go index 26e403e..ab528de 100644 --- a/cmd/escapepod/serve.go +++ b/cmd/escapepod/serve.go @@ -78,8 +78,8 @@ var serveCmd = &cobra.Command{ } }() - // gracefull shutdown - sigChan := make(chan os.Signal) + // graceful shutdown + sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) <-sigChan diff --git a/pkg/crawler/crawler.go b/pkg/crawler/crawler.go index 9ff559c..768fe4b 100644 --- a/pkg/crawler/crawler.go +++ b/pkg/crawler/crawler.go @@ -11,7 +11,7 @@ import ( type Job struct { } -/// Crawler +// Crawler type Crawler struct { db *gorm.DB workerChan chan *Job @@ -29,7 +29,7 @@ func NewCrawler(db *gorm.DB) *Crawler { return crawler } -/// Run intended to be run from a go routine +// Run intended to be run from a go routine func (c *Crawler) Run(concurrency int, stopCh <-chan struct{}) { go c.Scheduler(stopCh) for i := 0; i < concurrency; i++ { @@ -38,7 +38,7 @@ func (c *Crawler) Run(concurrency int, stopCh <-chan struct{}) { <-stopCh } -/// Scheduler intended to be run from a goroutine +// Scheduler intended to be run from a goroutine func (c *Crawler) Scheduler(stopCh <-chan struct{}) { log.Debug("Starting scheduler") defer log.Debug("Stopping scheduler") diff --git a/pkg/db/db.go b/pkg/db/db.go index 839303b..1c6bacb 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -5,7 +5,9 @@ import ( "strings" "github.com/jinzhu/gorm" + // import postgres _ "github.com/jinzhu/gorm/dialects/postgres" + // import sqlite _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/labstack/gommon/log" "github.com/spf13/viper" @@ -13,17 +15,8 @@ import ( "github.com/rphillips/escapepod/pkg/models" ) -type DBParams struct { - Host string - Port int - User string - Password string - DBName string - SSLMode string -} - func openPostgres(dbURL string) (*gorm.DB, error) { - if len(dbURL) == 0 { + if dbURL == "" { dbURL = strings.Join([]string{ "host=" + viper.GetString("db.host"), "port=" + viper.GetString("db.port"), diff --git a/pkg/handlers/podcasts.go b/pkg/handlers/podcasts.go index 07b07e3..9251e0f 100644 --- a/pkg/handlers/podcasts.go +++ b/pkg/handlers/podcasts.go @@ -83,12 +83,16 @@ func PodcastsImport(c echo.Context) error { if err != nil { return c.JSON(http.StatusBadRequest, err) } - go importFromOPML(app, parsed) + go func() { + if err := importFromOPML(app, parsed); err != nil { + c.Logger().Errorf("could not import OPML: %v", err) + } + }() return c.JSON(http.StatusOK, "OK") } func importFromOPML(app *app.App, o *opml.OPML) error { - if len(o.Body.Outline) <= 0 { + if len(o.Body.Outline) == 0 { return fmt.Errorf("invalid opml") } if o.Body.Outline[0].Text != "feeds" { diff --git a/pkg/models/podcast.go b/pkg/models/podcast.go index c9e44df..a39d2eb 100644 --- a/pkg/models/podcast.go +++ b/pkg/models/podcast.go @@ -5,7 +5,10 @@ import ( "bytes" "image" "image/jpeg" + + // import png to support png _ "image/png" + "io" "io/ioutil" "net/http" @@ -36,8 +39,8 @@ type Podcast struct { func (p *Podcast) Crawl() error { parser := gofeed.NewParser() var feed *gofeed.Feed - if strings.HasPrefix("file://", *p.FeedURL) { - fileData, err := ioutil.ReadFile(strings.TrimPrefix("file://", *p.FeedURL)) + if strings.HasPrefix(*p.FeedURL, "file://") { + fileData, err := ioutil.ReadFile(strings.TrimPrefix(*p.FeedURL, "file://")) if err != nil { return err } diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 52ced0f..f4c936f 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -15,14 +15,12 @@ func RegisterRoutes(r *echo.Echo, fs stuffbin.FileSystem) { r.GET("/*", echo.WrapHandler(fs.FileServer())) api := r.Group("/api/v1") - { - api.POST("/podcasts", handlers.PodcastsCreate) - api.POST("/podcasts/import", handlers.PodcastsImport) - api.GET("/podcasts", handlers.PodcastsList) - api.GET("/podcasts/:id", handlers.PodcastsGet) - api.GET("/podcasts/:id/episodes", handlers.PodcastsGetEpisodes) - api.GET("/podcasts/:id/image", handlers.PodcastsImageGet) - } + api.POST("/podcasts", handlers.PodcastsCreate) + api.POST("/podcasts/import", handlers.PodcastsImport) + api.GET("/podcasts", handlers.PodcastsList) + api.GET("/podcasts/:id", handlers.PodcastsGet) + api.GET("/podcasts/:id/episodes", handlers.PodcastsGetEpisodes) + api.GET("/podcasts/:id/image", handlers.PodcastsImageGet) } func handleIndexPage(c echo.Context) error {