Full project address: Go-shop-B2C
preface
This article will explain some basic configuration to start the project, such as through the bee tool to generate model layer, CORS configuration, router configuration, log configuration and so on
The Bee tool generates the Model layer
Bee Tool Introduction
Bee tool is a project created to assist in rapid development of Beego projects. Through Bee, you can easily create, heat compile, develop, test, and deploy Beego projects.
Bee tool installation
go get -u github.com/beego/bee/v2
Copy the code
The input
bee version
Copy the code
As shown below:
This section only describes bee reverse generation of API applications based on MySql
API command
Bee API [appname] [-tables=""] [-driver=mysql] [-conn="root:<password>@tcp(127.0.0.1:3306)/test"]Copy the code
Description of optional commands
- Appname, the name of the generated API application
- -tables: indicates the generated table name. “” reflects all tables. If a fixed table name is entered, only the entered table name is reflected
- – Driver Indicates a database driver that supports Mysql, Oracle, and Mongodb
- -conn, connection address, root:@tcp(127.0.0.1:3306)/test
Enter the following command:
Bee API appname-tables ="" -driver= mysql-conn ="root:123456@tcp(127.0.0.1:3306)/go-shop-b2c"Copy the code
The directory structure is as follows:
Project and SQL files
- Click the jump
Project Routing Settings
We’ve created the BeeGo project and we’ve seen it up and running, so how does it work? Let’s start with the import file:
package main
import(_"quickstart/routers"
"github.com/beego/beego/v2/server/web"
)
func main(a) {
web.Run()
}
Copy the code
We see that main is the entry function, but we know that Go is executed in the following way:
The quickstart/routers package has been added to the interface, and the init function has been added to the interface.
package routers
import (
"appname/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init(a) {
ns := beego.NewNamespace("/v1",
beego.NSNamespace("/ad",
beego.NSInclude(
&controllers.AdController{},
),
),
)
beego.AddNamespace(ns)
}
Copy the code
CORS configuration
API projects typically require cross-domain configuration to facilitate front-end local debugging, as follows:
func corsInit(a) {
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
/ / optional parameters "GET", "POST", "PUT", "DELETE", "OPTIONS" (*) for all
// where Options cross-domain complex request precheck
AllowMethods: []string{"GET"."POST"."PUT"."DELETE"."OPTIONS"},
// Specifies the types of headers allowed
AllowHeaders: []string{"Origin"."Authorization"."Access-Control-Allow-Origin"."Access-Control-Allow-Headers"."Content-Type"},
// A list of exposed HTTP headers
ExposeHeaders: []string{"Content-Length"."Access-Control-Allow-Origin"."Access-Control-Allow-Headers"."Content-Type"},
// If set, authentication credentials such as cookies are allowed to be shared
AllowCredentials: true.// Specify the accessible domain name AllowOrigins
AllowOrigins: []string{"*"}}}))Copy the code
The log configuration
_ = logs.SetLogger("console")
logs.EnableFuncCallDepth(true)
Copy the code