[feat] 命名优化

This commit is contained in:
what 2023-06-05 16:52:03 +08:00
parent 73c755524a
commit b2a2d70f66
5 changed files with 100 additions and 84 deletions

View File

@ -4,7 +4,6 @@ import (
"net/http"
"github.com/samber/do"
"google.golang.org/grpc"
)
type Controller interface {
@ -13,18 +12,6 @@ type Controller interface {
Dispatch(job string, payload any, u User) error
}
type GRpController interface {
Controller
// 获取 gRPC 服务的描述信息
GetGRpcServiceDesc() *grpc.ServiceDesc
}
type JobController interface {
Controller
// 任务处理
Handle(any) error
}
type HttpController interface {
Controller
// 获取用户信息
@ -37,6 +24,8 @@ type HttpController interface {
Route() Route
// 内部调用
Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error)
// 内部调用
// Invoke()
}
type WsController interface {
@ -55,14 +44,6 @@ type BaseController struct {
container *do.Injector
}
type GRpcBaseController struct {
Controller
}
type JobBaseController struct {
Controller
}
type HttpBaseController struct {
Controller
request *http.Request
@ -81,7 +62,7 @@ func (this BaseController) Container() *do.Injector {
}
func (this BaseController) Dispatch(job string, payload any, u User) error {
return do.MustInvoke[Job](this.Container()).Dispatch(job, payload, u)
return do.MustInvoke[JobService](this.Container()).Dispatch(job, payload, u)
}
func (this HttpBaseController) Request() *http.Request {
@ -121,14 +102,6 @@ func (WsBaseController) WsClientGroup(GlobalParams) WsClientGroup {
return defaultWsClientGroup
}
func (JobBaseController) Handle(any) error {
return nil
}
func (GRpcBaseController) GetGRpcServiceDesc() *grpc.ServiceDesc {
return nil
}
func NewBaseController(container *do.Injector) Controller {
return &BaseController{container}
}
@ -146,15 +119,3 @@ func NewWsBaseController(ws WsClient, ctr HttpController) WsController {
ws: ws,
}
}
func NewGRpBaseController(container *do.Injector) JobController {
return &JobBaseController{
Controller: &BaseController{container},
}
}
func NewJobBaseController(container *do.Injector) GRpController {
return &GRpcBaseController{
Controller: &BaseController{container},
}
}

55
grpc.go
View File

@ -1,51 +1,32 @@
package contracts
import (
"encoding/json"
"io"
"github.com/golang/protobuf/jsonpb"
"github.com/jhump/protoreflect/desc"
"github.com/samber/do"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type GRpc interface {
type GRpcServcer interface {
Start() error
Stop() error
Restart() error
}
type GRpcResponse interface {
// 获取响应头
Header() metadata.MD
// 发送 stream 数据
Send(data string) error
// 接收 unary / stream 数据
Recv() io.Reader
// 接收 unary / stream 数据, 并转 json 对象
RecvDecoder() *json.Decoder
// 关闭 stream 数据
Close() error
type GRpc interface {
Controller
// 获取 gRPC 服务的描述信息
GetGRpcServiceDesc() *grpc.ServiceDesc
}
type GRpcall interface {
// 接口请求
Invoke(service, method, data string, headers []string) (GRpcResponse, error)
// 获取客户端连接
GetClientConn() *grpc.ClientConn
// 获取服务端, 服务列表
GetServices() ([]string, error)
// 获取服务端, 服务下的方法列表
GetServiceMethods(name string) ([]string, error)
// 获取服务所有类型
GetAnyResolver() (jsonpb.AnyResolver, error)
// 获取 proto 文件描述
GetAllFilesDescriptor() ([]*desc.FileDescriptor, error)
// 获取服务描述
GetServiceDescriptor(service string) (*desc.ServiceDescriptor, error)
// 获取服务方法描述
GetServiceMethodDescriptor(service, method string) (*desc.MethodDescriptor, error)
// 关闭客户端连接
Close() error
type GRpcBase struct {
Controller
}
func (GRpcBase) GetGRpcServiceDesc() *grpc.ServiceDesc {
return nil
}
func NewJGRpcBase(container *do.Injector) GRpc {
return &GRpcBase{
Controller: &BaseController{container},
}
}

45
grpcall.go Normal file
View File

@ -0,0 +1,45 @@
package contracts
import (
"encoding/json"
"io"
"github.com/golang/protobuf/jsonpb"
"github.com/jhump/protoreflect/desc"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type GRpcall interface {
// 接口请求
Invoke(service, method, data string, headers []string) (GRpcResponse, error)
// 获取客户端连接
GetClientConn() *grpc.ClientConn
// 获取服务端, 服务列表
GetServices() ([]string, error)
// 获取服务端, 服务下的方法列表
GetServiceMethods(name string) ([]string, error)
// 获取服务所有类型
GetAnyResolver() (jsonpb.AnyResolver, error)
// 获取 proto 文件描述
GetAllFilesDescriptor() ([]*desc.FileDescriptor, error)
// 获取服务描述
GetServiceDescriptor(service string) (*desc.ServiceDescriptor, error)
// 获取服务方法描述
GetServiceMethodDescriptor(service, method string) (*desc.MethodDescriptor, error)
// 关闭客户端连接
Close() error
}
type GRpcResponse interface {
// 获取响应头
Header() metadata.MD
// 发送 stream 数据
Send(data string) error
// 接收 unary / stream 数据
Recv() io.Reader
// 接收 unary / stream 数据, 并转 json 对象
RecvDecoder() *json.Decoder
// 关闭 stream 数据
Close() error
}

31
job.go
View File

@ -1,8 +1,37 @@
package contracts
type Job interface {
import (
"fmt"
"github.com/samber/do"
)
type JobServer interface {
Start() error
Stop() error
Restart() error
}
type JobService interface {
Dispatch(job string, payload any, u User) error
}
type Job interface {
Controller
// 任务处理
Handle(any) error
}
type JobBase struct {
Controller
}
func (JobBase) Handle(any) error {
return fmt.Errorf("")
}
func NewJobBase(container *do.Injector) Job {
return &JobBase{
Controller: &BaseController{container},
}
}

View File

@ -4,10 +4,10 @@ type Service interface {
LoadGoPlugin(file string) error
RegControllers(items map[string]Controller) error
RegListens(items map[string]ResListener) error
RegGRpControllers(items map[string]GRpController) error
RegJobControllers(items map[string]JobController) error
RegGRpcs(items map[string]GRpc) error
RegJobs(items map[string]Job) error
GetResListener(code string) (ResListener, bool)
GetAppController(code string) (Controller, bool)
GetGRpController(code string) (GRpController, bool)
GetJobController(code string) (JobController, bool)
GetAppGRpc(code string) (GRpc, bool)
GetAppJob(code string) (Job, bool)
}