DEV Community

zhuyasen
zhuyasen

Posted on

Example 3, Automatically generate generic web service (gin) project code, increasing development efficiency by at least 1x

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

After installing the tool sponge, execute the command to open the UI interface:

sponge run
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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 
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:8080/apis/swagger/index.html in your browser to test the API.

Image description

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay