Skip to content

Commit

Permalink
Refactoring and new features (#35)
Browse files Browse the repository at this point in the history
* new error collector & improvements for errors
* update golangci-lint, microoptimizations, linter fixes
* UseTLS10 method
* remove dead code
* add 1.17 to the test matrix
* fix for docstring
* split the core package symbols into the subpackages (if feasible)
  • Loading branch information
Neur0toxine authored Dec 1, 2021
1 parent b0d5488 commit 52109ee
Show file tree
Hide file tree
Showing 73 changed files with 2,322 additions and 752 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ jobs:
- name: Lint code with golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.36
version: v1.42.1
only-new-issues: true
tests:
name: Tests
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.16']
go-version: ['1.16', '1.17']
steps:
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
Expand Down
14 changes: 4 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ linters:
- gocyclo
- godot
- goimports
- golint
- gomnd
- revive
- gosec
- ifshort
- interfacer
- lll
- makezero
- maligned
- misspell
- nestif
- prealloc
- predeclared
- scopelint
- sqlclosecheck
- unconvert
- whitespace
Expand All @@ -56,9 +52,11 @@ linters-settings:
enable:
- assign
- atomic
- atomicalign
- bools
- buildtag
- copylocks
- fieldalignment
- httpresponse
- loopclosure
- lostcancel
Expand Down Expand Up @@ -152,12 +150,10 @@ linters-settings:
local-prefixes: github.com/retailcrm/mg-transport-core
lll:
line-length: 120
maligned:
suggest-new: true
misspell:
locale: US
nestif:
min-complexity: 4
min-complexity: 6
whitespace:
multi-if: false
multi-func: false
Expand All @@ -166,7 +162,6 @@ issues:
exclude-rules:
- path: _test\.go
linters:
- gomnd
- lll
- bodyclose
- errcheck
Expand All @@ -175,7 +170,6 @@ issues:
- ineffassign
- whitespace
- makezero
- maligned
- ifshort
- errcheck
- funlen
Expand Down
6 changes: 3 additions & 3 deletions cmd/transport-core-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/jessevdk/go-flags"

"github.com/retailcrm/mg-transport-core/core"
"github.com/retailcrm/mg-transport-core/v2/core/db"
)

// Options for tool command.
Expand All @@ -20,7 +20,7 @@ func init() {
_, err := parser.AddCommand("migration",
"Create new empty migration in specified directory.",
"Create new empty migration in specified directory.",
&core.NewMigrationCommand{},
&db.NewMigrationCommand{},
)

if err != nil {
Expand All @@ -30,7 +30,7 @@ func init() {

func main() {
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { // nolint:errorlint
os.Exit(0)
} else {
os.Exit(1)
Expand Down
38 changes: 16 additions & 22 deletions core/config.go → core/config/config.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package core
package config

import (
"io/ioutil"
"path/filepath"
"regexp"
"time"

"github.com/op/go-logging"
"gopkg.in/yaml.v2"
)

var (
markdownSymbols = []string{"*", "_", "`", "["}
slashRegex = regexp.MustCompile(`/+$`)
)

// ConfigInterface settings data structure.
type ConfigInterface interface {
// Configuration settings data structure.
type Configuration interface {
GetVersion() string
GetSentryDSN() string
GetLogLevel() logging.Level
GetHTTPConfig() HTTPServerConfig
GetDBConfig() DatabaseConfig
GetAWSConfig() ConfigAWS
GetAWSConfig() AWS
GetTransportInfo() InfoInterface
GetHTTPClientConfig() *HTTPClientConfig
GetUpdateInterval() int
Expand All @@ -39,16 +33,16 @@ type InfoInterface interface {

// Config struct.
type Config struct {
HTTPClientConfig *HTTPClientConfig `yaml:"http_client"`
ConfigAWS AWS `yaml:"config_aws"`
TransportInfo Info `yaml:"transport_info"`
HTTPServer HTTPServerConfig `yaml:"http_server"`
Version string `yaml:"version"`
LogLevel logging.Level `yaml:"log_level"`
Database DatabaseConfig `yaml:"database"`
SentryDSN string `yaml:"sentry_dsn"`
HTTPServer HTTPServerConfig `yaml:"http_server"`
Debug bool `yaml:"debug"`
Database DatabaseConfig `yaml:"database"`
UpdateInterval int `yaml:"update_interval"`
ConfigAWS ConfigAWS `yaml:"config_aws"`
TransportInfo Info `yaml:"transport_info"`
HTTPClientConfig *HTTPClientConfig `yaml:"http_client"`
LogLevel logging.Level `yaml:"log_level"`
Debug bool `yaml:"debug"`
}

// Info struct.
Expand All @@ -59,8 +53,8 @@ type Info struct {
Secret string `yaml:"secret"`
}

// ConfigAWS struct.
type ConfigAWS struct {
// AWS struct.
type AWS struct {
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
Region string `yaml:"region"`
Expand All @@ -72,19 +66,19 @@ type ConfigAWS struct {
// DatabaseConfig struct.
type DatabaseConfig struct {
Connection interface{} `yaml:"connection"`
Logging bool `yaml:"logging"`
TablePrefix string `yaml:"table_prefix"`
MaxOpenConnections int `yaml:"max_open_connections"`
MaxIdleConnections int `yaml:"max_idle_connections"`
ConnectionLifetime int `yaml:"connection_lifetime"`
Logging bool `yaml:"logging"`
}

// HTTPClientConfig struct.
type HTTPClientConfig struct {
Timeout time.Duration `yaml:"timeout"`
SSLVerification *bool `yaml:"ssl_verification"`
MockAddress string `yaml:"mock_address"`
MockedDomains []string `yaml:"mocked_domains"`
Timeout time.Duration `yaml:"timeout"`
}

// HTTPServerConfig struct.
Expand Down Expand Up @@ -157,7 +151,7 @@ func (c Config) IsDebug() bool {
}

// GetAWSConfig AWS configuration.
func (c Config) GetAWSConfig() ConfigAWS {
func (c Config) GetAWSConfig() AWS {
return c.ConfigAWS
}

Expand Down
2 changes: 1 addition & 1 deletion core/config_test.go → core/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package config

import (
"io/ioutil"
Expand Down
13 changes: 8 additions & 5 deletions core/migrate.go → core/db/migrate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package db

import (
"fmt"
Expand All @@ -16,9 +16,9 @@ var migrations *Migrate
type Migrate struct {
db *gorm.DB
first *gormigrate.Migration
versions []string
migrations map[string]*gormigrate.Migration
GORMigrate *gormigrate.Gormigrate
versions []string
prepared bool
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func (m *Migrate) MigrateNextTo(version string) error {
case current < next:
return m.GORMigrate.MigrateTo(next)
case current > next:
return errors.New(fmt.Sprintf("current migration version '%s' is higher than fetched version '%s'", current, next))
return fmt.Errorf("current migration version '%s' is higher than fetched version '%s'", current, next)
default:
return nil
}
Expand All @@ -144,7 +144,7 @@ func (m *Migrate) MigratePreviousTo(version string) error {
case current > prev:
return m.GORMigrate.RollbackTo(prev)
case current < prev:
return errors.New(fmt.Sprintf("current migration version '%s' is lower than fetched version '%s'", current, prev))
return fmt.Errorf("current migration version '%s' is lower than fetched version '%s'", current, prev)
case prev == "0":
return m.GORMigrate.RollbackMigration(m.first)
default:
Expand Down Expand Up @@ -241,8 +241,11 @@ func (m *Migrate) prepareMigrations() error {
return nil
}

i := 0
keys = make([]string, len(m.migrations))
for key := range m.migrations {
keys = append(keys, key)
keys[i] = key
i++
}

sort.Strings(keys)
Expand Down
2 changes: 1 addition & 1 deletion core/migrate_test.go → core/db/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package db

import (
"database/sql"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package db

import (
"fmt"
Expand Down Expand Up @@ -32,7 +32,7 @@ func init() {

// NewMigrationCommand struct.
type NewMigrationCommand struct {
Directory string `short:"d" long:"directory" default:"./migrations" description:"Directory where migration will be created"`
Directory string `short:"d" long:"directory" default:"./migrations" description:"Directory where migration will be created"` // nolint:lll
}

// FileExists returns true if provided file exist and it's not directory.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package db

import (
"fmt"
Expand Down Expand Up @@ -26,8 +26,8 @@ func (s *MigrationGeneratorSuite) SetupSuite() {

func (s *MigrationGeneratorSuite) Test_FileExists() {
var (
seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
notExist = fmt.Sprintf("/tmp/%d", seededRand.Int31())
seededRand = rand.New(rand.NewSource(time.Now().UnixNano())) // nolint:gosec
notExist = fmt.Sprintf("/tmp/%d", seededRand.Int31())
)

assert.False(s.T(), s.command.FileExists(notExist))
Expand Down
18 changes: 18 additions & 0 deletions core/db/models/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package models

import "time"

// Account model.
type Account struct {
CreatedAt time.Time
UpdatedAt time.Time
ChannelSettingsHash string `gorm:"column:channel_settings_hash; type:varchar(70)" binding:"max=70"`
Name string `gorm:"column:name; type:varchar(100)" json:"name,omitempty" binding:"max=100"`
Lang string `gorm:"column:lang; type:varchar(2)" json:"lang,omitempty" binding:"max=2"`
Channel uint64 `gorm:"column:channel; not null; unique" json:"channel,omitempty"`
ID int `gorm:"primary_key"`
ConnectionID int `gorm:"column:connection_id" json:"connectionId,omitempty"`
}

// Accounts list.
type Accounts []Account
17 changes: 17 additions & 0 deletions core/db/models/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

import "time"

// Connection model.
type Connection struct {
CreatedAt time.Time
UpdatedAt time.Time
Key string `gorm:"column:api_key; type:varchar(100); not null" json:"api_key,omitempty" binding:"required,max=100"` // nolint:lll
URL string `gorm:"column:api_url; type:varchar(255); not null" json:"api_url,omitempty" binding:"required,validateCrmURL,max=255"` // nolint:lll
GateURL string `gorm:"column:mg_url; type:varchar(255); not null;" json:"mg_url,omitempty" binding:"max=255"`
GateToken string `gorm:"column:mg_token; type:varchar(100); not null; unique" json:"mg_token,omitempty" binding:"max=100"` // nolint:lll
ClientID string `gorm:"column:client_id; type:varchar(70); not null; unique" json:"clientId,omitempty"`
Accounts []Account `gorm:"foreignkey:ConnectionID"`
ID int `gorm:"primary_key"`
Active bool `json:"active,omitempty"`
}
24 changes: 24 additions & 0 deletions core/db/models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package models

import "time"

// User model.
type User struct {
CreatedAt time.Time
UpdatedAt time.Time
ExternalID string `gorm:"column:external_id; type:varchar(255); not null; unique"`
UserPhotoURL string `gorm:"column:user_photo_url; type:varchar(255)" binding:"max=255"`
UserPhotoID string `gorm:"column:user_photo_id; type:varchar(100)" binding:"max=100"`
ID int `gorm:"primary_key"`
}

// TableName will return table name for User
// It will not work if User is not embedded, but mapped as another type
// type MyUser User // will not work
// but
// type MyUser struct { // will work
// User
// }
func (User) TableName() string {
return "mg_user"
}
2 changes: 1 addition & 1 deletion core/models_test.go → core/db/models/user_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package models

import (
"testing"
Expand Down
12 changes: 8 additions & 4 deletions core/orm.go → core/db/orm.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package core
package db

import (
"time"

"github.com/jinzhu/gorm"

"github.com/retailcrm/mg-transport-core/v2/core/config"

// PostgreSQL is an default.
_ "github.com/jinzhu/gorm/dialects/postgres"
)
Expand All @@ -14,13 +17,14 @@ type ORM struct {
}

// NewORM will init new database connection.
func NewORM(config DatabaseConfig) *ORM {
func NewORM(config config.DatabaseConfig) *ORM {
orm := &ORM{}
orm.createDB(config)
orm.CreateDB(config)
return orm
}

func (orm *ORM) createDB(config DatabaseConfig) {
// CreateDB connection using provided config.
func (orm *ORM) CreateDB(config config.DatabaseConfig) {
db, err := gorm.Open("postgres", config.Connection)
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 52109ee

Please sign in to comment.