40 lines
870 B
Go
40 lines
870 B
Go
package uniss
|
||
|
||
import (
|
||
"dashboard/models"
|
||
"dashboard/pkg/singlemap"
|
||
)
|
||
|
||
type httpServerHandler struct {
|
||
httpC chan *models.UnisHttpRequest
|
||
// mu sync.RWMutex // 没有必要使用锁,应为完全可以保证在一个协程内访问map
|
||
*singlemap.SingleMap[string,httpServerHandlerFunc]
|
||
}
|
||
|
||
type httpServerHandlerFunc func(*models.UnisHttpRequest, *models.UnisHttpResponse) error
|
||
|
||
func newhttpServerHandler() *httpServerHandler {
|
||
res := new(httpServerHandler)
|
||
|
||
res.httpC = make(chan *models.UnisHttpRequest)
|
||
res.SingleMap = singlemap.NewSingleMap[string,httpServerHandlerFunc]()
|
||
|
||
return res
|
||
}
|
||
|
||
func (u *httpServerHandler) httpHandle(msg *models.UnisHttpRequest) {
|
||
resP := msg.ResP
|
||
resC := msg.ResC
|
||
|
||
defer func() {
|
||
if resC != nil {
|
||
resC <- resP
|
||
}
|
||
}()
|
||
|
||
if fn, ok := u.Get(msg.Id); ok && fn != nil {
|
||
if err := fn(msg, resP); err == nil {
|
||
}
|
||
}
|
||
}
|