diff --git a/main.go b/main.go index 5dc1e93f..ee8dea50 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,7 @@ package main +// env CGO_ENABLED=0 go build -v -a -tags netgo -installsuffix netgo -ldflags "-s -w" -o nats-api + import ( "flag" "fmt" @@ -11,7 +13,8 @@ var version = "1.0.0" func main() { ver := flag.Bool("version", false, "Prints version") - apiHost := flag.String("api-host", "", "django api base url") + apiHost := flag.String("api-host", "", "django full base url") + natsHost := flag.String("nats-host", "", "nats full connection string") debug := flag.Bool("debug", false, "Debug") flag.Parse() @@ -20,5 +23,5 @@ func main() { return } - api.Listen(*apiHost, *debug) + api.Listen(*apiHost, *natsHost, *debug) } diff --git a/natsapi/api.go b/natsapi/api.go index f4fa1636..0d67372d 100644 --- a/natsapi/api.go +++ b/natsapi/api.go @@ -18,14 +18,14 @@ import ( var rClient = resty.New() -func getAPI(apihost string) (string, error) { - if apihost != "" { - return apihost, nil +func getAPI(apihost, natshost string) (string, string, error) { + if apihost != "" && natshost != "" { + return apihost, natshost, nil } f, err := os.Open(`/etc/nginx/sites-available/rmm.conf`) if err != nil { - return "", err + return "", "", err } defer f.Close() @@ -33,26 +33,25 @@ func getAPI(apihost string) (string, error) { for scanner.Scan() { if strings.Contains(scanner.Text(), "server_name") && !strings.Contains(scanner.Text(), "301") { r := strings.NewReplacer("server_name", "", ";", "") - return strings.ReplaceAll(r.Replace(scanner.Text()), " ", ""), nil + s := strings.ReplaceAll(r.Replace(scanner.Text()), " ", "") + return fmt.Sprintf("https://%s/natsapi", s), fmt.Sprintf("tls://%s:4222", s), nil } } - return "", errors.New("unable to parse api from nginx conf") + return "", "", errors.New("unable to parse api from nginx conf") } -func Listen(apihost string, debug bool) { - var baseURL string - api, err := getAPI(apihost) +func Listen(apihost, natshost string, debug bool) { + api, natsurl, err := getAPI(apihost, natshost) if err != nil { log.Fatalln(err) } if debug { - baseURL = fmt.Sprintf("http://%s:8000/natsapi", api) - } else { - baseURL = fmt.Sprintf("https://%s/natsapi", api) + fmt.Println("Api base url: ", api) + fmt.Println("Nats connection url: ", natsurl) } - rClient.SetHostURL(baseURL) + rClient.SetHostURL(api) rClient.SetTimeout(30 * time.Second) natsinfo, err := rClient.R().SetResult(&NatsInfo{}).Get("/natsinfo/") if err != nil { @@ -69,8 +68,7 @@ func Listen(apihost string, debug bool) { nats.ReconnectBufSize(-1), } - server := fmt.Sprintf("tls://%s:4222", api) - nc, err := nats.Connect(server, opts...) + nc, err := nats.Connect(natsurl, opts...) if err != nil { log.Fatalln(err) } diff --git a/natsapi/bin/nats-api b/natsapi/bin/nats-api index 0eaafe6a..236995e8 100755 Binary files a/natsapi/bin/nats-api and b/natsapi/bin/nats-api differ