// 启动Web服务 func Run() { runtime.GOMAXPROCS(runtime.NumCPU()); http.HandleFunc("/", loadController) addr := conf.GetValue("http", "addr") port := conf.GetValue("http", "port") str := util.Colorize(fmt.Sprintf("[I] Running on %s:%s", addr, port), "note") log.Printf(str) //log.Printf("[I] Running on %s:%s", addr, port) err := http.ListenAndServe(addr+":"+port, nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
func newRedisPool() *redis.Pool { poolNum, _ := strconv.Atoi(conf.GetValue("pool", "redis")) fmt.Printf("初始化 Redis 连接池,连接数:%d \n", poolNum) return &redis.Pool{ MaxIdle: 80, MaxActive: poolNum, // max number of connections Dial: func() (redis.Conn, error) { host := conf.GetValue("redis", "host") port := conf.GetValue("redis", "port") c, err := redis.Dial("tcp", host+":"+port) //if err != nil { //panic(err.Error()) //} return c, err }, } }
func newMysqlPool() *ConnPool { poolNum, _ := strconv.Atoi(conf.GetValue("pool", "mysql")) fmt.Printf("初始化 Mysql 连接池,连接数:%d \n", poolNum) return &ConnPool{ MaxActive: poolNum, //Dial: func() (*autorc.Conn, error) { Dial: func() (interface{}, error) { //conn := autorc.New("tcp", "", "localhost:3306", "root", "root", "test") //conn.Register("set names utf8") db, err := InitDB() return db, err }, } }
// (读+写)连接数据库+选择数据库 //func InitDB(address, user, pass, name string, logSlowQuery bool, logSlowTime int64) (*DB, error){ func InitDB() (*DB, error) { //fmt.Println("InitDB") host := conf.GetValue("db", "host") port := conf.GetValue("db", "port") user := conf.GetValue("db", "user") pass := conf.GetValue("db", "pass") name := conf.GetValue("db", "name") logSlowQuery, _ := strconv.ParseBool(conf.GetValue("db", "log_slow_query")) logSlowTime, _ := strconv.ParseInt(conf.GetValue("db", "log_slow_time"), 0, 64) address := host + ":" + port //db := new(DB) db := &DB{logSlowQuery: logSlowQuery, logSlowTime: logSlowTime} conn := autorc.New("tcp", "", address, user, pass, name) conn.Register("set names utf8") db.Conn = conn return db, nil }