Skip to main content
Version: 2.8.x(Latest)

This chapter will formally develop the Star English Book project, where we will develop the first interface: user registration, to provide user registration functionality.

Code Layering


The GoFrame framework has an excellent code layering design, with a clear and well-structured software architecture. During development, you can follow the "three-step" rule: first, define interface information at the Api layer, then the Controller layer receives HTTP interface requests, and finally, the Logic layer completes the specific logic and database interaction. This is actually a variant of the MVC concept.

Flow

ORM


ORM stands for Object-Relational Mapping, and its core function is to map data tables to objects, typically structs in the Go language.

After mapping, we can handle data tables by manipulating objects, avoiding the need to write SQL statements directly. This not only improves code readability and maintainability but also prevents common SQL injection issues from a security standpoint.

GoFrame provides built-in ORM functionality through the gdb component, offering convenient and common SQL operations.

Here's an example:

ctx := gctx.New()
Users.Ctx(ctx).Where("username", "admin").One()

This is a simple query operation, and ORM will automatically generate SQL statements based on it.

SELECT * FROM users WHERE username = 'admin';

ORM only provides the data table mapping function, and actual interaction with the database still requires using a database driver.

Database Driver


GoFrame is a component-based framework, and for better extensibility, the database driver has been decoupled, allowing users to choose different database drivers according to their needs to support different databases.

Installing Drivers

Install the MySQL driver:

$ go get -u github.com/gogf/gf/contrib/drivers/mysql/v2

Other database drivers:

go get -u github.com/gogf/gf/contrib/drivers/clickhouse/v2
go get -u github.com/gogf/gf/contrib/drivers/dm/v2
go get -u github.com/gogf/gf/contrib/drivers/mssql/v2
go get -u github.com/gogf/gf/contrib/drivers/oracle/v2
go get -u github.com/gogf/gf/contrib/drivers/pgsql/v2
go get -u github.com/gogf/gf/contrib/drivers/sqlite/v2
go get -u github.com/gogf/gf/contrib/drivers/sqlitecgo/v2

Importing Drivers

Globally import the driver:

main.go

package main

import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
...
)

func main() {
cmd.Main.Run(gctx.GetInitCtx())
}

Checking the Driver

After completing the above, we need to check if the database driver is successfully installed. Add the database configuration in the configuration file, and if there is any content, clear all of it to maintain consistency with the following:

manifest/config/config.yaml

server:
address: ":8000" # Server listening port
openapiPath: "/api.json" # OpenAPI interface document address
swaggerPath: "/swagger" # Built-in SwaggerUI display address

database:
default:
link: "mysql:root:12345678@tcp(127.0.0.1:3306)/star?loc=Local"
debug: true

Modify the main function to check if the database is connected properly before the program runs.

main.go

package main

···

func main() {
var err error

// Check if the database is connected
err = connDb()
if err != nil {
panic(err)
}

cmd.Main.Run(gctx.GetInitCtx())
}

// connDb checks if the database connection is normal
func connDb() error {
err := g.DB().PingMaster()
if err != nil {
return errors.New("Failed to connect to the database")
}
return nil
}

Run the project, if no error is reported, the database driver is successfully installed.

$ gf run main.go
build: .\main.go
go build -o .\main.exe .\main.go
.\main.exe
build running pid: 24612
2024-11-07 16:42:51.197 [INFO] {f89117371ba305188476a74abc958a23} swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2024-11-07 16:42:51.197 [INFO] pid[24612]: http server started listening on [:8000]
2024-11-07 16:42:51.197 [INFO] {f89117371ba305188476a74abc958a23} openapi specification is serving at address: http://127.0.0.1:8000/api.json

ADDRESS | METHOD | ROUTE | HANDLER | MIDDLEWARE
----------|--------|------------|-------------------------------------------------------|--------------------
:8000 | ALL | /api.json | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec |
----------|--------|------------|-------------------------------------------------------|--------------------
:8000 | ALL | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI | HOOK_BEFORE_SERVE
----------|--------|------------|-------------------------------------------------------|--------------------

gf run main.go is a command provided by GF CLI to run the program. When a developer modifies a go file in the project, the command stops the original program, automatically compiles, and runs the current program. It can be used as a replacement for go run main.go.