Exemplo n.º 1
0
Arquivo: main.go Projeto: 29n/gopush2
func main() {
	var err error

	// parse cmd-line arguments
	flag.Parse()
	// init config
	Conf, err = cfg.New(cfg.ConfFile)
	if err != nil {
		Log.Printf("cfg.New(\"%s\") failed (%s)", cfg.ConfFile, err.Error())
		return
	}

	// init log
	if Conf.Log != "" {
		f, err := os.OpenFile(Conf.Log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
		if err != nil {
			Log.Printf("os.OpenFile(\"%s\", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) failed (%s)", Conf.Log, err.Error())
			return
		}

		defer f.Close()
		Log = log.New(f, "", log.LstdFlags|log.Lshortfile)
	}

	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProcs)
	// create channel
	channel = NewChannelList()
	Log.Printf("gopush2 service start.")
	// start stats
	StartStats()

	if Conf.Protocol == WebsocketProtocol {
		// Start http push service
		if err = StartHttp(); err != nil {
			Log.Printf("StartHttp() failed (%s)", err.Error())
		}
	} else if Conf.Protocol == TCPProtocol {
		// Start http push service
		if err = StartTCP(); err != nil {
			Log.Printf("StartTCP() failed (%s)", err.Error())
		}
	} else {
		Log.Printf("unknown configuration protocol: %d", Conf.Protocol)
	}

	// exit
	Log.Printf("gopush2 service stop.")
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: qmdx/gopush2
func main() {
	var err error

	// parse cmd-line arguments
	flag.Parse()
	// init config
	Conf, err = cfg.New(cfg.ConfFile)
	if err != nil {
		Log.Printf("cfg.New(\"%s\") failed (%s)", cfg.ConfFile, err.Error())
		return
	}

	// init log
	if Conf.Log != "" {
		f, err := os.OpenFile(Conf.Log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
		if err != nil {
			Log.Printf("os.OpenFile(\"%s\", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) failed (%s)", Conf.Log, err.Error())
			return
		}

		defer f.Close()
		Log = log.New(f, "", log.LstdFlags|log.Lshortfile)
	}

	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProcs)

	// set sub handler
	http.Handle("/sub", websocket.Handler(Subscribe))
	if Conf.Debug == 1 {
		http.HandleFunc("/client", Client)
	}

	Log.Printf("gopush2 service start.")
	// pprof
	if Conf.Pprof == 1 {
		if Conf.PprofAddr != Conf.Addr || Conf.PprofPort != Conf.Port {
			go func() {
				profServeMux := http.NewServeMux()
				profServeMux.HandleFunc("/debug/pprof/", pprof.Index)
				profServeMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
				profServeMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
				profServeMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
				err := http.ListenAndServe(fmt.Sprintf("%s:%d", Conf.PprofAddr, Conf.PprofPort), profServeMux)
				if err != nil {
					panic(err)
				}
			}()
		}
	}

	// publish
	if Conf.PubAddr != Conf.Addr || Conf.PubPort != Conf.Port {
		go func() {
			pubServeMux := http.NewServeMux()
			pubServeMux.HandleFunc("/pub", Publish)
			err := http.ListenAndServe(fmt.Sprintf("%s:%d", Conf.PubAddr, Conf.PubPort), pubServeMux)
			if err != nil {
				panic(err)
			}
		}()
	} else {
		http.HandleFunc("/pub", Publish)
	}

	// start listen and pending here
	if err = Listen(Conf.Addr, Conf.Port); err != nil {
		Log.Printf("Listen() failed (%s)", err.Error())
		return
	}

	Log.Printf("gopush2 service stop.")
}