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 blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay