69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package uniss
|
|
|
|
import (
|
|
"dashboard/models"
|
|
"sync"
|
|
)
|
|
|
|
type httpServerHandler struct {
|
|
httpC chan *models.UnisHttpRequest
|
|
mu sync.RWMutex
|
|
mapFn map[string]HttpServerHandlerFunc
|
|
pool sync.Pool
|
|
}
|
|
|
|
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)
|
|
res.pool.New = func() any { return new(models.UnisHttpResponse) }
|
|
|
|
return res
|
|
}
|
|
|
|
func (u *httpServerHandler) httpHandle(msg *models.UnisHttpRequest) {
|
|
res := u.pool.Get().(*models.UnisHttpResponse)
|
|
resC := msg.ResC
|
|
|
|
defer func() {
|
|
if resC != nil {
|
|
resC <- res
|
|
}
|
|
}()
|
|
|
|
fn, ok := u.getHandle(msg.Id)
|
|
if ok {
|
|
if err := fn(msg, res); 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
|
|
}
|