grpc framework can work with gin and grpc-gateway
Deprecated, please use the new template https://github.com/powerpuffpenguin/jsgenerate_grpc-gateway
A grpc framework template Use jsgenerate tool to quickly create a grpc server framework, which can work with gin grpc-gateway.
After installing jsgenerate, clone the project to the ~/.jsgenerate/init folder
Use the following command to initialize the frame in the current directory
jsgenerate init jsgenerate_grpc -p your_domain/your_project -t init-supplement -t default
Please replace your_domain/your_project with your own project package name
tag | meaning |
---|---|
init-supplement | Skip existing files |
init-trunc | Overwrite existing file |
gateway | Generate gateway code |
gin | Generate gateway,gin code |
view | Generate gateway, gin code and a default angular front-end code |
db | Generate database code |
default | gateway+gin+view+db |
The struct Management in package management implements module management and middleware management.DefaultManagement() will return the default module manager
Management will confirm the processing module according to the fullMethod requested by grpc. The fullMethod is parsed according to the prefix + module id + implementation method. For example
package jsgenerate_kk.features.logger;
pb/features and module/features should be one-to-one correspondence, used to define grpc and implement grpc modules, respectively. You can refer to the generated default module to implement your own module.
A Management module needs to implement the Module interface
// Module grpc module interface
type Module interface {
// ID module id must lower case and unique
ID() string
// OnStart callback at before started
OnStart(basePath string, data json.RawMessage)
// RegisterGRPC register grpc
RegisterGRPC(srv *grpc.Server, middleware *Middleware) error
// RegisterGateway register grpc-gateway
RegisterGateway(srv *runtime.ServeMux, clientConn *grpc.ClientConn) error
// OnReload callback when module should reload configure
OnReload(basePath string, data json.RawMessage, tag string) (e error)
// OnClearDBCache callback when module should clear db cache
OnClearDBCache(tag string) error
// OnClearCache callback when module should clear cache
OnClearCache(tag string) error
// OnStop callback at before stoped
OnStop()
}
If you don’t need to support grpc-gateway, you don’t need to define RegisterGateway
build.js is an automation tool under nodejs that provides project compilation functions
$ ./build.js
Usage: ./build.js [options] [command]
Options:
-h, --help display help for command
Commands:
linux [options] build code to linux
freebsd [options] build code to freebsd
darwin [options] build code to darwin
windows [options] build code to windows
version update version/version.go
test [options] run go test
grpc [options] build *.proto to grpc code
source build static source to golang code
help [command] display help for command
example
# Generate the proto in /pb into golang's grpc implementation
# If grpc-gateway is enabled, please refer to https://github.com/grpc-ecosystem/grpc-gateway to configure the environment
./build.js grpc -l go
# Embed /static into the front end of the web page, if the gin function is not enabled, this operation is not required
./build.js source
# Compile the program under linux/freebsd/darwin/windows
./build.js linux