Beispiel #1
0
func (this *Server) Start() error {
	logger.Debug("serverConfig is %#v", this.config)
	this.router.matcher.sort()
	conf := this.config
	server := &http.Server{Handler: http.HandlerFunc(this.serverHttpHandler), MaxHeaderBytes: conf.MaxHeaderBytes, TLSConfig: conf.TLSConfig}
	if conf.ReadTimeout > 0 {
		server.ReadTimeout = conf.ReadTimeout
	}
	if conf.WriteTimeout > 0 {
		server.WriteTimeout = conf.WriteTimeout
	}
	http2.ConfigureServer(server, &http2.Server{})
	var err error
	this.listener, err = net.Listen("tcp", conf.serverAddr)
	if err != nil {
		return errors.New(logger.Error("listen [%s] fail:%s", conf.serverAddr, err))
	}
	logger.Info("start HttpServer :%s", conf.serverAddr)
	keepAliveListrener := tcpKeepAliveListener{this.listener.(*net.TCPListener)}
	if conf.TLSConfig != nil {
		conf.TLSConfig.NextProtos = append(conf.TLSConfig.NextProtos, "http/1.1")
		go server.Serve(tls.NewListener(keepAliveListrener, conf.TLSConfig))
	} else {
		go server.Serve(keepAliveListrener)
	}
	return nil
}
Beispiel #2
0
func (this *Server) Stop() {
	if this.listener != nil {
		logger.Debug("Close Http Server")
		this.listener.Close()
	}
}