예제 #1
0
파일: db.go 프로젝트: robinmin/polaris
// InitDB initialize a connection to specified database
func (conf *DBConfig) InitDB() *DBEngine {
	// get driver
	dialect, driver := dialectAndDriver(conf.Type)
	conf.Driver = driver

	strDSN := conf.String()
	if strDSN == "" {
		log.Error("Invalid DSN has been provided : " + strDSN)
		return nil
	}
	// log.Debug("[" + driver + "] ==> " + strDSN)
	// open connection
	db, err := sql.Open(driver, strDSN)
	if err != nil {
		log.Error("Error connecting to db: " + err.Error())
		return nil
	}
	// fmt.Printf("db = %#v\n", db)
	// dbEngine := &gorp.DbMap{Db: db, Dialect: dialect}
	dbEngine := &DBEngine{DbMap: gorp.DbMap{Db: db, Dialect: dialect}}
	// open SQL log or not
	if conf.Verbose {
		if len(conf.LogFile) > 0 {
			sql_log, err := os.OpenFile(conf.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
			if err == nil {
				dbEngine.TraceOn("[SQL]", stdlog.New(sql_log, "sql", stdlog.Lmicroseconds))
			} else {
				dbEngine.TraceOn("[SQL]", log.GetLogger("file"))
			}
		} else {
			dbEngine.TraceOn("[SQL]", log.GetLogger("file"))
		}
	} else {
		dbEngine.TraceOff()
	}

	conf._ready = true
	return dbEngine
}
예제 #2
0
파일: polaris.go 프로젝트: robinmin/polaris
func (app *PolarisApplication) RunApp() bool {
	config := app.Config.GetBasicConfig()

	host := os.Getenv("HOST")
	// if len(host) <= 0 {
	// 	host = os.Getenv("COMPUTERNAME")
	// }
	port := config.Port
	log.Info("listening on " + host + ":" + strconv.Itoa(port))
	lgr := log.GetLogger("stdout")
	if lgr != nil {
		lgr.Fatalln(http.ListenAndServe(host+":"+strconv.Itoa(port), app))
		return true
	}
	return false
}