Compare commits

...

5 Commits
master ... nats

Author SHA1 Message Date
Paulo Simão 6aae4a5cdb nats migration 2021-11-14 10:31:07 -03:00
Paulo Simão 429580c010 wip 2021-11-13 21:35:07 +00:00
Paulo Simão c79c476473 fronted + nats 2021-11-13 16:30:02 -03:00
Paulo Simão 581b4def8a nats migration 2021-11-11 11:07:20 -03:00
Paulo Simão 5b590ab31b start nats migration 2021-11-11 05:56:07 -03:00
11 changed files with 476 additions and 26 deletions

View File

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

View File

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

View File

@ -0,0 +1,29 @@
package frontend
import "go.digitalcircle.com.br/open/replaycli-go/api"
type Cli struct {
}
type HttpRes struct {
Headers map[string]string
Body []byte
}
type HttpReq struct {
Url string
Headers map[string]string
Body []byte
}
func (c *Cli) Get(req *HttpReq, copts ...*api.CallOpts) (*HttpRes, error) {
res := &HttpRes{}
//err := c.HttpCli().JsonPost("/ipc/ocr/", opts, res)
err := api.Call("frontend.handle", req, &res, copts...)
return res, err
}
func NewCli() *Cli {
ret := &Cli{}
return ret
}

12
api/http/lib.go 100644
View File

@ -0,0 +1,12 @@
package http
type HttpRes struct {
Headers map[string]string
Body []byte
}
type HttpReq struct {
Url string
Headers map[string]string
Body []byte
}

145
api/nats.go 100644
View File

@ -0,0 +1,145 @@
package api
import (
"encoding/json"
"errors"
"fmt"
"github.com/nats-io/nats.go"
"log"
"os"
"time"
)
var nc *nats.Conn
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{}
if opts != nil && len(opts) > 1 {
opt = opts[0]
}
if opt.To == 0 {
opt.To = 60
}
defer func() {
if nc != nil && err != nil {
err1 := nc.Publish("err", []byte(fmt.Sprintf("Error calling nats: %s: \nIN: %v\nOUT: %v", s, err.Error(), in, out)))
if err1 != nil {
log.Printf("Error calling nats: %s: \nIN: %v\nOUT: %v", s, err.Error(), in, out)
log.Printf("Inner error: %s", err1.Error())
}
} else if nc == nil && err != nil {
log.Printf("Error calling nats: %s: \nIN: %v\nOUT: %v\n(nc is nil)", s, err.Error(), in, out)
}
}()
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 err
}
}
var bs []byte
if in != nil {
bs, err = json.Marshal(in)
if err != nil {
return err
}
} else {
bs = []byte("{}")
}
msg, err := nc.Request(s, bs, time.Second+time.Duration(opt.To))
if err != nil {
return err
}
if out != nil {
err = json.Unmarshal(msg.Data, out)
if err != nil {
out = bs
return err
}
}
errstr := msg.Header.Get("ERR")
if errstr != "" {
return errors.New(errstr)
}
return nil
}
func Close() error {
return nc.Drain()
}
func Pub(s string, in interface{}) error {
var err error
defer func() {
if nc != nil && err != nil {
err1 := nc.Publish("err", []byte(fmt.Sprintf("Error calling nats: %s: \nIN: %v", s, err.Error(), in)))
if err1 != nil {
log.Printf("Error calling nats: %s: \nIN: %v", s, err.Error(), in)
log.Printf("Inner error: %s", err1.Error())
}
} else if nc == nil && err != nil {
log.Printf("Error calling nats: %s: \nIN: %v\n(nc is nil)", s, err.Error(), in)
}
}()
bs, err := json.Marshal(in)
if err != nil {
return err
}
err = nc.Publish(s, bs)
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)
}

View File

@ -169,16 +169,16 @@ type Opts struct {
}
type Cli struct {
*api.ApiCli
//*api.ApiCli
}
func (c *Cli) OCR(opts *Opts) (*Alto, error) {
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
}

View File

@ -16,12 +16,14 @@ type Cli struct {
func (c *Cli) ClipRead() (string, error) {
ret := ""
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/clip/read"), &ret)
err := api.Call("wingui.clip.read", nil, &ret)
//err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/clip/read"), &ret)
return ret, err
}
func (c *Cli) ClipWrite(site string) error {
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/clip/write?str=%s", site), nil)
func (c *Cli) ClipWrite(in string) error {
var out interface{}
err := api.Call("wingui.clip.read", in, &out)
//err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/clip/write?str=%s", site), nil)
return err
}

11
go.mod
View File

@ -9,6 +9,17 @@ require (
github.com/Microsoft/go-winio v0.5.1
github.com/gorilla/websocket v1.4.2
github.com/mitchellh/go-ps v1.0.0
github.com/nats-io/nats.go v1.13.1-0.20211018182449-f2416a8b1483
go.digitalcircle.com.br/open/httpcli v0.0.0-20211031093505-ecf33aed8afb
golang.org/x/sys v0.0.0-20211031064116-611d5d643895 // indirect
)
require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/nats-io/nats-server/v2 v2.6.4 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/mod v0.5.1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)

59
go.sum
View File

@ -1,23 +1,70 @@
github.com/Microsoft/go-winio v0.5.0 h1:Elr9Wn+sGKPlkaBvwu4mTrxtmOp3F3yV9qhaHbXGjwU=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0=
github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/nats-io/jwt/v2 v2.1.0 h1:1UbfD5g1xTdWmSeRV8bh/7u+utTiBsRtWhLl1PixZp4=
github.com/nats-io/jwt/v2 v2.1.0/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k=
github.com/nats-io/nats-server/v2 v2.6.4 h1:WjR1ylV/5Urth88K8U78wEEnWFYEJ9DNM0Q5DTlTx0g=
github.com/nats-io/nats-server/v2 v2.6.4/go.mod h1:LlMieumxNUnCloOTVFv7Wog0YnasScxARUMXVXv9/+M=
github.com/nats-io/nats.go v1.13.1-0.20211018182449-f2416a8b1483 h1:GMx3ZOcMEVM5qnUItQ4eJyQ6ycwmIEB/VC/UxvdevE0=
github.com/nats-io/nats.go v1.13.1-0.20211018182449-f2416a8b1483/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
go.digitalcircle.com.br/open/httpcli v0.0.0-20211010235458-74d201454158 h1:vFm0G1b4hBDJ7y/s2NqRuX99lwuI8/sxxH3XLUOdi0o=
go.digitalcircle.com.br/open/httpcli v0.0.0-20211010235458-74d201454158/go.mod h1:e/6vw7F4vgfs+ThQoLeBbYCYLTH1b5thzFwkFixi+oA=
go.digitalcircle.com.br/open/httpcli v0.0.0-20211031093505-ecf33aed8afb h1:gv0FO26InL2ctmhCqthsOFEooi0FvA1rFT6VXzgveJE=
go.digitalcircle.com.br/open/httpcli v0.0.0-20211031093505-ecf33aed8afb/go.mod h1:e/6vw7F4vgfs+ThQoLeBbYCYLTH1b5thzFwkFixi+oA=
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211031064116-611d5d643895 h1:iaNpwpnrgL5jzWS0vCNnfa8HqzxveCFpFx3uC/X4Tps=
golang.org/x/sys v0.0.0-20211031064116-611d5d643895/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

154
svc/lib.go 100644
View File

@ -0,0 +1,154 @@
package svc
import (
"encoding/json"
"errors"
"fmt"
"github.com/nats-io/nats.go"
"log"
"os"
"reflect"
"strings"
"time"
"unicode"
)
var nc *nats.Conn
var compname string
func Init(s string) error {
var err error
compname = s
var apikey string
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, nats.Name(compname))
} else {
log.Printf("Connecting to NATS Server w TK: " + apikey)
nc, err = nats.Connect(nats.DefaultURL, nats.Name(compname), nats.Token(apikey))
}
if err != nil {
return err
}
}
return err
}
func Close() error {
err := nc.Drain()
chEnd <- struct{}{}
return err
}
var chEnd chan struct{}
func Serve() {
chEnd = make(chan struct{})
<-chEnd
}
func NormalizeName(s string) string {
sb := strings.Builder{}
for i, v := range s {
if unicode.IsUpper(v) && i != 0 {
sb.WriteString(".")
}
sb.WriteRune(unicode.ToLower(v))
}
return sb.String()
}
func Register(h interface{}) {
var err error
tp := reflect.TypeOf(h)
for i := 0; i < tp.NumMethod(); i++ {
mtd := tp.Method(i)
mtdt := mtd.Type
intp := mtdt.In(1)
isPtr := false
if intp.Kind() == reflect.Ptr {
isPtr = true
intp = intp.Elem()
}
mn := NormalizeName(compname) + "." + NormalizeName(mtd.Name)
err = nc.Publish("sub", []byte(mn))
if err != nil {
log.Printf(err.Error())
}
_, err = nc.Subscribe(mn, func(msg *nats.Msg) {
ni := reflect.New(intp).Interface()
var err error
if intp.Kind() == reflect.Ptr {
err = json.Unmarshal(msg.Data, ni)
} else {
err = json.Unmarshal(msg.Data, &ni)
}
if err != nil {
log.Printf(err.Error())
}
var rawret []reflect.Value
if isPtr {
rawret = mtd.Func.Call([]reflect.Value{reflect.ValueOf(h), reflect.ValueOf(ni)})
} else {
rawret = mtd.Func.Call([]reflect.Value{reflect.ValueOf(h), reflect.ValueOf(ni).Elem()})
}
ret := rawret[0].Interface()
err1 := rawret[1].Interface()
//if err1 != nil {
// log.Printf(err1.(error).Error())
//}
var bs []byte
rmsg := &nats.Msg{}
rmsg.Data = bs
bs, err = json.Marshal(ret)
if err1 != nil {
rmsg.Header = nats.Header{}
rmsg.Header.Set("ERR", fmt.Sprintf("%#v", err1))
}
if err != nil {
log.Printf(err.Error())
}
rmsg.Data = bs
err = msg.RespondMsg(rmsg)
if err != nil {
log.Printf(err.Error())
}
})
if err != nil {
log.Printf(err.Error())
}
if err != nil {
nc.Publish("err", []byte(err.Error()))
}
}
}
func Nc() *nats.Conn {
return nc
}
func Call(s string, in interface{}, out interface{}) error {
bs, err := json.Marshal(in)
if err != nil {
return err
}
msg, err := nc.Request(s, bs, time.Minute)
if err != nil {
return err
}
err = json.Unmarshal(msg.Data, out)
if err != nil {
return err
}
errstr := msg.Header.Get("ERR")
if errstr != "" {
return errors.New(errstr)
}
return nil
}

30
ver/lib.go 100644
View File

@ -0,0 +1,30 @@
package ver
import (
"golang.org/x/mod/semver"
"log"
"os"
)
var Ver string
func EnsureVer(v string) {
build := os.Getenv("BUILD_VER")
if build == "" {
log.Printf("No BUILD_VER found - skipping verification")
return
}
if semver.Compare(build, v) < 0 {
log.Printf("Replay version is %s, lower than required. Should be minimum %s - aborting", build, v)
os.Exit(0)
}
if semver.Major(build) != semver.Major(v) {
log.Printf("Replay major version is %s, different from feature requirement: %s - aborting", build, v)
}
}
func Get() string {
if Ver == "" {
Ver = os.Getenv("BUILD_VER")
}
return Ver
}