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 package main
// env CGO_ENABLED=0 go build -v -a -tags netgo -installsuffix netgo -ldflags "-s -w" -o nats-api
import ( import (
"flag" "flag"
"fmt" "fmt"
@ -11,7 +13,8 @@ var version = "1.0.0"
func main() { func main() {
ver := flag.Bool("version", false, "Prints version") 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") debug := flag.Bool("debug", false, "Debug")
flag.Parse() flag.Parse()
@ -20,5 +23,5 @@ func main() {
return return
} }
api.Listen(*apiHost, *debug) api.Listen(*apiHost, *natsHost, *debug)
} }

View File

@ -18,14 +18,14 @@ import (
var rClient = resty.New() var rClient = resty.New()
func getAPI(apihost string) (string, error) { func getAPI(apihost, natshost string) (string, string, error) {
if apihost != "" { if apihost != "" && natshost != "" {
return apihost, nil return apihost, natshost, 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
} }
defer f.Close() defer f.Close()
@ -33,26 +33,25 @@ func getAPI(apihost string) (string, error) {
for scanner.Scan() { for scanner.Scan() {
if strings.Contains(scanner.Text(), "server_name") && !strings.Contains(scanner.Text(), "301") { if strings.Contains(scanner.Text(), "server_name") && !strings.Contains(scanner.Text(), "301") {
r := strings.NewReplacer("server_name", "", ";", "") 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) { func Listen(apihost, natshost string, debug bool) {
var baseURL string api, natsurl, err := getAPI(apihost, natshost)
api, err := getAPI(apihost)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
if debug { if debug {
baseURL = fmt.Sprintf("http://%s:8000/natsapi", api) fmt.Println("Api base url: ", api)
} else { fmt.Println("Nats connection url: ", natsurl)
baseURL = fmt.Sprintf("https://%s/natsapi", api)
} }
rClient.SetHostURL(baseURL) rClient.SetHostURL(api)
rClient.SetTimeout(30 * time.Second) 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 {
@ -69,8 +68,7 @@ func Listen(apihost string, debug bool) {
nats.ReconnectBufSize(-1), nats.ReconnectBufSize(-1),
} }
server := fmt.Sprintf("tls://%s:4222", api) nc, err := nats.Connect(natsurl, opts...)
nc, err := nats.Connect(server, opts...)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

Binary file not shown.