add natsapi flags

This commit is contained in:
wh1te909 2021-01-12 21:14:43 +00:00
parent ee44240569
commit a2f74e0488
2 changed files with 24 additions and 7 deletions

View File

@ -1,9 +1,15 @@
package main package main
import ( import (
"flag"
"github.com/wh1te909/tacticalrmm/natsapi" "github.com/wh1te909/tacticalrmm/natsapi"
) )
func main() { func main() {
api.Listen() apiHost := flag.String("api-host", "", "django api base url")
debug := flag.Bool("debug", false, "Debug")
flag.Parse()
api.Listen(*apiHost, *debug)
} }

View File

@ -18,7 +18,11 @@ import (
var rClient = resty.New() var rClient = resty.New()
func getAPI() (string, error) { func getAPI(apihost string) (string, error) {
if apihost != "" {
return apihost, nil
}
f, err := os.Open(`/etc/nginx/sites-available/rmm.conf`) f, err := os.Open(`/etc/nginx/sites-available/rmm.conf`)
if err != nil { if err != nil {
return "", err return "", err
@ -35,14 +39,21 @@ func getAPI() (string, error) {
return "", errors.New("unable to parse api from nginx conf") return "", errors.New("unable to parse api from nginx conf")
} }
func Listen() { func Listen(apihost string, debug bool) {
api, err := getAPI() var baseURL string
api, err := getAPI(apihost)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
//rClient.SetHostURL(fmt.Sprintf("https://%s/natsapi", api))
rClient.SetHostURL(fmt.Sprintf("http://%s:8000/natsapi", api)) if debug {
rClient.SetTimeout(5 * time.Second) baseURL = fmt.Sprintf("http://%s:8000/natsapi", api)
} else {
baseURL = fmt.Sprintf("https://%s/natsapi", api)
}
rClient.SetHostURL(baseURL)
rClient.SetTimeout(30 * time.Second)
natsinfo, err := rClient.R().SetResult(&NatsInfo{}).Get("/natsinfo/") natsinfo, err := rClient.R().SetResult(&NatsInfo{}).Get("/natsinfo/")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)