CRUD API interface code generation tools are relatively mature, CRUD code is standardized fixed, relatively easy to generate, for any custom API interface code, should be many developers or manual writing, these are not standardized interface code, how should it be generated?
The solution here is to use the protobuf protocol to describe the api interface, and generate any api interface code of the web service through the protoc plug-in protoc-gen-go-gin.
Dependencies
- Prepare proto file user.proto.
- Installation tool sponge.
After installing the tool sponge, execute the command to open the UI interface:
sponge run
Quickly Create a Web Project
Enter the UI interface of sponge, click 【Protobuf】→【Create Web Project】in the left menu bar, and fill in some parameters to generate the web service project code.
The web framework uses gin. It also includes swagger documents, common service governance function codes, and build and deployment scripts. You can choose which database to use.
Change to the web directory and execute the command:
# Generate code
make proto
# Open the internal/handler/user.go code file and fill in the specific logic code according to the generated template code.
# compile and start web services
make run
Open http://localhost:8080/apis/swagger/index.html in your browser to test the API.
This is the generic web service code generated from the above steps https://github.com/zhufuyi/sponge_examples/tree/main/3_web-gin-protobuf
You can automatically add CRUD api interfaces
and manually add custom api interfaces
to the generated generic web service code, see the detailed documentation for developing generic web services https://go-sponge.com/zh-cn/web-development-protobuf
Top comments (0)