Skip to content

Commit 881a4c5

Browse files
committed
completed config and env setup
1 parent 0db3059 commit 881a4c5

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

.env.sample

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GATEWAYGO_CONFIG_FILE = "configs/gatewaygo.config.json"

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*config.json
2+
*.env

configs/gatewaygo.config.sample.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"listen_address": "0.0.0.0:8130",
3+
"db_type": "postgres",
4+
"db_user": "postgres",
5+
"db_password": "123456",
6+
"db_host": "localhost",
7+
"db_database": "database"
8+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ go 1.16
55
require (
66
github.com/go-sql-driver/mysql v1.6.0 // indirect
77
github.com/gorilla/mux v1.8.0 // indirect
8+
github.com/joho/godotenv v1.3.0 // indirect
89
github.com/lib/pq v1.10.2 // indirect
10+
github.com/pkg/errors v0.9.1 // indirect
11+
github.com/sirupsen/logrus v1.8.1 // indirect
912
)

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
23
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
34
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
45
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
6+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
7+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
58
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
69
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
10+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
11+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
14+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
15+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
16+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
17+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

main.go

+71-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
11
package main
22

3-
func main() {
4-
db_type := "postgres"
5-
db_user := "postgres"
6-
db_password := "123456"
7-
db_host := "localhost"
8-
db_database := "dbSapHasap"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
9+
"github.com/joho/godotenv"
10+
"github.com/pkg/errors"
11+
log "github.com/sirupsen/logrus"
12+
)
13+
14+
type config struct {
15+
ListenAddress string `json:"listen_address"`
16+
DB_Type string `json:"db_type"`
17+
DB_User string `json:"db_user"`
18+
DB_Password string `json:"db_password"`
19+
DB_Host string `json:"db_host"`
20+
DB_Database string `json:"db_database"`
21+
}
22+
23+
func ReadConfig(source string) (c *config, err error) {
24+
var raw []byte
25+
raw, err = ioutil.ReadFile(source)
26+
if err != nil {
27+
eMsg := "error reading config from file"
28+
log.WithError(err).Error(eMsg)
29+
err = errors.Wrap(err, eMsg)
30+
return
31+
}
32+
err = json.Unmarshal(raw, &c)
33+
if err != nil {
34+
eMsg := "error parsing config from json"
35+
log.WithError(err).Error(eMsg)
36+
err = errors.Wrap(err, eMsg)
37+
c = nil
38+
}
39+
return
40+
}
41+
42+
func run() error {
43+
var configFile string
44+
var conf *config
45+
var err error
46+
err = godotenv.Load()
47+
if err != nil {
48+
log.WithError(err).Error("error loading .env, ignoring")
49+
}
50+
configFile = os.Getenv("GATEWAYGO_CONFIG_FILE")
51+
if configFile == "" {
52+
configFile = "gatewaygo.config.json"
53+
}
54+
conf, err = ReadConfig(configFile)
55+
if err != nil {
56+
log.WithError(err).WithField("config-file", configFile).Error("error loading configuration")
57+
return err
58+
}
59+
60+
db_type := conf.DB_Type
61+
db_user := conf.DB_User
62+
db_password := conf.DB_Password
63+
db_host := conf.DB_Host
64+
db_database := conf.DB_Database
965

1066
a := App{}
1167
a.Initialize(
@@ -16,6 +72,14 @@ func main() {
1672
db_database,
1773
)
1874

19-
a.Run(":8080")
75+
a.Run(conf.ListenAddress)
2076

77+
return nil
78+
}
79+
80+
func main() {
81+
if err := run(); err != nil {
82+
fmt.Fprintf(os.Stderr, "%v", err)
83+
os.Exit(1)
84+
}
2185
}

0 commit comments

Comments
 (0)