123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package boot
- import (
- "fmt"
- "net/http"
- "github.com/gin-gonic/gin"
- "github.com/gomodule/redigo/redis"
- "github.com/spf13/viper"
- "go.uber.org/zap"
- "shopperthree_manage_v2/app/router"
- "shopperthree_manage_v2/client/mysql"
- "shopperthree_manage_v2/client/sredis"
- "shopperthree_manage_v2/logger"
- )
- func Run() {
- var err error
- StoreInit()
- err = InitModel()
- if err != nil {
- panic(err)
- }
- StartServer()
- }
- //初始化相关必要持久化服务
- func StoreInit() {
- //连接mysql
- var err error
- // 连接数据库
- _, err = mysql.Dial("mysql", viper.GetString("mysql.address"))
- if err != nil {
- panic(err)
- }
- //初始化redis
- sredis.MasterPool = &redis.Pool{ //实例化一个连接池
- MaxIdle: 16, //最初的连接数量
- // MaxActive:1000000, //最大连接数量
- MaxActive: 0, //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
- IdleTimeout: 300, //连接关闭时间 300秒 (300秒不使用自动关闭)
- Dial: func() (redis.Conn, error) { //要连接的redis数据库
- conn, err := redis.Dial("tcp", viper.GetString("redis.master.address"))
- if err != nil {
- return nil, err
- }
- if viper.GetString("redis.master.password") != "" {
- _, err = conn.Do("AUTH", viper.GetString("redis.master.password"))
- if err != nil {
- return nil, err
- }
- }
- return conn, err
- },
- }
- sredis.SalvePool = &redis.Pool{ //实例化一个连接池
- MaxIdle: 16, //最初的连接数量
- // MaxActive:1000000, //最大连接数量
- MaxActive: 0, //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
- IdleTimeout: 300, //连接关闭时间 300秒 (300秒不使用自动关闭)
- Dial: func() (redis.Conn, error) { //要连接的redis数据库
- conn, err := redis.Dial("tcp", viper.GetString("redis.slave.address"))
- if err != nil {
- return nil, err
- }
- if viper.GetString("redis.slave.password") != "" {
- _, err = conn.Do("AUTH", viper.GetString("redis.slave.password"))
- if err != nil {
- return nil, err
- }
- }
- return conn, err
- },
- }
- }
- //启动服务
- func StartServer() {
- //
- r := gin.Default()
- router.StepRouters(r)
- addr := fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))
- logger.Info(fmt.Sprintf("Http Server has been started. http://%s", addr))
- logger.Fatal("start http server", zap.Error(http.ListenAndServe(addr, r)))
- }
|