[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" "net/http"
"github.com/samber/do" "github.com/samber/do"
"google.golang.org/grpc"
) )
type Controller interface { type Controller interface {
@ -13,18 +12,6 @@ type Controller interface {
Dispatch(job string, payload any, u User) error 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 { type HttpController interface {
Controller Controller
// 获取用户信息 // 获取用户信息
@ -37,6 +24,8 @@ type HttpController interface {
Route() Route Route() Route
// 内部调用 // 内部调用
Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error) Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error)
// 内部调用
// Invoke()
} }
type WsController interface { type WsController interface {
@ -55,14 +44,6 @@ type BaseController struct {
container *do.Injector container *do.Injector
} }
type GRpcBaseController struct {
Controller
}
type JobBaseController struct {
Controller
}
type HttpBaseController struct { type HttpBaseController struct {
Controller Controller
request *http.Request request *http.Request
@ -81,7 +62,7 @@ func (this BaseController) Container() *do.Injector {
} }
func (this BaseController) Dispatch(job string, payload any, u User) error { 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 { func (this HttpBaseController) Request() *http.Request {
@ -121,14 +102,6 @@ func (WsBaseController) WsClientGroup(GlobalParams) WsClientGroup {
return defaultWsClientGroup return defaultWsClientGroup
} }
func (JobBaseController) Handle(any) error {
return nil
}
func (GRpcBaseController) GetGRpcServiceDesc() *grpc.ServiceDesc {
return nil
}
func NewBaseController(container *do.Injector) Controller { func NewBaseController(container *do.Injector) Controller {
return &BaseController{container} return &BaseController{container}
} }
@ -146,15 +119,3 @@ func NewWsBaseController(ws WsClient, ctr HttpController) WsController {
ws: ws, 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 package contracts
import ( import (
"encoding/json" "github.com/samber/do"
"io"
"github.com/golang/protobuf/jsonpb"
"github.com/jhump/protoreflect/desc"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/metadata"
) )
type GRpc interface { type GRpcServcer interface {
Start() error Start() error
Stop() error Stop() error
Restart() error Restart() error
} }
type GRpcResponse interface { type GRpc interface {
// 获取响应头 Controller
Header() metadata.MD // 获取 gRPC 服务的描述信息
// 发送 stream 数据 GetGRpcServiceDesc() *grpc.ServiceDesc
Send(data string) error
// 接收 unary / stream 数据
Recv() io.Reader
// 接收 unary / stream 数据, 并转 json 对象
RecvDecoder() *json.Decoder
// 关闭 stream 数据
Close() error
} }
type GRpcall interface { type GRpcBase struct {
// 接口请求 Controller
Invoke(service, method, data string, headers []string) (GRpcResponse, error) }
// 获取客户端连接
GetClientConn() *grpc.ClientConn func (GRpcBase) GetGRpcServiceDesc() *grpc.ServiceDesc {
// 获取服务端, 服务列表 return nil
GetServices() ([]string, error) }
// 获取服务端, 服务下的方法列表
GetServiceMethods(name string) ([]string, error) func NewJGRpcBase(container *do.Injector) GRpc {
// 获取服务所有类型 return &GRpcBase{
GetAnyResolver() (jsonpb.AnyResolver, error) Controller: &BaseController{container},
// 获取 proto 文件描述 }
GetAllFilesDescriptor() ([]*desc.FileDescriptor, error)
// 获取服务描述
GetServiceDescriptor(service string) (*desc.ServiceDescriptor, error)
// 获取服务方法描述
GetServiceMethodDescriptor(service, method string) (*desc.MethodDescriptor, error)
// 关闭客户端连接
Close() error
} }

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 package contracts
type Job interface { import (
"fmt"
"github.com/samber/do"
)
type JobServer interface {
Start() error Start() error
Stop() error Stop() error
Restart() error Restart() error
}
type JobService interface {
Dispatch(job string, payload any, u User) error 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 LoadGoPlugin(file string) error
RegControllers(items map[string]Controller) error RegControllers(items map[string]Controller) error
RegListens(items map[string]ResListener) error RegListens(items map[string]ResListener) error
RegGRpControllers(items map[string]GRpController) error RegGRpcs(items map[string]GRpc) error
RegJobControllers(items map[string]JobController) error RegJobs(items map[string]Job) error
GetResListener(code string) (ResListener, bool) GetResListener(code string) (ResListener, bool)
GetAppController(code string) (Controller, bool) GetAppController(code string) (Controller, bool)
GetGRpController(code string) (GRpController, bool) GetAppGRpc(code string) (GRpc, bool)
GetJobController(code string) (JobController, bool) GetAppJob(code string) (Job, bool)
} }