Skip to content

Commit a2b594a

Browse files
author
Mikhail Podtserkovskiy
committed
refactoring
1 parent f2ca887 commit a2b594a

File tree

13 files changed

+196
-158
lines changed

13 files changed

+196
-158
lines changed

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package config
33
import (
44
"encoding/json"
55
"errors"
6+
"github.com/Sirupsen/logrus"
67
log "github.com/Sirupsen/logrus"
78
"os"
8-
"github.com/Sirupsen/logrus"
99
)
1010

1111
type Config struct {
@@ -33,7 +33,7 @@ type Logger struct {
3333

3434
type DB struct {
3535
Implementation string `json:"implementation"`
36-
Connection string `json:"connection"`
36+
Connection string `json:"connection"`
3737
}
3838

3939
type Statsd struct {

handlers/registerNode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strconv"
88

99
log "github.com/Sirupsen/logrus"
10-
"github.com/qa-dev/jsonwire-grid/pool"
1110
"github.com/qa-dev/jsonwire-grid/jsonwire"
11+
"github.com/qa-dev/jsonwire-grid/pool"
1212
)
1313

1414
type RegisterNode struct {

invoke.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,20 @@
11
package main
22

33
import (
4-
log "github.com/Sirupsen/logrus"
54
_ "github.com/go-sql-driver/mysql"
6-
"github.com/jmoiron/sqlx"
75
"github.com/qa-dev/jsonwire-grid/config"
8-
"github.com/qa-dev/jsonwire-grid/pool"
96

107
"errors"
11-
mysqlMigrations "github.com/qa-dev/jsonwire-grid/storage/migrations/mysql"
8+
"github.com/qa-dev/jsonwire-grid/storage"
129
"github.com/qa-dev/jsonwire-grid/storage/mysql"
13-
"github.com/rubenv/sql-migrate"
1410
)
1511

16-
func invokeStorage(config config.Config) (storage pool.StorageInterface, err error) {
12+
func invokeStorageFactory(config config.Config) (factory storage.StorageFactoryInterface, err error) {
1713
switch config.DB.Implementation {
1814
case "mysql":
19-
var db *sqlx.DB
20-
db, err = sqlx.Open("mysql", config.DB.Connection)
21-
if err != nil {
22-
err = errors.New("Database connection error: " + err.Error())
23-
return
24-
}
25-
26-
storage = mysql.NewMysqlStorage(db)
27-
28-
migrations := &migrate.AssetMigrationSource{
29-
Asset: mysqlMigrations.Asset,
30-
AssetDir: mysqlMigrations.AssetDir,
31-
Dir: "storage/migrations/mysql",
32-
}
33-
var n int
34-
n, err = migrate.Exec(db.DB, "mysql", migrations, migrate.Up)
35-
if err != nil {
36-
err = errors.New("Migrations failed, " + err.Error())
37-
return
38-
}
39-
log.Infof("Applied %d migrations!\n", n)
15+
factory = new(mysql.Factory)
4016
default:
41-
err = errors.New("Invalid config, unknown param [DB.Implementation=" + config.DB.Implementation + "]")
17+
err = errors.New("Invalid config, unknown param [db.implementation=" + config.DB.Implementation + "]")
4218
}
4319
return
4420
}

jsonwire/jsonwire.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
)
66

77
type Message struct {
8-
SessionId string `json:"sessionId"`
9-
Status int `json:"status"`
8+
SessionId string `json:"sessionId"`
9+
Status int `json:"status"`
1010
Value interface{} `json:"value"`
1111
}
1212

@@ -26,9 +26,9 @@ type Sessions struct {
2626
}
2727

2828
type Register struct {
29-
Class string `json:"class"`
29+
Class string `json:"class"`
3030
Configuration *Configuration `json:"configuration"`
31-
CapabilitiesList []Capabilities `json:"capabilities"` // selenium 3
31+
CapabilitiesList []Capabilities `json:"capabilities"` // selenium 3
3232
}
3333

3434
type Capabilities map[string]interface{}
@@ -50,8 +50,8 @@ type Configuration struct {
5050
}
5151

5252
type ApiProxy struct {
53-
ID string `json:"id"`
53+
ID string `json:"id"`
5454
Request interface{} `json:"request"` //todo: пока не ясно зачем он нужен
55-
Msg string `json:"msg"`
56-
Success bool `json:"success"`
57-
}
55+
Msg string `json:"msg"`
56+
Success bool `json:"success"`
57+
}

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ func main() {
4444
if err != nil {
4545
log.Fatal("Invalid value grid.busy_node_duration in config")
4646
}
47-
storage, err := invokeStorage(*cfg)
47+
storageFactory, err := invokeStorageFactory(*cfg)
4848
if err != nil {
49-
log.Fatalf("Can't invoke storage, %s", err)
49+
log.Fatalf("Can't invoke storage factory, %s", err)
50+
}
51+
storage, err := storageFactory.Create(*cfg)
52+
if err != nil {
53+
log.Fatalf("Can't create storage factory, %s", err)
5054
}
5155
poolInstance := pool.NewPool(storage)
5256
poolInstance.SetBusyNodeDuration(busyNodeDuration)

selenium/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package selenium
22

33
import (
44
"encoding/json"
5+
"errors"
56
"github.com/qa-dev/jsonwire-grid/jsonwire"
67
"io/ioutil"
78
"net/http"
89
"net/url"
910
"strings"
10-
"errors"
1111
)
1212

1313
type Client struct {
@@ -102,7 +102,7 @@ func (req request) send(outputStruct interface{}) error {
102102
}
103103
err = json.Unmarshal(body, outputStruct)
104104
if err != nil {
105-
err = errors.New("can't unmarshal response ["+ string(body)+"], " + err.Error())
105+
err = errors.New("can't unmarshal response [" + string(body) + "], " + err.Error())
106106
return err
107107
}
108108
return nil

storage/comon_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package storage
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"github.com/qa-dev/jsonwire-grid/pool"
5+
"github.com/stretchr/testify/assert"
66
"testing"
77
)
88

storage/factory.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package storage
2+
3+
import (
4+
"github.com/qa-dev/jsonwire-grid/config"
5+
"github.com/qa-dev/jsonwire-grid/pool"
6+
)
7+
8+
type StorageFactoryInterface interface {
9+
Create(config.Config) (pool.StorageInterface, error)
10+
}

storage/mysql/factory.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package mysql
2+
3+
import (
4+
"errors"
5+
log "github.com/Sirupsen/logrus"
6+
_ "github.com/go-sql-driver/mysql"
7+
"github.com/jmoiron/sqlx"
8+
"github.com/qa-dev/jsonwire-grid/config"
9+
"github.com/qa-dev/jsonwire-grid/pool"
10+
mysqlMigrations "github.com/qa-dev/jsonwire-grid/storage/migrations/mysql"
11+
"github.com/rubenv/sql-migrate"
12+
)
13+
14+
type Factory struct {
15+
}
16+
17+
func (f *Factory) Create(config config.Config) (pool.StorageInterface, error) {
18+
db, err := sqlx.Open("mysql", config.DB.Connection)
19+
if err != nil {
20+
err = errors.New("Database connection error: " + err.Error())
21+
return nil, err
22+
}
23+
24+
storage := NewMysqlStorage(db)
25+
26+
migrations := &migrate.AssetMigrationSource{
27+
Asset: mysqlMigrations.Asset,
28+
AssetDir: mysqlMigrations.AssetDir,
29+
Dir: "storage/migrations/mysql",
30+
}
31+
n, err := migrate.Exec(db.DB, "mysql", migrations, migrate.Up)
32+
if err != nil {
33+
err = errors.New("Migrations failed, " + err.Error())
34+
return nil, err
35+
}
36+
log.Infof("Applied %d migrations!\n", n)
37+
return storage, nil
38+
}

storage/mysql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"fmt"
77
_ "github.com/go-sql-driver/mysql"
88
"github.com/jmoiron/sqlx"
9-
"github.com/rubenv/sql-migrate"
109
"github.com/qa-dev/jsonwire-grid/pool"
1110
"github.com/qa-dev/jsonwire-grid/storage/mysql"
11+
"github.com/rubenv/sql-migrate"
1212
"os"
1313
"strings"
1414
"testing"

0 commit comments

Comments
 (0)