func InitMysql() { userName := beego.AppConfig.String("mysql_user_name") userPass := beego.AppConfig.String("mysql_user_pass") ip := beego.AppConfig.String("mysql_ip") port, err := beego.AppConfig.Int("mysql_port") dbName := beego.AppConfig.String("mysql_db_name") log.Greenf("userName:%v,userPass:%v,ip:%v,port:%v,dbName:%v \n", userName, userPass, ip, port, dbName) if nil != err { port = 3306 } orm.Debug = true driver_url := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", userName, userPass, ip, port, dbName) log.Greenf("driver_url:%v\n ", driver_url) orm.RegisterDataBase("default", "mysql", driver_url) }
func initDbEngine() { config := global.Config engineType, err := config.GetString("_", "engine") // if engine not define, it's install mode, skip database engine initial if err != nil || global.Install { return } if engineType == "mysql" { username := config.GetStringDefault("mysql", "username", "root") password := config.GetStringDefault("mysql", "password", "") host := config.GetStringDefault("mysql", "host", "127.0.0.1") port := config.GetIntDefault("mysql", "port", 3306) dbName := "elapen" dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", username, password, host, port, dbName) engine, err = xorm.NewEngine("mysql", dsn) if err != nil { log.Redln(err) } else { log.Greenf("use mysql ") log.Bluef("%s\n", dsn) } } else { dsn := config.GetStringDefault("sqlite", "path", "./data.sqlite") engine, err = xorm.NewEngine("sqlite3", dsn) if err != nil { log.Redln(err) } else { log.Greenf("use sqlite ") log.Bluef("%s\n", dsn) } } runMode := config.GetStringDefault("_", "runmode", "prod") if runMode == "dev" { global.Engine.ShowSQL(true) } global.Engine = engine }
func responseLog(ctx Context) { content := "[%s] %s: complete %s %s %d\n" switch ctx.GetResponseWriter().Status() { case 301, 302: log.Bluef(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status()) case 304: log.Greenf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status()) case 401, 403: log.Yellowf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status()) case 404: log.Redf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status()) case 500: log.Pinkf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status()) default: log.Printf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status()) } }
func InitRedis() { cacheConfig := beego.AppConfig.String("cache") log.Greenf("cacheConfig:%v \n", cacheConfig) var cc cache.Cache if "redis" == cacheConfig { var err error defer utils.Recover("redis init falure") cc, err = cache.NewCache("redis", `{"conn":"`+beego.AppConfig.String("redis_host")+`"}`) if err != nil { log.Redf("%v", err) } cache, ok := cc.(*caches.MyRedisCache) if ok { redis = cache } else { log.Redf("parse cache to MyRedisCache failure !") } } }