Web services framework based on GO-GIN framework

Project Github:Github.com/Benny66/gin…

app

Project project main code folder directory, including DAO data layer, Model model layer, service logic layer;

  • Data layer is the encapsulation operation of data operation;
  • Model layer is to define the database table model;
  • The logic layer is the processing layer of the core business logic of the project.

Most of the calls between modules in the framework of this project adopt the han mode in singleton mode, which is global initialization when loading (the disadvantage is that the startup takes up memory and is slow). If you don’t know about it, you can view it in the same directory

Golang singleton mode. Md

Dao [Data layer]

Dao [data layer] is a module based on GORM to add, delete, check and change data

  • Create and UPDATE require transactions to be enabled for data creation and modification, and control the start, rollback, and commit operations at the logical layer.
  • Common encapsulation methods include add, delete, check and change, Paginate, FindAll, WhereQuery, Joins, Preloads and so on
func (dao *userDao) Create(tx *gorm.DB, data *model.User) (rowsAffected int64, err error) { db := tx.Create(data) if err = db.Error; db.Error ! = nil { return } rowsAffected = db.RowsAffected return } func (dao *userDao) WhereQuery(query interface{}, args ... interface{}) *userDao { return &userDao{ dao.gm.Where(query, args...) , } } func (dao *userDao) Joins(query string, args ... interface{}) *userDao { return &userDao{ dao.gm.Joins(query, args), } }Copy the code

【 Model 】

The model layer is the module that defines the database table name and the field in the table.

  • ModelTime defines automatic conversion of store and query time formats
  • You can define the table name and table fields corresponding to the model
type User struct { ID uint `gorm:"primaryKey; column:id" json:"id"` UserName string `gorm:"column:username; unique; not null" json:"username"` Password string `gorm:"column:password; not null" json:"password"` CreatedAt ModelTime `gorm:"column:created_at" json:"created_at"` UpdatedAt ModelTime `gorm:"column:updated_at" json:"updated_at"` }Copy the code

Service [Logical layer]

Service [logic layer] is the main code layer. Developers basically develop and fix bugs on this module. It is the core content to implement the logic of their projects

  • Basically, a service module corresponds to a file. For example, each interface of the login module can be named user.go
  • The define module is used to define the directory of data structure types. Different service types and logins require different request parameters and return parameter structures. For example, define type UserLoginApiReq struct to receive the parameter types of login interfaces
  • Service module, as the name implies, is the processing module of business logic service, including the verification of request data parameters, business logic processing data, calling the data layer to save the database
  • Router. go Interconnects routes and defines them in a unified manner

Config [system configuration]

System configuration module, config.go, including service information, database information, log configuration information, WS configuration information and so on

Db [database]

Database folder module, currently using a lightweight SQLite database, the subsequent compatible with multiple databases

Migrations

Data migration file module, database migration script command can be carried out when the project is initialized or upgraded;

You need to add install.lock to the root directory to migrate the database

Public 【 public module 】

The common module of the project, including: image, HTML, CSS, JS and other files

Routers [Routing]

When the Web service is started, the gin routing module is initialized and the service is started

runtime

Runtime module to save files such as log logs and cache

utils

Toolkit required by the project framework, including: self-encapsulated libraries and invoke third-party encapsulated libraries