From 6aae4a5cdb7a4b3283c9382584f6a27f9ed512c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Sun, 14 Nov 2021 10:31:07 -0300 Subject: [PATCH] nats migration --- api/anticaptcha/cli.go | 19 +++++++------------ api/chrome/cli.go | 25 +++++++++++++++++++++++++ api/nats.go | 35 +++++++++++++++++++++++++++++++++++ api/ocr/cli.go | 5 ++--- ver/lib.go | 8 ++++++-- 5 files changed, 75 insertions(+), 17 deletions(-) diff --git a/api/anticaptcha/cli.go b/api/anticaptcha/cli.go index f7649e0..8e5bf21 100644 --- a/api/anticaptcha/cli.go +++ b/api/anticaptcha/cli.go @@ -5,15 +5,8 @@ import ( ) type Cli struct { - *api.ApiCli - //cli *api.Cli } -//func (c *Cli) HttpCli() *api.Cli { -// -// return c.HttpCli() -//} - type Req struct { Site string `json:"site"` Data string `json:"data"` @@ -21,7 +14,7 @@ type Req struct { To int `json:"to"` } -func (c *Cli) Recaptchav2(site string, data string) (string, error) { +func (c *Cli) Recaptchav2(site string, data string, copts ...*api.CallOpts) (string, error) { ret := "" req := &Req{ Site: site, @@ -29,22 +22,24 @@ func (c *Cli) Recaptchav2(site string, data string) (string, error) { Img: nil, To: 300, } - err := c.HttpCli().JsonPost("/ipc/anticaptcha/recaptchav2", req, &ret) + err := api.Call("anticaptcha.recaptchav2", req, &ret, copts...) return ret, err } -func (c *Cli) Image2text(site string, data []byte) (string, error) { +func (c *Cli) Image2text(site string, data []byte, copts ...*api.CallOpts) (string, error) { ret := "" req := &Req{ Site: site, Img: data, To: 300, } - err := c.HttpCli().JsonPost("/ipc/anticaptcha/image2text", req, &ret) + + err := api.Call("anticaptcha.image2text", req, &ret, copts...) return ret, err + } func NewCli() *Cli { - ret := &Cli{ApiCli: api.NewApiCli()} + ret := &Cli{} return ret } diff --git a/api/chrome/cli.go b/api/chrome/cli.go index b557026..c5313a9 100644 --- a/api/chrome/cli.go +++ b/api/chrome/cli.go @@ -6,6 +6,31 @@ import ( "go.digitalcircle.com.br/open/replaycli-go/api" ) +type Req struct { + To int + Url string + Id string + Eval string + Title string + Msg map[string]interface{} +} +type Res struct { + Id string + TabMetadata []TabMetadata + Ret map[string]interface{} + Raw []byte +} + +type TabMetadata struct { + Description string `json:"description"` + DevtoolsFrontendUrl string `json:"devtoolsFrontendUrl"` + Id string `json:"id"` + Title string `json:"title"` + Type string `json:"type"` + Url string `json:"url"` + WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"` +} + type Cli struct { *api.ApiCli } diff --git a/api/nats.go b/api/nats.go index 2a44301..0f348d5 100644 --- a/api/nats.go +++ b/api/nats.go @@ -16,6 +16,23 @@ type CallOpts struct { To int } +func NC() (nc *nats.Conn, err error) { + if nc == nil { + + apikey = os.Getenv("REPLAY_APIKEY") + if apikey == "" { + log.Printf("Connecting to NATS Server w/o TK.") + nc, err = nats.Connect(nats.DefaultURL) + } else { + log.Printf("Connecting to NATS Server w TK: " + apikey) + nc, err = nats.Connect(nats.DefaultURL, nats.Token(apikey)) + } + if err != nil { + return nil, err + } + } + return nc, nil +} func Call(s string, in interface{}, out interface{}, opts ...*CallOpts) error { var err error opt := &CallOpts{} @@ -108,3 +125,21 @@ func Pub(s string, in interface{}) error { return err } +func Init() error { + var err error + nc, err = NC() + if err != nil { + return err + } + nc.Subscribe("exit", func(msg *nats.Msg) { + log.Printf("Exit Required") + os.Exit(0) + }) + return nil +} +func Err(e error) { + Pub("err", e.Error()) +} +func Dbg(e string) { + Pub("dbg", e) +} diff --git a/api/ocr/cli.go b/api/ocr/cli.go index f87d4a6..4497918 100644 --- a/api/ocr/cli.go +++ b/api/ocr/cli.go @@ -169,17 +169,16 @@ type Opts struct { } type Cli struct { - *api.ApiCli + //*api.ApiCli } func (c *Cli) OCR(opts *Opts, copts ...*api.CallOpts) (*Alto, error) { res := &Alto{} - //err := c.HttpCli().JsonPost("/ipc/ocr/", opts, res) err := api.Call("ocr.ocr", opts, &res, copts...) return res, err } func NewCli() *Cli { - ret := &Cli{ApiCli: api.NewApiCli()} + ret := &Cli{} return ret } diff --git a/ver/lib.go b/ver/lib.go index d68059b..e22fe17 100644 --- a/ver/lib.go +++ b/ver/lib.go @@ -6,6 +6,8 @@ import ( "os" ) +var Ver string + func EnsureVer(v string) { build := os.Getenv("BUILD_VER") if build == "" { @@ -21,6 +23,8 @@ func EnsureVer(v string) { } } func Get() string { - build := os.Getenv("BUILD_VER") - return build + if Ver == "" { + Ver = os.Getenv("BUILD_VER") + } + return Ver }