Compare commits

...

3 Commits

Author SHA1 Message Date
eson
f1729f129a feat(logger): delete Logger() 2020-11-26 17:36:57 +08:00
eson
f61302d25f style(ginmode): set gin mode release 2020-11-26 17:26:19 +08:00
eson
b9d931da68 feat(context): add Unmarshal json 2020-11-25 17:43:33 +08:00
3 changed files with 49 additions and 16 deletions

23
callback.go Normal file
View File

@ -0,0 +1,23 @@
package cwclient
import "encoding/json"
// Callback 发送代理连接获取内容后的回调函数
type Callback struct {
label string
hash string
Do func(cxt *CallbackContext)
}
// CallbackContext Callback上下文
type CallbackContext struct {
TaskID string
Content string
Error error
Carry interface{} // 传递的参数.
}
// ContentJSON 返回 json反序列化的对象
func (cxt *CallbackContext) ContentJSON(obj interface{}) error {
return json.Unmarshal([]byte(cxt.Content), obj)
}

View File

@ -21,21 +21,6 @@ func init() {
log.SetFlags(log.Llongfile | log.LstdFlags)
}
// CallbackContext Callback上下文
type CallbackContext struct {
TaskID string
Content string
Error error
Carry interface{} // 传递的参数.
}
// Callback 发送代理连接获取内容后的回调函数
type Callback struct {
label string
hash string
Do func(cxt *CallbackContext)
}
// Client 客户端
type Client struct {
chromeProxyAddr string
@ -183,7 +168,10 @@ func (cli *Client) Connect() {
}
cli.port = fmt.Sprintf("%d", listener.Addr().(*net.TCPAddr).Port)
router := gin.Default()
router := gin.New()
router.Use(gin.Recovery())
gin.SetMode(gin.ReleaseMode)
router.POST("/:label", func(c *gin.Context) {
label := c.Param("label")
if f, ok := cli.register.Load(label); ok {

View File

@ -5,8 +5,30 @@ import (
"log"
"net/http"
"testing"
"time"
)
func TestSelectChan(t *testing.T) {
var a chan string = make(chan string)
var b chan string = make(chan string)
go func() {
time.Sleep(time.Second * 2)
a <- "1"
time.Sleep(time.Second * 2)
b <- "2"
}()
select {
case s := <-a:
log.Println("a", s)
case s := <-b:
log.Println("b", s)
case <-time.After(time.Second * 5):
log.Println("timeout 5s")
}
}
func TestPort(t *testing.T) {
cli := New("http://localhost:7123")