Exemple #1
0
//	new context
func newContext(server *LeafveinServer, rw http.ResponseWriter, req *http.Request, respWirteCompress bool) *HttpContext {
	var outWrite io.Writer
	var err error

	acceptEncoding := SFStringsUtil.ToLower(req.Header.Get("Accept-Encoding"))
	encoding := "none"

	if respWirteCompress && 0 <= strings.Index(acceptEncoding, "gzip") {
		encoding = "gzip"
		outWrite, err = gzip.NewWriterLevel(rw, gzip.BestSpeed)
	} else if respWirteCompress && 0 <= strings.Index(acceptEncoding, "deflate") {
		encoding = "deflate"
		outWrite, err = flate.NewWriter(rw, flate.BestSpeed)
	} else {
		outWrite = rw
	}

	if nil != err {
		encoding = "none"
		outWrite = rw
	}

	context := new(HttpContext)
	context.lvServer = server
	context.RespWrite = rw
	context.Request = req
	context.reqBody = nil
	context.session = nil
	context.contentEncoding = encoding
	context.comperssWriter = outWrite
	context.isCloseWriter = false
	context.isParseForm = false
	context.formparamErr = nil
	context.formparams = nil

	//	TODO 有待修改,看如何获取scheme
	if nil != req.TLS {
		context.reqScheme = URI_SCHEME_HTTPS
	} else {
		context.reqScheme = URI_SCHEME_HTTP
	}

	return context
}
Exemple #2
0
/**
 *	start leafvein server
 *
 *	@param startName "DevelStart" or "Start"
 *	@param goroutine
 */
func (lv *LeafveinServer) start(startName string, goroutine bool) {
	if lv.IsStart() {
		return
	}
	var runFunc func() = nil
	var defaultServer *http.Server = nil

	//	start info
	logInfo := fmt.Sprintf("(%v)Leafvein %v...\n", lv.AppName(), startName)

	if !lv.parseRouter(&logInfo, startName) {
		return
	}

	if 0 >= lv.port {
		lv.port = 8080
	}
	addr := fmt.Sprintf("%s:%d", lv.addr, lv.port)

	//	由于addr设置为127.0.0.1的时候就只能允许内网进行http://localhost:(port)/进行访问,本机IP访问不了。
	//	为了友好的显示,如果addr设置为空的时候允许IP或localhost进行访问做了特别的显示除了(http://0.0.0.0:8080)
	logAddr := ""
	logTLSAddr := ""
	if strings.Index(addr, ":") == 0 {
		logAddr = "http://0.0.0.0" + addr
		logTLSAddr = "https://0.0.0.0"
	} else {
		logAddr = "http://" + addr
		logTLSAddr = "https://" + lv.addr
	}

	if 0 == len(lv.tlsCertPath) || 0 == len(lv.tlsKeyPath) || !lv.tlsAloneRun {
		// default server and listen
		defaultServer = &http.Server{
			Addr:         addr,
			Handler:      lv,
			ReadTimeout:  time.Duration(lv.serverTimeout) * time.Second,
			WriteTimeout: time.Duration(lv.serverTimeout) * time.Second,
		}

		netListen, err := net.Listen("tcp", addr)
		if err != nil {
			lv.log.Fatal("(%v) %v Listen: %v \n", lv.appName, startName, err)
			return
		}
		lv.listener = &leafveinListener{netListen.(*net.TCPListener), DEFAULT_KEEP_ALIVE_PERIOD, nil}
	}

	//	handle tls
	if 0 != len(lv.tlsCertPath) && 0 != len(lv.tlsKeyPath) {
		// see http://golang.org/src/pkg/net/http/server.go?#L1823

		certFullpath := lv.tlsCertPath
		if !filepath.IsAbs(certFullpath) {
			certFullpath = filepath.Join(SFFileManager.GetExecDir(), lv.tlsCertPath)
		}
		keyFullpath := lv.tlsKeyPath
		if !filepath.IsAbs(keyFullpath) {
			keyFullpath = filepath.Join(SFFileManager.GetExecDir(), lv.tlsKeyPath)
		}

		certPEMBlock, err := ioutil.ReadFile(certFullpath)
		if nil != err {
			lv.log.Fatal("(%v) %v Serve Read SSL cert file: %v \n", lv.appName, startName, err)
			return
		}
		keyPEMBlock, err := ioutil.ReadFile(keyFullpath)
		if nil != err {
			lv.log.Fatal("(%v) %v Serve Read SSL key file: %v \n", lv.appName, startName, err)
			return
		}

		if 0 == lv.tlsPort {
			lv.tlsPort = lv.port + 1
		}

		tlsServer := &http.Server{
			Addr:         fmt.Sprintf("%s:%d", lv.addr, lv.tlsPort),
			Handler:      lv,
			ReadTimeout:  time.Duration(lv.serverTimeout) * time.Second,
			WriteTimeout: time.Duration(lv.serverTimeout) * time.Second,
		}

		tlsServer.TLSConfig = new(tls.Config)
		tlsServer.TLSConfig.NextProtos = []string{"http/1.1"}
		tlsServer.TLSConfig.Certificates = make([]tls.Certificate, 1)
		tlsServer.TLSConfig.Certificates[0], err = tls.X509KeyPair(certPEMBlock, keyPEMBlock)
		if nil != err {
			lv.log.Fatal("(%v) %v TLS Serve: %v \n", lv.appName, startName, err)
			return
		}

		tlsListen, err := net.Listen("tcp", tlsServer.Addr)
		if err != nil {
			lv.log.Fatal("(%v) %v TLS Listen: %v \n", lv.appName, startName, err)
			return
		}

		//	http start
		if nil != lv.listener && nil != defaultServer {
			logInfo += fmt.Sprintf("(%v) %v to Listen on %v. Go to %v \n", lv.appName, startName, lv.port, logAddr)
			go func() {
				err = defaultServer.Serve(lv.listener)
				if err != nil {
					lv.log.Fatal("(%v) %v Serve: %v \n", lv.appName, startName, err)
				}
			}()
		}

		//	tls start
		lv.tlsListener = &leafveinListener{tlsListen.(*net.TCPListener), DEFAULT_KEEP_ALIVE_PERIOD, tlsServer.TLSConfig}

		logInfo += fmt.Sprintf("(%v) %v to TLS Listen on %v. Go to %v:%v \n", lv.appName, startName, lv.tlsPort, logTLSAddr, lv.tlsPort)
		lv.log.Info(logInfo)

		runFunc = func() {
			err = tlsServer.Serve(lv.tlsListener)
			if err != nil {
				lv.log.Fatal("(%v) %v TLS Serve: %v \n", lv.appName, startName, err)
			}
			lv.isStart = false
			if nil != _serverWaitGroup {
				_serverWaitGroup.Done()
			}
		}

	} else {

		if nil == defaultServer || nil == lv.listener {
			lv.log.Fatal("default server or default listener init nil. port is configured correctly?")
			return
		}

		logInfo += fmt.Sprintf("(%v) %v to Listen on %v. Go to %v \n", lv.appName, startName, lv.port, logAddr)
		lv.log.Info(logInfo)

		runFunc = func() {
			err := defaultServer.Serve(lv.listener)
			if err != nil {
				lv.log.Fatal("(%v) %v Serve: %v \n", lv.appName, startName, err)
			}
			lv.isStart = false
			if nil != _serverWaitGroup {
				_serverWaitGroup.Done()
			}
		}

	}

	//	start server
	lv.isStart = true
	if nil != _serverWaitGroup {
		_serverWaitGroup.Add(1)
	}

	if goroutine {
		go runFunc()
	} else {
		runFunc()
	}
}