Beispiel #1
0
func (this *App) Run(config *AppConfig, routerFunc func(string) ControllerInterface) {
	this.config = config

	if logger, err := log.NewDataLogger(this.config.LogPath, "access", log.TruncateHour, log.LevelInfo, 1000); err != nil {
		panic(err)
	} else {
		this.accessLogger = logger
	}

	if logger, err := log.NewDataLogger(this.config.LogPath, "error", log.TruncateImmediately, log.LevelError, 1000); err != nil {
		panic(err)
	} else {
		this.appLogger = logger
	}

	if cache, err := cache.NewCache(config.CacheAddress, config.CachePassword, config.CacheDB); err != nil {
		this.appLogger.Fatal("cache init error %v", err)
	} else {
		this.cache = cache
	}

	orm, ormerr := orm.NewOrm(
		config.DbType,
		config.DbHost,
		config.DbUser,
		config.DbPassword,
		config.DbName,
		config.DbPort,
	)

	if ormerr != nil {
		this.appLogger.Fatal("orm init error %v", ormerr)
	}
	this.orm = orm

	runtime.GOMAXPROCS(runtime.NumCPU())

	toolkit.SignalWatchRegister(this.quit, os.Kill, os.Interrupt, syscall.SIGTERM)
	toolkit.SignalWatchRun()
	//init log for app

	listen, _ := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", config.Port))

	if routerFunc != nil {
		this.Router.SetRouterFunc(routerFunc)
	}
	err := http.Serve(listen, this)

	if err != nil {
		this.appLogger.Fatal("%v", err)
	}
}
Beispiel #2
0
func (this *App) createControllerLogger(name string) *log.DataLogger {
	logger, err := log.NewDataLogger(this.config.LogPath, name, log.TruncateHour, log.LevelInfo, 1000)
	if err != nil {
		this.appLogger.Fatal("%v", err)
	}
	return logger
}