DEV Community

zhuyasen
zhuyasen

Posted on • Updated on

Example 1, Automatically generate a complete gin+gorm+redis+CRUD web service project without writing a line of Go code

Dependencies

  • Import sql into mysql services.
  • Installation tool sponge.

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 Sponge UI interface, click on the left menu bar 【SQL】→【Create web project】, fill in some parameters to generate the complete project code for the web service.

Image description

The web service code is mainly composed of commonly used libraries such as gin, gorm, go-redis, and also includes swagger documentation, test code, common service governance function code, build deployment scripts, etc.

Switch to the web directory and run the command:

# Generate swagger documentation  
make docs  

# Compile and start the web service  
make run  
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:8080/swagger/index.html in your browser to perform CRUD operations on the table.

Image description

Batch add CRUD code embedded in web service

Enter the Sponge UI interface, click on the left menu bar 【Public】→【Generate handler CRUD code】, select any number of tables to generate code, then move the generated CRUD code to the web service directory to complete batch addition of CURD interfaces in the web service without changing any code.

Image description

Switch to the web directory and run the command:

# Generate swagger documentation  
make docs  

# Compile and start the web service  
make run  
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:8080/swagger/index.html in your browser to see the newly added CRUD interfaces.

This is the complete web service code generated from the above steps https://github.com/zhufuyi/sponge_examples/tree/main/1_web-gin-CRUD

Click to view detailed web service development documentation https://go-sponge.com/web-development-mysql

Top comments (0)