diff --git a/internal/bootstrap/data/dev.go b/internal/bootstrap/data/dev.go index efdfc6af..cdb9afd5 100644 --- a/internal/bootstrap/data/dev.go +++ b/internal/bootstrap/data/dev.go @@ -37,9 +37,9 @@ func initDevData() { func initDevDo() { if flags.Dev { go func() { - err := message.GetMessenger().WaitSend(map[string]string{ - "type": "dev", - "msg": "dev mode", + err := message.GetMessenger().WaitSend(message.Message{ + Type: "string", + Content: "dev mode", }, 10) if err != nil { log.Debugf("%+v", err) diff --git a/internal/message/post.go b/internal/message/http.go similarity index 64% rename from internal/message/post.go rename to internal/message/http.go index 55030f41..3f023dec 100644 --- a/internal/message/post.go +++ b/internal/message/http.go @@ -8,16 +8,16 @@ import ( "github.com/pkg/errors" ) -type Post struct { - Received chan string // received messages from web - ToSend chan interface{} // messages to send to web +type Http struct { + Received chan string // received messages from web + ToSend chan Message // messages to send to web } type Req struct { Message string `json:"message" form:"message"` } -func (p *Post) GetHandle(c *gin.Context) { +func (p *Http) GetHandle(c *gin.Context) { select { case message := <-p.ToSend: common.SuccessResp(c, message) @@ -26,7 +26,7 @@ func (p *Post) GetHandle(c *gin.Context) { } } -func (p *Post) SendHandle(c *gin.Context) { +func (p *Http) SendHandle(c *gin.Context) { var req Req if err := c.ShouldBind(&req); err != nil { common.ErrorResp(c, err, 400) @@ -36,20 +36,20 @@ func (p *Post) SendHandle(c *gin.Context) { case p.Received <- req.Message: common.SuccessResp(c) default: - common.ErrorStrResp(c, "send failed", 500) + common.ErrorStrResp(c, "nowhere needed", 500) } } -func (p *Post) Send(data interface{}) error { +func (p *Http) Send(message Message) error { select { - case p.ToSend <- data: + case p.ToSend <- message: return nil default: return errors.New("send failed") } } -func (p *Post) Receive() (string, error) { +func (p *Http) Receive() (string, error) { select { case message := <-p.Received: return message, nil @@ -58,16 +58,16 @@ func (p *Post) Receive() (string, error) { } } -func (p *Post) WaitSend(data interface{}, d int) error { +func (p *Http) WaitSend(message Message, d int) error { select { - case p.ToSend <- data: + case p.ToSend <- message: return nil case <-time.After(time.Duration(d) * time.Second): return errors.New("send timeout") } } -func (p *Post) WaitReceive(d int) (string, error) { +func (p *Http) WaitReceive(d int) (string, error) { select { case message := <-p.Received: return message, nil @@ -76,7 +76,7 @@ func (p *Post) WaitReceive(d int) (string, error) { } } -var PostInstance = &Post{ +var HttpInstance = &Http{ Received: make(chan string), - ToSend: make(chan interface{}), + ToSend: make(chan Message), } diff --git a/internal/message/message.go b/internal/message/message.go index baf753ff..0ca0f2ae 100644 --- a/internal/message/message.go +++ b/internal/message/message.go @@ -1,12 +1,17 @@ package message +type Message struct { + Type string `json:"type"` + Content interface{} `json:"content"` +} + type Messenger interface { - Send(interface{}) error + Send(Message) error Receive() (string, error) - WaitSend(interface{}, int) error + WaitSend(Message, int) error WaitReceive(int) (string, error) } func GetMessenger() Messenger { - return PostInstance + return HttpInstance } diff --git a/server/router.go b/server/router.go index 01c3ecbf..2cc7b3ee 100644 --- a/server/router.go +++ b/server/router.go @@ -101,8 +101,8 @@ func admin(g *gin.RouterGroup) { task.POST("/copy/clear_done", handles.ClearDoneCopyTasks) ms := g.Group("/message") - ms.GET("/get", message.PostInstance.GetHandle) - ms.POST("/send", message.PostInstance.SendHandle) + ms.POST("/get", message.HttpInstance.GetHandle) + ms.POST("/send", message.HttpInstance.SendHandle) } func fs(g *gin.RouterGroup) {