From c407945109ff59e34d45459a77e979c2a9f10253 Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Fri, 11 Jan 2019 15:13:42 -0200 Subject: [PATCH 1/6] Added Config.DatabaseAddress This allows the server to connect to a MySQL server in a different machine. --- config.json.sample | 3 ++- core/model/types/config.go | 23 ++++++++++++----------- core/server.go | 11 ++++++----- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/config.json.sample b/config.json.sample index f38ab93..591aeef 100644 --- a/config.json.sample +++ b/config.json.sample @@ -4,10 +4,11 @@ "ApiPrefix": "", "KeyFile": "", "CertFile": "", + "DatabaseAddress": "", "Database": "openaccounting", "User": "openaccounting", "Password": "openaccounting", "SendgridKey": "", "SendgridEmail": "noreply@domain.com", "SendgridSender": "Sender" -} \ No newline at end of file +} diff --git a/core/model/types/config.go b/core/model/types/config.go index 92b1ef0..58c28c0 100644 --- a/core/model/types/config.go +++ b/core/model/types/config.go @@ -1,15 +1,16 @@ package types type Config struct { - WebUrl string - Port int - ApiPrefix string - KeyFile string - CertFile string - Database string - User string - Password string - SendgridKey string - SendgridEmail string - SendgridSender string + WebUrl string + Port int + ApiPrefix string + KeyFile string + CertFile string + DatabaseAddress string + Database string + User string + Password string + SendgridKey string + SendgridEmail string + SendgridSender string } diff --git a/core/server.go b/core/server.go index 817bac9..354ee4a 100644 --- a/core/server.go +++ b/core/server.go @@ -2,16 +2,17 @@ package main import ( "encoding/json" + "log" + "net/http" + "os" + "strconv" + "github.com/openaccounting/oa-server/core/api" "github.com/openaccounting/oa-server/core/auth" "github.com/openaccounting/oa-server/core/model" "github.com/openaccounting/oa-server/core/model/db" "github.com/openaccounting/oa-server/core/model/types" "github.com/openaccounting/oa-server/core/util" - "log" - "net/http" - "os" - "strconv" //"fmt" ) @@ -31,7 +32,7 @@ func main() { log.Fatal(err) } - connectionString := config.User + ":" + config.Password + "@/" + config.Database + connectionString := config.User + ":" + config.Password + "@" + config.DatabaseAddress + "/" + config.Database db, err := db.NewDB(connectionString) From 84132035046920b8f0b2b0c88320cdf250191a83 Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Fri, 11 Jan 2019 15:49:21 -0200 Subject: [PATCH 2/6] Updated log messages for ./config.json operations --- core/server.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/server.go b/core/server.go index 354ee4a..c1051cd 100644 --- a/core/server.go +++ b/core/server.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "log" "net/http" "os" @@ -13,7 +14,6 @@ import ( "github.com/openaccounting/oa-server/core/model/db" "github.com/openaccounting/oa-server/core/model/types" "github.com/openaccounting/oa-server/core/util" - //"fmt" ) func main() { @@ -22,14 +22,14 @@ func main() { file, err := os.Open("./config.json") if err != nil { - log.Fatal(err) + log.Fatal(fmt.Errorf("failed to open ./config.json with " + err.Error())) } decoder := json.NewDecoder(file) err = decoder.Decode(&config) if err != nil { - log.Fatal(err) + log.Fatal(fmt.Errorf("failed to decode ./config.json with " + err.Error())) } connectionString := config.User + ":" + config.Password + "@" + config.DatabaseAddress + "/" + config.Database From 82757607f8d96717d42c948dd9e434fe49137e2c Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Fri, 11 Jan 2019 16:15:49 -0200 Subject: [PATCH 3/6] Updated error logging in core/server.go --- core/server.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/server.go b/core/server.go index c1051cd..6e836f4 100644 --- a/core/server.go +++ b/core/server.go @@ -22,14 +22,14 @@ func main() { file, err := os.Open("./config.json") if err != nil { - log.Fatal(fmt.Errorf("failed to open ./config.json with " + err.Error())) + log.Fatal(fmt.Errorf("failed to open ./config.json with: %s", err.Error())) } decoder := json.NewDecoder(file) err = decoder.Decode(&config) if err != nil { - log.Fatal(fmt.Errorf("failed to decode ./config.json with " + err.Error())) + log.Fatal(fmt.Errorf("failed to decode ./config.json with: %s", err.Error())) } connectionString := config.User + ":" + config.Password + "@" + config.DatabaseAddress + "/" + config.Database @@ -44,8 +44,9 @@ func main() { app, err := api.Init(config.ApiPrefix) if err != nil { - log.Fatal(err) + log.Fatal(fmt.Errorf("failed to create api instance with: %s", err.Error())) } - log.Fatal(http.ListenAndServeTLS(":"+strconv.Itoa(config.Port), config.CertFile, config.KeyFile, app.MakeHandler())) + err = http.ListenAndServeTLS(":"+strconv.Itoa(config.Port), config.CertFile, config.KeyFile, app.MakeHandler()) + log.Fatal(fmt.Errorf("failed to start server with: %s", err.Error())) } From b1fdfcff377429a601e47bd09d35b1485eddda2c Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Fri, 11 Jan 2019 16:16:22 -0200 Subject: [PATCH 4/6] Enable the server to run without SSL if not certificate is provided --- core/server.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/server.go b/core/server.go index 6e836f4..1dfc574 100644 --- a/core/server.go +++ b/core/server.go @@ -47,6 +47,10 @@ func main() { log.Fatal(fmt.Errorf("failed to create api instance with: %s", err.Error())) } - err = http.ListenAndServeTLS(":"+strconv.Itoa(config.Port), config.CertFile, config.KeyFile, app.MakeHandler()) + if config.CertFile == "" || config.KeyFile == "" { + err = http.ListenAndServe(":"+strconv.Itoa(config.Port), app.MakeHandler()) + } else { + err = http.ListenAndServeTLS(":"+strconv.Itoa(config.Port), config.CertFile, config.KeyFile, app.MakeHandler()) + } log.Fatal(fmt.Errorf("failed to start server with: %s", err.Error())) } From 0f8d2d65d8038c5886d0bdc1f953f5b6ade1b2d0 Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Fri, 11 Jan 2019 16:16:56 -0200 Subject: [PATCH 5/6] Added Config.Address to allow the server to bind to a different IP address --- config.json.sample | 1 + core/model/types/config.go | 1 + core/server.go | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/config.json.sample b/config.json.sample index 591aeef..a77a167 100644 --- a/config.json.sample +++ b/config.json.sample @@ -1,5 +1,6 @@ { "WebUrl": "https://domain.com", + "Address": "", "Port": 8080, "ApiPrefix": "", "KeyFile": "", diff --git a/core/model/types/config.go b/core/model/types/config.go index 58c28c0..65e5842 100644 --- a/core/model/types/config.go +++ b/core/model/types/config.go @@ -2,6 +2,7 @@ package types type Config struct { WebUrl string + Address string Port int ApiPrefix string KeyFile string diff --git a/core/server.go b/core/server.go index 1dfc574..b9e8b90 100644 --- a/core/server.go +++ b/core/server.go @@ -48,9 +48,9 @@ func main() { } if config.CertFile == "" || config.KeyFile == "" { - err = http.ListenAndServe(":"+strconv.Itoa(config.Port), app.MakeHandler()) + err = http.ListenAndServe(config.Address+":"+strconv.Itoa(config.Port), app.MakeHandler()) } else { - err = http.ListenAndServeTLS(":"+strconv.Itoa(config.Port), config.CertFile, config.KeyFile, app.MakeHandler()) + err = http.ListenAndServeTLS(config.Address+":"+strconv.Itoa(config.Port), config.CertFile, config.KeyFile, app.MakeHandler()) } log.Fatal(fmt.Errorf("failed to start server with: %s", err.Error())) } From cf543ca60bf4803dc673dacca54c5bc3f0cbf4d5 Mon Sep 17 00:00:00 2001 From: Tarcisio Gruppi Date: Fri, 11 Jan 2019 17:23:44 -0200 Subject: [PATCH 6/6] Added log for database connection error --- core/server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/server.go b/core/server.go index b9e8b90..71457bc 100644 --- a/core/server.go +++ b/core/server.go @@ -35,6 +35,9 @@ func main() { connectionString := config.User + ":" + config.Password + "@" + config.DatabaseAddress + "/" + config.Database db, err := db.NewDB(connectionString) + if err != nil { + log.Fatal(fmt.Errorf("failed to connect to database with: %s", err.Error())) + } bc := &util.StandardBcrypt{} @@ -42,7 +45,6 @@ func main() { auth.NewAuthService(db, bc) app, err := api.Init(config.ApiPrefix) - if err != nil { log.Fatal(fmt.Errorf("failed to create api instance with: %s", err.Error())) }