tacticalrmm/natsapi/utils.go

54 lines
1.0 KiB
Go
Raw Normal View History

2021-11-13 00:45:28 +00:00
package api
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
nats "github.com/nats-io/nats.go"
trmm "github.com/wh1te909/trmm-shared"
)
func setupNatsOptions(key string) []nats.Option {
opts := []nats.Option{
nats.Name("TacticalRMM"),
nats.UserInfo("tacticalrmm", key),
nats.ReconnectWait(time.Second * 2),
nats.RetryOnFailedConnect(true),
nats.MaxReconnects(-1),
nats.ReconnectBufSize(-1),
}
return opts
}
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=disable",
r.Host, r.Port, r.User, r.Pass, r.DBName)
db, err = sqlx.Connect("postgres", psqlInfo)
if err != nil {
return
}
db.SetMaxOpenConns(20)
return
}