Skip to content

grpc framework can work with gin and grpc-gateway

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.art
Notifications You must be signed in to change notification settings

powerpuffpenguin/jsgenerate_grpc

Repository files navigation

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

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

// 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

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

$ ./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

About

grpc framework can work with gin and grpc-gateway

Topics

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.art

Stars

Watchers

Forks

Packages

No packages published