The article mentioned configuration and log has been initialized, the article goes on to the database and redis initialization is complete.
Initialize the database
Database orm I choose xorm. First, add the corresponding config.go config.json and database configuration.
config.json:
"db_config": {
"db_host": "127.0.0.1",
"db_port": "3306",
"db_user": "root",
"db_password": "123456",
"db_name": "test"
}
config.go
type DBConfig struct {
DbHost string `json:"db_host"`
DbPort string `json:"db_port"`
DbUser string `json:"db_user"`
DbPassword string `json:"db_password"`
DbName string `json:"db_name"`
}
Next, initialize the database:
package db
import (
"github.com/TomatoMr/awesomeframework/config"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/pkg/errors"
)
var engine *xorm.Engine
func InitEngine() error {
var err error
conf := config.GetConfig()
engine, err = xorm.NewEngine("mysql", conf.DBConfig.DbUser+
":"+conf.DBConfig.DbPassword+"@tcp("+conf.DBConfig.DbHost+":"+conf.DBConfig.DbPort+")/"+conf.DBConfig.DbName+"?charset=utf8")
if err != nil {
err = errors.Wrap(err, "InitEngine1")
return err
}
err = engine.Ping()
if err != nil {
err = errors.Wrap(err, "InitEngine2")
return err
}
return nil
}
func GetEngine() *xorm.Engine {
return engine
}
We then create a sql, and so will test the connection with:
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE `test`;
USE `test`;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`
(
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`name` varchar(40) NOT NULL COMMENT '名字',
`age` int NOT NULL DEFAULT 0 COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE = InnoDB COMMENT 'users';
Initialization redis
Also to add configuration:
config.json:
"redis_config": {
"addr": "127.0.0.1:6379",
"password": "",
"db": 0
}
config.go:
type RedisConfig struct {
Addr string `json:"addr"`
Password string `json:"password"`
DB int `json:"db"`
}
Initialization redis:
func InitRedis() error {
conf := config.GetConfig()
client = redis.NewClient(&redis.Options{
Addr: conf.RedisConfig.Addr,
Password: conf.RedisConfig.Password,
DB: conf.RedisConfig.DB,
})
pong, err := client.Ping().Result()
if err != nil {
err = errors.Wrap(err, "InitRedis")
return err
}
logger.GetLogger().Info("Redis ping:", zap.String("ping", pong))
return nil
}
Adjustment entry file
err = db.InitEngine()
if err != nil {
fmt.Printf("Init DB failed. Error is %v", err)
os.Exit(1)
}
err = redis.InitRedis()
if err != nil {
fmt.Printf("Init Redis failed. Error is %v", err)
os.Exit(1)
}
have a test
Using the above sql, create a database named test, and then build redis, and then we test:
Compile:
go build
run:
awesomeframework --config=./config/config.json
Look Log Print:
2020-01-20T20:09:26.798+0800 info Redis ping: {"ping": "PONG"}
2020-01-20T20:09:26.798+0800 info Init success.
summary
This article was written and redis database initialization, and canhttps://github.com/TomatoMr/awesomeframework find today's code. To be continued ......
I welcome the attention of the public number: onepunchgo, give me a shout.