项目作者: powerpuffpenguin

项目描述 :
grpc framework can work with gin and grpc-gateway
高级语言: TypeScript
项目地址: git://github.com/powerpuffpenguin/jsgenerate_grpc.git
创建时间: 2021-02-08T01:58:59Z
项目社区:https://github.com/powerpuffpenguin/jsgenerate_grpc

开源协议:MIT License

下载


Deprecated, please use the new template https://github.com/powerpuffpenguin/jsgenerate_grpc-gateway

jsgenerate_grpc

中文

A grpc framework template Use jsgenerate tool to quickly create a grpc server framework, which can work with gin grpc-gateway.

install

After installing jsgenerate, clone the project to the ~/.jsgenerate/init folder

new project

Use the following command to initialize the frame in the current directory

  1. 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

Directory Structure

  • bin -> output file directory
  • cmd -> console command analysis
    • cmd/internal/daemon -> Frame entrance
  • Configure -> project configuration file analysis
  • js-build -> automation script implemented by typescript
  • build.js -> automation script
  • logger -> log system
  • management -> grpc module management and middleware
    • module -> grpc module
    • pb -> grpc protocol definition
    • protocol -> golang grpc code generated by protoc
  • static -> when using gin, embedded web static files
  • third_party -> googleapis
  • utils -> tool function types used in some projects
  • web -> embedded gin support
  • view -> angular front-end project created when using the view tag to initialize

module

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

  1. The project prefix (generated from the project name) is /jsgenerate_kk.
  2. fullMethod is /jsgenerate_kk.features.logger/Service.Level
  3. At this time, Management will forward the request to the Service.Level of the features.logger module to respond
  4. At this time proto should be defined 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

  1. // Module grpc module interface
  2. type Module interface {
  3. // ID module id must lower case and unique
  4. ID() string
  5. // OnStart callback at before started
  6. OnStart(basePath string, data json.RawMessage)
  7. // RegisterGRPC register grpc
  8. RegisterGRPC(srv *grpc.Server, middleware *Middleware) error
  9. // RegisterGateway register grpc-gateway
  10. RegisterGateway(srv *runtime.ServeMux, clientConn *grpc.ClientConn) error
  11. // OnReload callback when module should reload configure
  12. OnReload(basePath string, data json.RawMessage, tag string) (e error)
  13. // OnClearDBCache callback when module should clear db cache
  14. OnClearDBCache(tag string) error
  15. // OnClearCache callback when module should clear cache
  16. OnClearCache(tag string) error
  17. // OnStop callback at before stoped
  18. OnStop()
  19. }

If you don’t need to support grpc-gateway, you don’t need to define RegisterGateway

middleware

  • Package management implements a middleware with two functions compatible with grpc interceptor. You should register the middleware for the function of the current module in RegisterGRPC
  • You can refer to the default module in /module/features to see how to use middleware
  • You can refer to the implementation in /module/interceptor.go to implement your own middleware

build.js

build.js is an automation tool under nodejs that provides project compilation functions

  1. $ ./build.js
  2. Usage: ./build.js [options] [command]
  3. Options:
  4. -h, --help display help for command
  5. Commands:
  6. linux [options] build code to linux
  7. freebsd [options] build code to freebsd
  8. darwin [options] build code to darwin
  9. windows [options] build code to windows
  10. version update version/version.go
  11. test [options] run go test
  12. grpc [options] build *.proto to grpc code
  13. source build static source to golang code
  14. help [command] display help for command

example

  1. # Generate the proto in /pb into golang's grpc implementation
  2. # If grpc-gateway is enabled, please refer to https://github.com/grpc-ecosystem/grpc-gateway to configure the environment
  3. ./build.js grpc -l go
  4. # Embed /static into the front end of the web page, if the gin function is not enabled, this operation is not required
  5. ./build.js source
  6. # Compile the program under linux/freebsd/darwin/windows
  7. ./build.js linux