update natsapi flags

This commit is contained in:
wh1te909 2021-01-16 00:01:31 +00:00
parent 5ef6a14d24
commit d810ce301f
3 changed files with 18 additions and 17 deletions

View File

@ -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)
}

View File

@ -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)
}

Binary file not shown.