Golang code specification framework construction specification

The purpose of writing this specification is to allow developers to build project frameworks more conveniently and quickly, while at the same time unifying the technology stack within the team.

guiding ideology

Framework construction needs to adapt to changes in the project and choose the most appropriate architecture for the project. Nothing is static. The technical architecture should be continuously improved and designed to the maximum extent possible while meeting current needs, but not over-designed.

Project importance

Choose different technology stacks according to the importance of the project.

core projects

Core projects need to adopt the most reliable technology stack and build the most complete code development framework.

Golang version

For new projects, golang-1.19 and above are recommended (recommended on 2023.05.09)

Layered architecture: DDD

Use DDD ideas to manage code services.

Service framework: Kratos

The service initially chooses a monolithic architecture and the service framework chooses Kratos, leaving the possibility of later expansion into microservices.

Please add image description

Flow Control: Flow Restrictor

Global process control should be set at creation time.

Please add image description

Interface definition: Protocol Buffer

External interfaces uniformly use the RestFul style HTTP protocol, and internal service remote calls use the GRPC protocol. Define the interface document by writing a Protocol Buffer file, automatically generate golang code and swagger files, and import the swagger file into apifox or postman to generate the interface document.

Dependency injection: Wire

Use wire to implement dependency injection.

database

When operating the database, multiple addition, deletion, modification and query operations are involved, and database transactions are required.

Relational database: MySQL+Ent

When the business logic is complex and the relationships between the businesses are relatively close, a relational database is used, and MySQL is the first choice for the relational database. Use Ent as an ORM framework to interact with MySQL database.

Non-relational database: MongoDB

When the main object of the business is a document object and the connection between the businesses is relatively simple, a non-relational database is used, and MongoDB is the first choice for the non-relational database.

Cache: Redis

Redis is the preferred cache database.

Search engine: Elasticsearch

The search engine uses ElasticSearch to update the data in ES using a combination of automatic scheduled synchronization and manual synchronization.

Code inspection: GIt-Hooks+golangci-lint

Before git commit, trigger code inspection through git hooks. Code inspection uses golangci-lint tool.

gitignore

You need to define gitignore and add the local debugging files that can be automatically generated by the program to the ignore list.

makefile

Build the project using makefile.

Project deployment

The project must contain the Dockerfile used to build the image. If you use docker deployment, there should be a docker-compose file. If you use K8s to deploy, you need to include ConfigMap, Deployment, Secret, Service and other yaml files.

important projects

The construction principles of important projects are almost the same as those of core projects.

General project

You can use the Gin framework to develop quickly, skip DDD, skip automatic code checking, you can omit the current limiter, and you can choose database and ORM more freely.

experimental project

You can freely choose the development framework and have more innovation and freedom.

Guess you like

Origin blog.csdn.net/qq_26356861/article/details/131290203