feat(iot-app): 初始化物联网应用基础框架
- 创建 API 和 RPC 模块的基础目录结构 - 配置 Makefile 支持多平台构建与 Docker 部署 - 生成 API 接口定义文件及对应处理逻辑 - 实现 RPC 服务端与客户端的初始代码 - 添加 Ent 数据库操作相关配置与工具函数 - 设置 Swagger 文档自动生成与本地调试服务 - 引入国际化支持与错误码统一管理机制 - 初始化 Git 版本控制并添加基础
This commit is contained in:
parent
884dadf68f
commit
0d6a40078c
|
|
@ -0,0 +1,152 @@
|
||||||
|
# Custom configuration | 独立配置
|
||||||
|
# Service name | 项目名称
|
||||||
|
SERVICE=Iot_App
|
||||||
|
# Service name in specific style | 项目经过style格式化的名称
|
||||||
|
SERVICE_STYLE=iot_app
|
||||||
|
# Service name in lowercase | 项目名称全小写格式
|
||||||
|
SERVICE_LOWER=iot_app
|
||||||
|
# Service name in snake format | 项目名称下划线格式
|
||||||
|
SERVICE_SNAKE=iot_app
|
||||||
|
# Service name in snake format | 项目名称短杠格式
|
||||||
|
SERVICE_DASH=iot_app
|
||||||
|
|
||||||
|
# The project version, if you don't use git, you should set it manually | 项目版本,如果不使用git请手动设置
|
||||||
|
VERSION=$(shell git describe --tags --always)
|
||||||
|
|
||||||
|
# The project file name style | 项目文件命名风格
|
||||||
|
PROJECT_STYLE=go_zero
|
||||||
|
|
||||||
|
# Whether to use i18n | 是否启用 i18n
|
||||||
|
PROJECT_I18N=true
|
||||||
|
|
||||||
|
# Swagger type, support yml,json | Swagger 文件类型,支持yml,json
|
||||||
|
SWAGGER_TYPE=json
|
||||||
|
|
||||||
|
# Ent enabled features | Ent 启用的官方特性
|
||||||
|
ENT_FEATURE=sql/execquery,intercept,sql/modifier,privacy,entql
|
||||||
|
|
||||||
|
# The arch of the build | 构建的架构
|
||||||
|
GOARCH=amd64
|
||||||
|
|
||||||
|
# The repository of docker | Docker 仓库地址
|
||||||
|
DOCKER_REPO=registry.cn-hangzhou.aliyuncs.com/simple_admin_vip
|
||||||
|
|
||||||
|
# ---- You may not need to modify the codes below | 下面的代码大概率不需要更改 ----
|
||||||
|
|
||||||
|
GO ?= go
|
||||||
|
GOFMT ?= gofmt "-s"
|
||||||
|
GOFILES := $(shell find . -name "*.go")
|
||||||
|
LDFLAGS := -s -w
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test: # Run test for the project | 运行项目测试
|
||||||
|
go test -v --cover ./api/internal/..
|
||||||
|
go test -v --cover ./rpc/internal/..
|
||||||
|
|
||||||
|
.PHONY: fmt
|
||||||
|
fmt: # Format the codes | 格式化代码
|
||||||
|
$(GOFMT) -w $(GOFILES)
|
||||||
|
|
||||||
|
.PHONY: lint
|
||||||
|
lint: # Run go linter | 运行代码错误分析
|
||||||
|
golangci-lint run -D staticcheck
|
||||||
|
|
||||||
|
.PHONY: tools
|
||||||
|
tools: # Install the necessary tools | 安装必要的工具
|
||||||
|
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
$(GO) install github.com/go-swagger/go-swagger/cmd/swagger@latest
|
||||||
|
|
||||||
|
.PHONY: docker
|
||||||
|
docker: # Build the docker image | 构建 docker 镜像
|
||||||
|
docker build -f Dockerfile-api -t $(DOCKER_REPO)/$(SERVICE_DASH)-tenant-api:${VERSION} .
|
||||||
|
docker build -f Dockerfile-rpc -t $(DOCKER_REPO)/$(SERVICE_DASH)-tenant-rpc:${VERSION} .
|
||||||
|
@echo "Build docker successfully"
|
||||||
|
|
||||||
|
.PHONY: publish-docker
|
||||||
|
publish-docker: # Publish docker image | 发布 docker 镜像
|
||||||
|
docker push $(DOCKER_REPO)/$(SERVICE_DASH)-tenant-rpc:${VERSION}
|
||||||
|
docker push $(DOCKER_REPO)/$(SERVICE_DASH)-tenant-api:${VERSION}
|
||||||
|
@echo "Publish docker successfully"
|
||||||
|
|
||||||
|
.PHONY: gen-api
|
||||||
|
gen-api: # Generate API files | 生成 API 的代码
|
||||||
|
goctls api go --api ./api/desc/all.api --dir ./api --trans_err=true --style=$(PROJECT_STYLE)
|
||||||
|
swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models --exclude-deps
|
||||||
|
@echo "Generate API files successfully"
|
||||||
|
|
||||||
|
.PHONY: gen-rpc
|
||||||
|
gen-rpc: # Generate RPC files from proto | 生成 RPC 的代码
|
||||||
|
goctls rpc protoc ./rpc/$(SERVICE_STYLE).proto --style=$(PROJECT_STYLE) --go_out=./rpc/types --go-grpc_out=./rpc/types --zrpc_out=./rpc --style=$(PROJECT_STYLE)
|
||||||
|
@echo "Generate RPC files successfully"
|
||||||
|
|
||||||
|
.PHONY: gen-ent
|
||||||
|
gen-ent: # Generate Ent codes | 生成 Ent 的代码
|
||||||
|
go run -mod=mod entgo.io/ent/cmd/ent generate --template glob="./rpc/ent/template/*.tmpl" ./rpc/ent/schema --feature $(ENT_FEATURE)
|
||||||
|
@echo "Generate Ent files successfully"
|
||||||
|
|
||||||
|
.PHONY: gen-rpc-ent-logic
|
||||||
|
gen-rpc-ent-logic: # Generate logic code from Ent, need model and group params | 根据 Ent 生成逻辑代码, 需要设置 model 和 group
|
||||||
|
goctls rpc ent --schema=./rpc/ent/schema --style=$(PROJECT_STYLE) --import_prefix=/rpc --service_name=$(SERVICE) --project_name=$(SERVICE_STYLE) -o=./rpc --model=$(model) --group=$(group) --i18n=$(PROJECT_I18N) --proto_out=./rpc/desc/$(shell echo $(model) | tr A-Z a-z).proto --overwrite=true
|
||||||
|
@echo "Generate logic codes from Ent successfully"
|
||||||
|
|
||||||
|
.PHONY: build-win
|
||||||
|
build-win: # Build project for Windows | 构建Windows下的可执行文件
|
||||||
|
env CGO_ENABLED=0 GOOS=windows GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_rpc.exe ./rpc/$(SERVICE_STYLE).go
|
||||||
|
env CGO_ENABLED=0 GOOS=windows GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_api.exe ./api/$(SERVICE_STYLE).go
|
||||||
|
@echo "Build project for Windows successfully"
|
||||||
|
|
||||||
|
.PHONY: build-mac
|
||||||
|
build-mac: # Build project for MacOS | 构建MacOS下的可执行文件
|
||||||
|
env CGO_ENABLED=0 GOOS=darwin GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_rpc ./rpc/$(SERVICE_STYLE).go
|
||||||
|
env CGO_ENABLED=0 GOOS=darwin GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_api ./api/$(SERVICE_STYLE).go
|
||||||
|
@echo "Build project for MacOS successfully"
|
||||||
|
|
||||||
|
.PHONY: build-linux
|
||||||
|
build-linux: # Build project for Linux | 构建Linux下的可执行文件
|
||||||
|
env CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_rpc ./rpc/$(SERVICE_STYLE).go
|
||||||
|
env CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -trimpath -o $(SERVICE_STYLE)_api ./api/$(SERVICE_STYLE).go
|
||||||
|
@echo "Build project for Linux successfully"
|
||||||
|
|
||||||
|
.PHONY: gen-swagger
|
||||||
|
gen-swagger: # Generate swagger file | 生成 swagger 文件
|
||||||
|
swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models --exclude-deps
|
||||||
|
@echo "Generate swagger successfully"
|
||||||
|
|
||||||
|
.PHONY: serve-swagger
|
||||||
|
serve-swagger: # Run the swagger server | 运行 swagger 服务
|
||||||
|
lsof -i:36666 | awk 'NR!=1 {print $2}' | xargs killall -9 || true
|
||||||
|
swagger serve -F=swagger --port 36666 $(SERVICE_STYLE).$(SWAGGER_TYPE)
|
||||||
|
@echo "Serve swagger-ui successfully"
|
||||||
|
|
||||||
|
.PHONY: help
|
||||||
|
help: # Show help | 显示帮助
|
||||||
|
@grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: format-api
|
||||||
|
format-api: # Format API files | 格式化 API 文件
|
||||||
|
@echo "Formatting API files..."
|
||||||
|
@if [ -f "./api/desc/all.api" ]; then \
|
||||||
|
goctl api format --dir ./api/desc ; \
|
||||||
|
echo "API files formatted successfully"; \
|
||||||
|
else \
|
||||||
|
echo "API files not found in ./desc directory"; \
|
||||||
|
fi
|
||||||
|
@if [ -f "./*.api" ]; then \
|
||||||
|
goctl api format --dir . --force; \
|
||||||
|
echo "Root directory API files formatted successfully"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
.PHONY: format-rpc
|
||||||
|
format-rpc: # Format RPC files | 格式化 RPC 文件
|
||||||
|
@echo "Formatting RPC files..."
|
||||||
|
@if [ -d "./rpc/desc" ]; then \
|
||||||
|
find ./desc -name "*.proto" -exec goctl rpc protoc {} --go_out=./ --go-grpc_out=./ --zrpc_out=. \; 2>/dev/null || echo "RPC formatting completed"; \
|
||||||
|
echo "RPC files formatted successfully"; \
|
||||||
|
else \
|
||||||
|
echo "RPC files directory ./rpc/desc not found"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
.PHONY: format-all
|
||||||
|
format-all: format-api format-rpc # Format both API and RPC files | 格式化所有 API 和 RPC 文件
|
||||||
|
@echo "All files formatted successfully"
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
syntax = "v1"
|
||||||
|
|
||||||
|
type Request {
|
||||||
|
Name string `path:"name,options=you|me"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
service api-api {
|
||||||
|
@handler ApiHandler
|
||||||
|
get /from/:name (Request) returns (Response)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
// Code scaffolded by goctl. Safe to edit.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/api/internal/config"
|
||||||
|
"mingyang-admin-iot-app/api/internal/handler"
|
||||||
|
"mingyang-admin-iot-app/api/internal/svc"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/conf"
|
||||||
|
"github.com/zeromicro/go-zero/rest"
|
||||||
|
)
|
||||||
|
|
||||||
|
var configFile = flag.String("f", "etc/api-api.yaml", "the config file")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
var c config.Config
|
||||||
|
conf.MustLoad(*configFile, &c)
|
||||||
|
|
||||||
|
server := rest.MustNewServer(c.RestConf)
|
||||||
|
defer server.Stop()
|
||||||
|
|
||||||
|
ctx := svc.NewServiceContext(c)
|
||||||
|
handler.RegisterHandlers(server, ctx)
|
||||||
|
|
||||||
|
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||||
|
server.Start()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Name: api-api
|
||||||
|
Host: 0.0.0.0
|
||||||
|
Port: 8888
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
module mingyang-admin-iot-app/api
|
||||||
|
|
||||||
|
go 1.25.3
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
// Code scaffolded by goctl. Safe to edit.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package config
|
||||||
|
|
||||||
|
import "github.com/zeromicro/go-zero/rest"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
rest.RestConf
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
// Code scaffolded by goctl. Safe to edit.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/rest/httpx"
|
||||||
|
"mingyang-admin-iot-app/api/internal/logic"
|
||||||
|
"mingyang-admin-iot-app/api/internal/svc"
|
||||||
|
"mingyang-admin-iot-app/api/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var req types.Request
|
||||||
|
if err := httpx.Parse(r, &req); err != nil {
|
||||||
|
httpx.ErrorCtx(r.Context(), w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
l := logic.NewApiLogic(r.Context(), svcCtx)
|
||||||
|
resp, err := l.Api(&req)
|
||||||
|
if err != nil {
|
||||||
|
httpx.ErrorCtx(r.Context(), w, err)
|
||||||
|
} else {
|
||||||
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/api/internal/svc"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/rest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||||
|
server.AddRoutes(
|
||||||
|
[]rest.Route{
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/from/:name",
|
||||||
|
Handler: ApiHandler(serverCtx),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
// Code scaffolded by goctl. Safe to edit.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/api/internal/svc"
|
||||||
|
"mingyang-admin-iot-app/api/internal/types"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ApiLogic struct {
|
||||||
|
logx.Logger
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiLogic {
|
||||||
|
return &ApiLogic{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *ApiLogic) Api(req *types.Request) (resp *types.Response, err error) {
|
||||||
|
// todo: add your logic here and delete this line
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// Code scaffolded by goctl. Safe to edit.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package svc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mingyang-admin-iot-app/api/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServiceContext struct {
|
||||||
|
Config config.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||||||
|
return &ServiceContext{
|
||||||
|
Config: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
|
// goctl 1.9.2
|
||||||
|
|
||||||
|
package types
|
||||||
|
|
||||||
|
type Request struct {
|
||||||
|
Name string `path:"name,options=you|me"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
Name: rpc.rpc
|
||||||
|
ListenOn: 0.0.0.0:8080
|
||||||
|
Etcd:
|
||||||
|
Hosts:
|
||||||
|
- 127.0.0.1:2379
|
||||||
|
Key: rpc.rpc
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
module mingyang-admin-iot-app/rpc
|
||||||
|
|
||||||
|
go 1.25.3
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import "github.com/zeromicro/go-zero/zrpc"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
zrpc.RpcServerConf
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/rpc/internal/svc"
|
||||||
|
"mingyang-admin-iot-app/rpc/rpc"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PingLogic struct {
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
logx.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
|
||||||
|
return &PingLogic{
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *PingLogic) Ping(in *rpc.Request) (*rpc.Response, error) {
|
||||||
|
// todo: add your logic here and delete this line
|
||||||
|
|
||||||
|
return &rpc.Response{}, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
|
// goctl 1.9.2
|
||||||
|
// Source: rpc.proto
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/rpc/internal/logic"
|
||||||
|
"mingyang-admin-iot-app/rpc/internal/svc"
|
||||||
|
"mingyang-admin-iot-app/rpc/rpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RpcServer struct {
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
rpc.UnimplementedRpcServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRpcServer(svcCtx *svc.ServiceContext) *RpcServer {
|
||||||
|
return &RpcServer{
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RpcServer) Ping(ctx context.Context, in *rpc.Request) (*rpc.Response, error) {
|
||||||
|
l := logic.NewPingLogic(ctx, s.svcCtx)
|
||||||
|
return l.Ping(in)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package svc
|
||||||
|
|
||||||
|
import "mingyang-admin-iot-app/rpc/internal/config"
|
||||||
|
|
||||||
|
type ServiceContext struct {
|
||||||
|
Config config.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||||||
|
return &ServiceContext{
|
||||||
|
Config: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/rpc/internal/config"
|
||||||
|
"mingyang-admin-iot-app/rpc/internal/server"
|
||||||
|
"mingyang-admin-iot-app/rpc/internal/svc"
|
||||||
|
"mingyang-admin-iot-app/rpc/rpc"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/conf"
|
||||||
|
"github.com/zeromicro/go-zero/core/service"
|
||||||
|
"github.com/zeromicro/go-zero/zrpc"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/reflection"
|
||||||
|
)
|
||||||
|
|
||||||
|
var configFile = flag.String("f", "etc/rpc.yaml", "the config file")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
var c config.Config
|
||||||
|
conf.MustLoad(*configFile, &c)
|
||||||
|
ctx := svc.NewServiceContext(c)
|
||||||
|
|
||||||
|
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||||
|
rpc.RegisterRpcServer(grpcServer, server.NewRpcServer(ctx))
|
||||||
|
|
||||||
|
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||||
|
reflection.Register(grpcServer)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
defer s.Stop()
|
||||||
|
|
||||||
|
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||||
|
s.Start()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package rpc;
|
||||||
|
option go_package="./rpc";
|
||||||
|
|
||||||
|
message Request {
|
||||||
|
string ping = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message Response {
|
||||||
|
string pong = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
service Rpc {
|
||||||
|
rpc Ping(Request) returns(Response);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,173 @@
|
||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// protoc-gen-go v1.36.10
|
||||||
|
// protoc v3.21.11
|
||||||
|
// source: rpc.proto
|
||||||
|
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
reflect "reflect"
|
||||||
|
sync "sync"
|
||||||
|
unsafe "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Verify that this generated code is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||||
|
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||||
|
)
|
||||||
|
|
||||||
|
type Request struct {
|
||||||
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
|
Ping string `protobuf:"bytes,1,opt,name=ping,proto3" json:"ping,omitempty"`
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Request) Reset() {
|
||||||
|
*x = Request{}
|
||||||
|
mi := &file_rpc_proto_msgTypes[0]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Request) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Request) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Request) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_rpc_proto_msgTypes[0]
|
||||||
|
if x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Request.ProtoReflect.Descriptor instead.
|
||||||
|
func (*Request) Descriptor() ([]byte, []int) {
|
||||||
|
return file_rpc_proto_rawDescGZIP(), []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Request) GetPing() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Ping
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
|
Pong string `protobuf:"bytes,1,opt,name=pong,proto3" json:"pong,omitempty"`
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Response) Reset() {
|
||||||
|
*x = Response{}
|
||||||
|
mi := &file_rpc_proto_msgTypes[1]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Response) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Response) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Response) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_rpc_proto_msgTypes[1]
|
||||||
|
if x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Response.ProtoReflect.Descriptor instead.
|
||||||
|
func (*Response) Descriptor() ([]byte, []int) {
|
||||||
|
return file_rpc_proto_rawDescGZIP(), []int{1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Response) GetPong() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Pong
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var File_rpc_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
|
const file_rpc_proto_rawDesc = "" +
|
||||||
|
"\n" +
|
||||||
|
"\trpc.proto\x12\x03rpc\"\x1d\n" +
|
||||||
|
"\aRequest\x12\x12\n" +
|
||||||
|
"\x04ping\x18\x01 \x01(\tR\x04ping\"\x1e\n" +
|
||||||
|
"\bResponse\x12\x12\n" +
|
||||||
|
"\x04pong\x18\x01 \x01(\tR\x04pong2*\n" +
|
||||||
|
"\x03Rpc\x12#\n" +
|
||||||
|
"\x04Ping\x12\f.rpc.Request\x1a\r.rpc.ResponseB\aZ\x05./rpcb\x06proto3"
|
||||||
|
|
||||||
|
var (
|
||||||
|
file_rpc_proto_rawDescOnce sync.Once
|
||||||
|
file_rpc_proto_rawDescData []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func file_rpc_proto_rawDescGZIP() []byte {
|
||||||
|
file_rpc_proto_rawDescOnce.Do(func() {
|
||||||
|
file_rpc_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_rpc_proto_rawDesc), len(file_rpc_proto_rawDesc)))
|
||||||
|
})
|
||||||
|
return file_rpc_proto_rawDescData
|
||||||
|
}
|
||||||
|
|
||||||
|
var file_rpc_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||||
|
var file_rpc_proto_goTypes = []any{
|
||||||
|
(*Request)(nil), // 0: rpc.Request
|
||||||
|
(*Response)(nil), // 1: rpc.Response
|
||||||
|
}
|
||||||
|
var file_rpc_proto_depIdxs = []int32{
|
||||||
|
0, // 0: rpc.Rpc.Ping:input_type -> rpc.Request
|
||||||
|
1, // 1: rpc.Rpc.Ping:output_type -> rpc.Response
|
||||||
|
1, // [1:2] is the sub-list for method output_type
|
||||||
|
0, // [0:1] is the sub-list for method input_type
|
||||||
|
0, // [0:0] is the sub-list for extension type_name
|
||||||
|
0, // [0:0] is the sub-list for extension extendee
|
||||||
|
0, // [0:0] is the sub-list for field type_name
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { file_rpc_proto_init() }
|
||||||
|
func file_rpc_proto_init() {
|
||||||
|
if File_rpc_proto != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
type x struct{}
|
||||||
|
out := protoimpl.TypeBuilder{
|
||||||
|
File: protoimpl.DescBuilder{
|
||||||
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
|
RawDescriptor: unsafe.Slice(unsafe.StringData(file_rpc_proto_rawDesc), len(file_rpc_proto_rawDesc)),
|
||||||
|
NumEnums: 0,
|
||||||
|
NumMessages: 2,
|
||||||
|
NumExtensions: 0,
|
||||||
|
NumServices: 1,
|
||||||
|
},
|
||||||
|
GoTypes: file_rpc_proto_goTypes,
|
||||||
|
DependencyIndexes: file_rpc_proto_depIdxs,
|
||||||
|
MessageInfos: file_rpc_proto_msgTypes,
|
||||||
|
}.Build()
|
||||||
|
File_rpc_proto = out.File
|
||||||
|
file_rpc_proto_goTypes = nil
|
||||||
|
file_rpc_proto_depIdxs = nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,121 @@
|
||||||
|
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// - protoc-gen-go-grpc v1.6.0
|
||||||
|
// - protoc v3.21.11
|
||||||
|
// source: rpc.proto
|
||||||
|
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
codes "google.golang.org/grpc/codes"
|
||||||
|
status "google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
// Requires gRPC-Go v1.64.0 or later.
|
||||||
|
const _ = grpc.SupportPackageIsVersion9
|
||||||
|
|
||||||
|
const (
|
||||||
|
Rpc_Ping_FullMethodName = "/rpc.Rpc/Ping"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RpcClient is the client API for Rpc service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||||
|
type RpcClient interface {
|
||||||
|
Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcClient struct {
|
||||||
|
cc grpc.ClientConnInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRpcClient(cc grpc.ClientConnInterface) RpcClient {
|
||||||
|
return &rpcClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *rpcClient) Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
|
||||||
|
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||||
|
out := new(Response)
|
||||||
|
err := c.cc.Invoke(ctx, Rpc_Ping_FullMethodName, in, out, cOpts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RpcServer is the server API for Rpc service.
|
||||||
|
// All implementations must embed UnimplementedRpcServer
|
||||||
|
// for forward compatibility.
|
||||||
|
type RpcServer interface {
|
||||||
|
Ping(context.Context, *Request) (*Response, error)
|
||||||
|
mustEmbedUnimplementedRpcServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnimplementedRpcServer must be embedded to have
|
||||||
|
// forward compatible implementations.
|
||||||
|
//
|
||||||
|
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||||
|
// pointer dereference when methods are called.
|
||||||
|
type UnimplementedRpcServer struct{}
|
||||||
|
|
||||||
|
func (UnimplementedRpcServer) Ping(context.Context, *Request) (*Response, error) {
|
||||||
|
return nil, status.Error(codes.Unimplemented, "method Ping not implemented")
|
||||||
|
}
|
||||||
|
func (UnimplementedRpcServer) mustEmbedUnimplementedRpcServer() {}
|
||||||
|
func (UnimplementedRpcServer) testEmbeddedByValue() {}
|
||||||
|
|
||||||
|
// UnsafeRpcServer may be embedded to opt out of forward compatibility for this service.
|
||||||
|
// Use of this interface is not recommended, as added methods to RpcServer will
|
||||||
|
// result in compilation errors.
|
||||||
|
type UnsafeRpcServer interface {
|
||||||
|
mustEmbedUnimplementedRpcServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterRpcServer(s grpc.ServiceRegistrar, srv RpcServer) {
|
||||||
|
// If the following call panics, it indicates UnimplementedRpcServer was
|
||||||
|
// embedded by pointer and is nil. This will cause panics if an
|
||||||
|
// unimplemented method is ever invoked, so we test this at initialization
|
||||||
|
// time to prevent it from happening at runtime later due to I/O.
|
||||||
|
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||||
|
t.testEmbeddedByValue()
|
||||||
|
}
|
||||||
|
s.RegisterService(&Rpc_ServiceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _Rpc_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(Request)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(RpcServer).Ping(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: Rpc_Ping_FullMethodName,
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(RpcServer).Ping(ctx, req.(*Request))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rpc_ServiceDesc is the grpc.ServiceDesc for Rpc service.
|
||||||
|
// It's only intended for direct use with grpc.RegisterService,
|
||||||
|
// and not to be introspected or modified (even as a copy)
|
||||||
|
var Rpc_ServiceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "rpc.Rpc",
|
||||||
|
HandlerType: (*RpcServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "Ping",
|
||||||
|
Handler: _Rpc_Ping_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{},
|
||||||
|
Metadata: "rpc.proto",
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
|
// goctl 1.9.2
|
||||||
|
// Source: rpc.proto
|
||||||
|
|
||||||
|
package rpcclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"mingyang-admin-iot-app/rpc/rpc"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/zrpc"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Request = rpc.Request
|
||||||
|
Response = rpc.Response
|
||||||
|
|
||||||
|
Rpc interface {
|
||||||
|
Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultRpc struct {
|
||||||
|
cli zrpc.Client
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRpc(cli zrpc.Client) Rpc {
|
||||||
|
return &defaultRpc{
|
||||||
|
cli: cli,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *defaultRpc) Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
|
||||||
|
client := rpc.NewRpcClient(m.cli.Conn())
|
||||||
|
return client.Ping(ctx, in, opts...)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue