This is a creation in Article, where the information may have evolved or changed.
Building the portal main main file
The way of Simplicity
In order to be as concise as possible, we only need to load the core components in the portal to
package mainimport ( // 引入项目的路由 "github.com/gohouse/kuaixinwen/router" // 引入驱动 . "github.com/gohouse/kuaixinwen/bootstrap" // fmt包 "fmt")func main() { fmt.Println("start...,port:8888, visit: http://localhost:8888") // 加载数据库 defer DB.Close() // 加载路由 router.Run(App.HttpServer) // 启动web服务 err := App.StartServer(8888) if err!=nil{ fmt.Println(err) }}
Here, the main.go file has been completely completed, the text has a corresponding comment, in addition, some objects, we need to do some instructions to him:
main function's
DB and
App
These two are the
bootstrap Gorose and Dotweb objects after the drive, as shown below
mainThe directories introduced in the package bootstrap are described below
/bootstrap/bootRouter.goFile
This is a routing driver, and all the request drivers for the site are done by him.
The content is very simple:
package bootstrapimport ( "github.com/devfeel/dotweb")var App *dotweb.DotWebfunc init() { //init DotApp App = dotweb.New() //set log path App.SetLogPath("../log")}
-
/bootstrap/bootdatabase.go file
Here to load the database configuration and driver, if you need to change the database related information, only need to maintain this driver
content is very simple:
package bootstrapimport (//Introducing Gorose "github.com/gohouse/gorose"//Introducing Database Configuration "github.com /gohouse/kuaixinwen/config "//introduce MySQL driver, here Select the corresponding database driver can _" Github.com/go-sql-driver/mysql ")//Database link object var DB gorose. Connectionfunc init () {var err error//Assemble database information, and load db/config. Dbconfig the database configuration information Db,err = Gorose. Open (config. Dbconfig) If err!=nil{panic ("Database link Failed")} errs: = DB. Ping () If errs!=nil{panic ("Database link Failed")}}
The corresponding /config/database.go configuration is as follows
package configvar DbConfig = map[string]interface{}{ "default": "mysql_dev", // 默认数据库配置 "SetMaxOpenConns": 300, // (连接池)最大打开的连接数,默认值为0表示不限制 "SetMaxIdleConns": 10, // (连接池)闲置的连接数, 默认-1 "mysql_dev": map[string]string{// 定义名为 mysql_dev 的数据库配置 "host": "192.168.200.248", // 数据库地址 "username": "gcore", // 数据库用户名 "password": "gcore", // 数据库密码 "port": "3306", // 端口 "database": "test", // 链接的数据库名字 "charset": "utf8", // 字符集 "protocol": "tcp", // 链接协议 "prefix": "", // 表前缀 "driver": "mysql", // 数据库驱动(mysql,sqlite,postgres,oracle,mssql) },}
Here, the drive is all done.
mainThe route introduced in the router next section describes
The project Source: Https://github.com/gohouse/kuaixinwen