Merge pull request #1 from txgruppi/master

Added Config.DatabaseAddress
This commit is contained in:
Patrick Nagurny
2019-01-11 16:03:57 -05:00
committed by GitHub
3 changed files with 35 additions and 23 deletions

View File

@@ -1,9 +1,11 @@
{ {
"WebUrl": "https://domain.com", "WebUrl": "https://domain.com",
"Address": "",
"Port": 8080, "Port": 8080,
"ApiPrefix": "", "ApiPrefix": "",
"KeyFile": "", "KeyFile": "",
"CertFile": "", "CertFile": "",
"DatabaseAddress": "",
"Database": "openaccounting", "Database": "openaccounting",
"User": "openaccounting", "User": "openaccounting",
"Password": "openaccounting", "Password": "openaccounting",

View File

@@ -1,15 +1,17 @@
package types package types
type Config struct { type Config struct {
WebUrl string WebUrl string
Port int Address string
ApiPrefix string Port int
KeyFile string ApiPrefix string
CertFile string KeyFile string
Database string CertFile string
User string DatabaseAddress string
Password string Database string
SendgridKey string User string
SendgridEmail string Password string
SendgridSender string SendgridKey string
SendgridEmail string
SendgridSender string
} }

View File

@@ -2,17 +2,18 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/openaccounting/oa-server/core/api" "github.com/openaccounting/oa-server/core/api"
"github.com/openaccounting/oa-server/core/auth" "github.com/openaccounting/oa-server/core/auth"
"github.com/openaccounting/oa-server/core/model" "github.com/openaccounting/oa-server/core/model"
"github.com/openaccounting/oa-server/core/model/db" "github.com/openaccounting/oa-server/core/model/db"
"github.com/openaccounting/oa-server/core/model/types" "github.com/openaccounting/oa-server/core/model/types"
"github.com/openaccounting/oa-server/core/util" "github.com/openaccounting/oa-server/core/util"
"log"
"net/http"
"os"
"strconv"
//"fmt"
) )
func main() { func main() {
@@ -21,19 +22,22 @@ func main() {
file, err := os.Open("./config.json") file, err := os.Open("./config.json")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(fmt.Errorf("failed to open ./config.json with: %s", err.Error()))
} }
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
err = decoder.Decode(&config) err = decoder.Decode(&config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(fmt.Errorf("failed to decode ./config.json with: %s", err.Error()))
} }
connectionString := config.User + ":" + config.Password + "@/" + config.Database connectionString := config.User + ":" + config.Password + "@" + config.DatabaseAddress + "/" + config.Database
db, err := db.NewDB(connectionString) db, err := db.NewDB(connectionString)
if err != nil {
log.Fatal(fmt.Errorf("failed to connect to database with: %s", err.Error()))
}
bc := &util.StandardBcrypt{} bc := &util.StandardBcrypt{}
@@ -41,10 +45,14 @@ func main() {
auth.NewAuthService(db, bc) auth.NewAuthService(db, bc)
app, err := api.Init(config.ApiPrefix) app, err := api.Init(config.ApiPrefix)
if err != nil { 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())) if config.CertFile == "" || config.KeyFile == "" {
err = http.ListenAndServe(config.Address+":"+strconv.Itoa(config.Port), app.MakeHandler())
} else {
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()))
} }