Skip to content

Commit 97a0a65

Browse files
committed
completed mssql connection
1 parent 881a4c5 commit 97a0a65

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

app.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net/http"
88

9+
_ "github.com/denisenkom/go-mssqldb"
910
"github.com/gorilla/mux"
1011
_ "github.com/lib/pq"
1112
)
@@ -15,12 +16,18 @@ type App struct {
1516
DB *sql.DB
1617
}
1718

18-
func (a *App) Initialize(db_type, db_user, db_password, db_host, db_database string) {
19+
func (a *App) Initialize(db_type, db_user, db_password, db_host, db_database, db_params string) {
1920

20-
connStr := fmt.Sprintf("%s://%s:%s@%s/%s?sslmode=disable", db_type, db_user, db_password, db_host, db_database)
21+
var connStr string
22+
switch db_type {
23+
case "sqlserver":
24+
connStr = fmt.Sprintf("%s://%s:%s@%s/instance?database=%s&%s", db_type, db_user, db_password, db_host, db_database, db_params)
25+
default:
26+
connStr = fmt.Sprintf("%s://%s:%s@%s/%s?%s", db_type, db_user, db_password, db_host, db_database, db_params)
27+
}
2128

2229
var err error
23-
a.DB, err = sql.Open("postgres", connStr)
30+
a.DB, err = sql.Open(db_type, connStr)
2431
if err != nil {
2532
log.Fatal(err)
2633
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"listen_address": "0.0.0.0:8000",
3+
"db_type": "postgres",
4+
"db_user": "postgres",
5+
"db_password": "123456",
6+
"db_host": "localhost",
7+
"db_database": "database",
8+
"db_params": "sslmode=disable"
9+
}

configs/gatewaygo.config.sample.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"db_user": "postgres",
55
"db_password": "123456",
66
"db_host": "localhost",
7-
"db_database": "database"
7+
"db_database": "database",
8+
"db_params": "sslmode=disable"
89
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/mike-bionic/db-gateway-go
33
go 1.16
44

55
require (
6-
github.com/go-sql-driver/mysql v1.6.0 // indirect
6+
github.com/denisenkom/go-mssqldb v0.10.0 // indirect
77
github.com/gorilla/mux v1.8.0 // indirect
88
github.com/joho/godotenv v1.3.0 // indirect
99
github.com/lib/pq v1.10.2 // indirect

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
3-
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2+
github.com/denisenkom/go-mssqldb v0.10.0 h1:QykgLZBorFE95+gO3u9esLd0BmbvpWp0/waNNZfHBM8=
3+
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
4+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
5+
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
46
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
57
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
68
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
@@ -13,5 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
1315
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
1416
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
1517
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
18+
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
19+
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
20+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
1621
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
1722
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type config struct {
1818
DB_Password string `json:"db_password"`
1919
DB_Host string `json:"db_host"`
2020
DB_Database string `json:"db_database"`
21+
DB_Params string `json:"db_params"`
2122
}
2223

2324
func ReadConfig(source string) (c *config, err error) {
@@ -62,6 +63,7 @@ func run() error {
6263
db_password := conf.DB_Password
6364
db_host := conf.DB_Host
6465
db_database := conf.DB_Database
66+
db_params := conf.DB_Params
6567

6668
a := App{}
6769
a.Initialize(
@@ -70,6 +72,7 @@ func run() error {
7072
db_password,
7173
db_host,
7274
db_database,
75+
db_params,
7376
)
7477

7578
a.Run(conf.ListenAddress)

0 commit comments

Comments
 (0)