tacticalrmm/natsapi/utils.go

40 lines
742 B
Go
Raw Normal View History

2021-11-13 00:45:28 +00:00
package api
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
trmm "github.com/wh1te909/trmm-shared"
)
func GetConfig(cfg string) (db *sqlx.DB, r DjangoConfig, err error) {
if cfg == "" {
2021-11-13 21:30:01 +00:00
cfg = "/rmm/api/tacticalrmm/nats-api.conf"
2021-11-13 00:45:28 +00:00
if !trmm.FileExists(cfg) {
err = errors.New("unable to find config file")
return
}
}
jret, _ := ioutil.ReadFile(cfg)
err = json.Unmarshal(jret, &r)
if err != nil {
return
}
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=%s",
r.Host, r.Port, r.User, r.Pass, r.DBName, r.SSLMode)
2021-11-13 00:45:28 +00:00
db, err = sqlx.Connect("postgres", psqlInfo)
if err != nil {
return
}
db.SetMaxOpenConns(20)
return
}