Files
OpenList/internal/message/http.go
Kuingsmile fdcc2f136e chore: change module name to OpenListTeam/OpenList (#2)
* Enable blank issue

* chore(README.md): update docs (temporally)

* Update FUNDING.yml

* chore: purge README.md

* chore: change module name to OpenListTeam/OpenList

* fix: fix link errors

* chore: remove v3 in module name

* fix: resolve some conficts

* fix: resolve conficts

* docs: update with latest file

---------

Co-authored-by: ShenLin <773933146@qq.com>
Co-authored-by: Hantong Chen <cxwdyx620@gmail.com>
Co-authored-by: joshua <i@joshua.su>
Co-authored-by: Hantong Chen <70561268+cxw620@users.noreply.github.com>
2025-06-12 22:02:46 +08:00

83 lines
1.6 KiB
Go

package message
import (
"time"
"github.com/OpenListTeam/OpenList/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
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 *Http) GetHandle(c *gin.Context) {
select {
case message := <-p.ToSend:
common.SuccessResp(c, message)
default:
common.ErrorStrResp(c, "no message", 404)
}
}
func (p *Http) SendHandle(c *gin.Context) {
var req Req
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
select {
case p.Received <- req.Message:
common.SuccessResp(c)
default:
common.ErrorStrResp(c, "nowhere needed", 500)
}
}
func (p *Http) Send(message Message) error {
select {
case p.ToSend <- message:
return nil
default:
return errors.New("send failed")
}
}
func (p *Http) Receive() (string, error) {
select {
case message := <-p.Received:
return message, nil
default:
return "", errors.New("receive failed")
}
}
func (p *Http) WaitSend(message Message, d int) error {
select {
case p.ToSend <- message:
return nil
case <-time.After(time.Duration(d) * time.Second):
return errors.New("send timeout")
}
}
func (p *Http) WaitReceive(d int) (string, error) {
select {
case message := <-p.Received:
return message, nil
case <-time.After(time.Duration(d) * time.Second):
return "", errors.New("receive timeout")
}
}
var HttpInstance = &Http{
Received: make(chan string),
ToSend: make(chan Message),
}