66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package uniss
|
||
|
||
import (
|
||
"dashboard/models"
|
||
)
|
||
|
||
type httpServerHandler struct {
|
||
httpC chan *models.UnisHttpRequest
|
||
// mu sync.RWMutex // 没有必要使用锁,应为完全可以保证在一个协程内访问map
|
||
mapFn map[string]HttpServerHandlerFunc
|
||
}
|
||
|
||
type HttpServerHandlerFunc func(*models.UnisHttpRequest, *models.UnisHttpResponse) error
|
||
|
||
func newhttpServerHandler() *httpServerHandler {
|
||
res := new(httpServerHandler)
|
||
|
||
res.httpC = make(chan *models.UnisHttpRequest)
|
||
res.mapFn = make(map[string]HttpServerHandlerFunc)
|
||
|
||
return res
|
||
}
|
||
|
||
func (u *httpServerHandler) httpHandle(msg *models.UnisHttpRequest) {
|
||
resP := msg.ResP
|
||
resC := msg.ResC
|
||
|
||
defer func() {
|
||
if resC != nil {
|
||
resC <- resP
|
||
}
|
||
}()
|
||
|
||
fn, ok := u.getHandle(msg.Id)
|
||
if ok {
|
||
if err := fn(msg, resP); err == nil {
|
||
}
|
||
}
|
||
}
|
||
|
||
func (u *httpServerHandler) pushHandle(key string, handle HttpServerHandlerFunc) {
|
||
// u.mu.Lock()
|
||
// defer u.mu.Unlock()
|
||
|
||
u.mapFn[key] = handle
|
||
}
|
||
|
||
func (u *httpServerHandler) delHandle(key string) {
|
||
// u.mu.Lock()
|
||
// defer u.mu.Unlock()
|
||
|
||
delete(u.mapFn, key)
|
||
}
|
||
|
||
func (u *httpServerHandler) getHandle(key string) (HttpServerHandlerFunc, bool) {
|
||
// u.mu.RLock()
|
||
// defer u.mu.RUnlock()
|
||
|
||
res, ok := u.mapFn[key]
|
||
if !ok {
|
||
return nil, false
|
||
}
|
||
|
||
return res, ok
|
||
}
|