Exemplo n.º 1
0
func main() {
	var (
		ch         chan bool = make(chan bool)
		confFile   string
		httpPort   int
		socketPort int
		restPort   int
		mode       string //启动模式: h开启http,s开启socket,a开启所有
		debug      bool
		trace      bool
		runDaemon  bool // 运行daemon
		help       bool
		newApp     *core.MainApp
	)

	flag.IntVar(&socketPort, "port2", 1001, "socket server port")
	flag.IntVar(&httpPort, "port", 1002, "web server port")
	flag.IntVar(&restPort, "port3", 1003, "rest api port")
	flag.StringVar(&mode, "mode", "shr", "boot mode.'h'- boot http service,'s'- boot socket service")
	flag.BoolVar(&debug, "debug", false, "enable debug")
	flag.BoolVar(&trace, "trace", false, "enable trace")
	flag.BoolVar(&help, "help", false, "command usage")
	flag.StringVar(&confFile, "conf", "app.conf", "")
	flag.BoolVar(&runDaemon, "d", false, "run daemon")
	flag.Parse()

	if help {
		flag.Usage()
		return
	}

	runtime.GOMAXPROCS(runtime.NumCPU())

	newApp = core.NewMainApp(confFile)
	if !newApp.Init(debug, trace) {
		os.Exit(1)
	}

	go handleSignal(ch)

	if v := newApp.Config().GetInt("server_port"); v != 0 {
		httpPort = v
	}
	if v := newApp.Config().GetInt("socket_port"); v != 0 {
		socketPort = v
	}
	if v := newApp.Config().GetInt("api_service_port"); v != 0 {
		restPort = v
	}

	gof.CurrentApp = newApp
	app.Init(newApp)
	cache.Initialize(storage.NewRedisStorage(newApp.Redis()))
	core.RegisterTypes()

	var booted bool

	if runDaemon {
		go daemon.Run(newApp)
	}

	if strings.Contains(mode, "s") {
		booted = true
		go app.RunSocket(newApp, socketPort, debug, trace)
	}

	if strings.Contains(mode, "h") {
		booted = true
		go app.RunWeb(newApp, httpPort, debug, trace)
	}

	if strings.Contains(mode, "r") {
		booted = true
		go app.RunRestApi(newApp, restPort)
	}

	if booted {
		<-ch
	}
}
Exemplo n.º 2
0
func (this *MainApp) Storage() gof.Storage {
	if this._storage == nil {
		this._storage = storage.NewRedisStorage(this.Redis())
	}
	return this._storage
}
Exemplo n.º 3
0
// 存储,通常用来存储全局的变量。或缓存一些共享的数据。
func (this *HttpApp) Storage() gof.Storage {
	// 使用一个Redis存储数据
	return storage.NewRedisStorage(nil)
}
Exemplo n.º 4
0
func main() {
	var (
		ch        chan bool = make(chan bool)
		confFile  string
		httpPort  int
		restPort  int
		debug     bool
		trace     bool
		runDaemon bool // 运行daemon
		help      bool
		newApp    *core.MainApp
	)

	flag.IntVar(&httpPort, "port", 14190, "web server port")
	flag.IntVar(&restPort, "restport", 14191, "rest api port")
	flag.BoolVar(&debug, "debug", false, "enable debug")
	flag.BoolVar(&trace, "trace", false, "enable trace")
	flag.BoolVar(&help, "help", false, "command usage")
	flag.StringVar(&confFile, "conf", "app.conf", "")
	flag.BoolVar(&runDaemon, "d", false, "run daemon")
	flag.Parse()

	if help {
		flag.Usage()
		return
	}

	log.SetOutput(os.Stdout)
	log.SetFlags(log.LstdFlags | log.Ltime | log.Ldate | log.Lshortfile)

	runtime.GOMAXPROCS(runtime.NumCPU())
	newApp = core.NewMainApp(confFile)
	if !newApp.Init(debug, trace) {
		os.Exit(1)
	}
	fix.CustomFix()
	go fix.SignalNotify(ch)

	if v := newApp.Config().GetInt("server_port"); v != 0 {
		httpPort = v
	}
	if v := newApp.Config().GetInt("api_service_port"); v != 0 {
		restPort = v
	}

	gof.CurrentApp = newApp
	dps.Init(newApp)
	cache.Initialize(storage.NewRedisStorage(newApp.Redis()))
	session.Set(newApp.Storage(), "")

	var booted bool

	if runDaemon {
		go daemon.Run(newApp)
	}

	go app.Run(ch, newApp, fmt.Sprintf(":%d", httpPort)) //运行HTTP

	go restapi.Run(newApp, restPort) // 运行REST API

	if booted {
		<-ch
	}

	os.Exit(1) // 退出
}