Beispiel #1
0
// Run beego application.
func (app *App) Run() {
	addr := BConfig.Listen.HTTPAddr

	if BConfig.Listen.HTTPPort != 0 {
		addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPAddr, BConfig.Listen.HTTPPort)
	}

	var (
		err        error
		l          net.Listener
		endRunning = make(chan bool, 1)
	)

	// run cgi server
	if BConfig.Listen.EnableFcgi {
		if BConfig.Listen.EnableStdIo {
			if err = fcgi.Serve(nil, app.Handlers); err == nil { // standard I/O
				logs.Info("Use FCGI via standard I/O")
			} else {
				logs.Critical("Cannot use FCGI via standard I/O", err)
			}
			return
		}
		if BConfig.Listen.HTTPPort == 0 {
			// remove the Socket file before start
			if utils.FileExists(addr) {
				os.Remove(addr)
			}
			l, err = net.Listen("unix", addr)
		} else {
			l, err = net.Listen("tcp", addr)
		}
		if err != nil {
			logs.Critical("Listen: ", err)
		}
		if err = fcgi.Serve(l, app.Handlers); err != nil {
			logs.Critical("fcgi.Serve: ", err)
		}
		return
	}

	app.Server.Handler = app.Handlers
	app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
	app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
	app.Server.ErrorLog = logs.GetLogger("HTTP")

	// run graceful mode
	if BConfig.Listen.Graceful {
		httpsAddr := BConfig.Listen.HTTPSAddr
		app.Server.Addr = httpsAddr
		if BConfig.Listen.EnableHTTPS {
			go func() {
				time.Sleep(20 * time.Microsecond)
				if BConfig.Listen.HTTPSPort != 0 {
					httpsAddr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
					app.Server.Addr = httpsAddr
				}
				server := grace.NewServer(httpsAddr, app.Handlers)
				server.Server.ReadTimeout = app.Server.ReadTimeout
				server.Server.WriteTimeout = app.Server.WriteTimeout
				if err := server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
					logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
					time.Sleep(100 * time.Microsecond)
					endRunning <- true
				}
			}()
		}
		if BConfig.Listen.EnableHTTP {
			go func() {
				server := grace.NewServer(addr, app.Handlers)
				server.Server.ReadTimeout = app.Server.ReadTimeout
				server.Server.WriteTimeout = app.Server.WriteTimeout
				if BConfig.Listen.ListenTCP4 {
					server.Network = "tcp4"
				}
				if err := server.ListenAndServe(); err != nil {
					logs.Critical("ListenAndServe: ", err, fmt.Sprintf("%d", os.Getpid()))
					time.Sleep(100 * time.Microsecond)
					endRunning <- true
				}
			}()
		}
		<-endRunning
		return
	}

	// run normal mode
	if BConfig.Listen.EnableHTTPS {
		go func() {
			time.Sleep(20 * time.Microsecond)
			if BConfig.Listen.HTTPSPort != 0 {
				app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
			} else if BConfig.Listen.EnableHTTP {
				BeeLogger.Info("Start https server error, confict with http.Please reset https port")
				return
			}
			logs.Info("https server Running on https://%s", app.Server.Addr)
			if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
				logs.Critical("ListenAndServeTLS: ", err)
				time.Sleep(100 * time.Microsecond)
				endRunning <- true
			}
		}()
	}
	if BConfig.Listen.EnableHTTP {
		go func() {
			app.Server.Addr = addr
			logs.Info("http server Running on http://%s", app.Server.Addr)
			if BConfig.Listen.ListenTCP4 {
				ln, err := net.Listen("tcp4", app.Server.Addr)
				if err != nil {
					logs.Critical("ListenAndServe: ", err)
					time.Sleep(100 * time.Microsecond)
					endRunning <- true
					return
				}
				if err = app.Server.Serve(ln); err != nil {
					logs.Critical("ListenAndServe: ", err)
					time.Sleep(100 * time.Microsecond)
					endRunning <- true
					return
				}
			} else {
				if err := app.Server.ListenAndServe(); err != nil {
					logs.Critical("ListenAndServe: ", err)
					time.Sleep(100 * time.Microsecond)
					endRunning <- true
				}
			}
		}()
	}
	<-endRunning
}
Beispiel #2
0
// Run beego application.
func (app *App) Run() {
	addr := HttpAddr

	if HttpPort != 0 {
		addr = fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
	}

	var (
		err error
		l   net.Listener
	)
	endRunning := make(chan bool, 1)

	if UseFcgi {
		if UseStdIo {
			err = fcgi.Serve(nil, app.Handlers) // standard I/O
			if err == nil {
				BeeLogger.Info("Use FCGI via standard I/O")
			} else {
				BeeLogger.Info("Cannot use FCGI via standard I/O", err)
			}
		} else {
			if HttpPort == 0 {
				// remove the Socket file before start
				if utils.FileExists(addr) {
					os.Remove(addr)
				}
				l, err = net.Listen("unix", addr)
			} else {
				l, err = net.Listen("tcp", addr)
			}
			if err != nil {
				BeeLogger.Critical("Listen: ", err)
			}
			err = fcgi.Serve(l, app.Handlers)
		}
	} else {
		if Graceful {
			app.Server.Addr = addr
			app.Server.Handler = app.Handlers
			app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
			app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
			if EnableHttpTLS {
				go func() {
					time.Sleep(20 * time.Microsecond)
					if HttpsPort != 0 {
						addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
						app.Server.Addr = addr
					}
					server := grace.NewServer(addr, app.Handlers)
					server.Server = app.Server
					err := server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
					if err != nil {
						BeeLogger.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
						time.Sleep(100 * time.Microsecond)
						endRunning <- true
					}
				}()
			}
			if EnableHttpListen {
				go func() {
					server := grace.NewServer(addr, app.Handlers)
					server.Server = app.Server
					if ListenTCP4 && HttpAddr == "" {
						server.Network = "tcp4"
					}
					err := server.ListenAndServe()
					if err != nil {
						BeeLogger.Critical("ListenAndServe: ", err, fmt.Sprintf("%d", os.Getpid()))
						time.Sleep(100 * time.Microsecond)
						endRunning <- true
					}
				}()
			}
		} else {
			app.Server.Addr = addr
			app.Server.Handler = app.Handlers
			app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
			app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second

			if EnableHttpTLS {
				go func() {
					time.Sleep(20 * time.Microsecond)
					if HttpsPort != 0 {
						app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
					}
					BeeLogger.Info("https server Running on %s", app.Server.Addr)
					err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
					if err != nil {
						BeeLogger.Critical("ListenAndServeTLS: ", err)
						time.Sleep(100 * time.Microsecond)
						endRunning <- true
					}
				}()
			}

			if EnableHttpListen {
				go func() {
					app.Server.Addr = addr
					BeeLogger.Info("http server Running on %s", app.Server.Addr)
					if ListenTCP4 && HttpAddr == "" {
						ln, err := net.Listen("tcp4", app.Server.Addr)
						if err != nil {
							BeeLogger.Critical("ListenAndServe: ", err)
							time.Sleep(100 * time.Microsecond)
							endRunning <- true
							return
						}
						err = app.Server.Serve(ln)
						if err != nil {
							BeeLogger.Critical("ListenAndServe: ", err)
							time.Sleep(100 * time.Microsecond)
							endRunning <- true
							return
						}
					} else {
						err := app.Server.ListenAndServe()
						if err != nil {
							BeeLogger.Critical("ListenAndServe: ", err)
							time.Sleep(100 * time.Microsecond)
							endRunning <- true
						}
					}
				}()
			}
		}

	}
	<-endRunning
}