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 "+
|
2022-04-05 21:14:22 +00:00
|
|
|
"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
|
|
|
|
}
|