dashboard/models/unishttpparams.go
2025-06-04 17:07:17 +08:00

89 lines
1.7 KiB
Go

package models
import (
"fmt"
"strings"
)
// http server
type UnisHttpRequest struct {
ResC chan *UnisHttpResponse
ResP *UnisHttpResponse
Id string
Msg interface{}
}
func (u *UnisHttpRequest) SetReqParam(id string, msg interface{}) {
u.Id = id
u.Msg = msg
}
type UnisHttpResponse struct {
Code int
Msg interface{}
Data interface{}
}
func (u *UnisHttpResponse) SetResParam(code int, msg, data interface{}) {
u.Code = code
u.Msg = msg
u.Data = data
}
type UnisHttpUrl string
const UnisHttpUrlPrefix = "/api/unis"
func (u UnisHttpUrl) URL() string {
return strings.TrimPrefix(string(u), UnisHttpUrlPrefix)
}
const (
UNIS_HTTP_URL_CONFIG_ADD UnisHttpUrl = "/api/unis/config/v1/add"
)
func (u UnisHttpUrl) GetMsgId() string {
return mapHttpUrlId[u]
}
type UnisHttpMsgId string
const (
UNIS_HTTP_ID_CONFIG_ADD UnisHttpMsgId = "/api/unis/config/v1/add"
)
var mapHttpUrlId = map[UnisHttpUrl]string{
UNIS_HTTP_URL_CONFIG_ADD: string(UNIS_HTTP_ID_CONFIG_ADD),
}
var UnisHttpResponseOk = &UnisHttpResponse{Code: int(CodeSuccess), Msg: CodeSuccess.String()}
// http client
type UnisHttpClientHandlerFunc func(*UnisHttpClientRequest) error
type UnisHttpClientRequest struct {
Url string
Methord string
Msg interface{}
Sn uint64
Handle UnisHttpClientHandlerFunc
}
func (u *UnisHttpClientRequest) SetResParam(methord string, url string, body interface{}, sn uint64, handle UnisHttpClientHandlerFunc) {
u.Methord = methord
u.Url = url
u.Msg = body
u.Handle = handle
u.Sn = sn
}
type UnisHttpClientUrl string
func (u UnisHttpClientUrl) URI(host string, port int) string {
return fmt.Sprintf("http://%s:%d%s", host, port, string(u))
}
const (
UNIS_HTTP_CLIENT_URL_SOURCE_LIST UnisHttpClientUrl = "/api/unis/source-list/v1/update"
)