httpservice新增ManualStart开关,支持手动开始监听

This commit is contained in:
orgin
2022-10-24 11:03:32 +08:00
parent b87a78c85b
commit c3484e9d5b
2 changed files with 29 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package network
import ( import (
"crypto/tls" "crypto/tls"
"errors"
"github.com/duanhf2012/origin/log" "github.com/duanhf2012/origin/log"
"net/http" "net/http"
"time" "time"
@@ -37,6 +38,10 @@ func (slf *HttpServer) Start() {
} }
func (slf *HttpServer) startListen() error { func (slf *HttpServer) startListen() error {
if slf.httpServer != nil {
return errors.New("Duplicate start not allowed")
}
var tlsCaList []tls.Certificate var tlsCaList []tls.Certificate
var tlsConfig *tls.Config var tlsConfig *tls.Config
for _, caFile := range slf.caFileList { for _, caFile := range slf.caFileList {

View File

@@ -93,6 +93,7 @@ type HttpService struct {
listenAddr string listenAddr string
corsHeader *CORSHeader corsHeader *CORSHeader
processTimeout time.Duration processTimeout time.Duration
manualStart bool
} }
type HttpFiltrate func(session *HttpSession) bool //true is pass type HttpFiltrate func(session *HttpSession) bool //true is pass
@@ -347,30 +348,36 @@ func (httpService *HttpService) OnInit() error {
if iConfig == nil { if iConfig == nil {
return fmt.Errorf("%s service config is error!", httpService.GetName()) return fmt.Errorf("%s service config is error!", httpService.GetName())
} }
tcpCfg := iConfig.(map[string]interface{}) httpCfg := iConfig.(map[string]interface{})
addr, ok := tcpCfg["ListenAddr"] addr, ok := httpCfg["ListenAddr"]
if ok == false { if ok == false {
return fmt.Errorf("%s service config is error!", httpService.GetName()) return fmt.Errorf("%s service config is error!", httpService.GetName())
} }
var readTimeout time.Duration = DefaultReadTimeout var readTimeout time.Duration = DefaultReadTimeout
var writeTimeout time.Duration = DefaultWriteTimeout var writeTimeout time.Duration = DefaultWriteTimeout
if cfgRead, ok := tcpCfg["ReadTimeout"]; ok == true { if cfgRead, ok := httpCfg["ReadTimeout"]; ok == true {
readTimeout = time.Duration(cfgRead.(float64)) * time.Millisecond readTimeout = time.Duration(cfgRead.(float64)) * time.Millisecond
} }
if cfgWrite, ok := tcpCfg["WriteTimeout"]; ok == true { if cfgWrite, ok := httpCfg["WriteTimeout"]; ok == true {
writeTimeout = time.Duration(cfgWrite.(float64)) * time.Millisecond writeTimeout = time.Duration(cfgWrite.(float64)) * time.Millisecond
} }
if manualStart, ok := httpCfg["ManualStart"]; ok == true {
httpService.manualStart = manualStart.(bool)
}else{
manualStart =false
}
httpService.processTimeout = DefaultProcessTimeout httpService.processTimeout = DefaultProcessTimeout
if cfgProcessTimeout, ok := tcpCfg["ProcessTimeout"]; ok == true { if cfgProcessTimeout, ok := httpCfg["ProcessTimeout"]; ok == true {
httpService.processTimeout = time.Duration(cfgProcessTimeout.(float64)) * time.Millisecond httpService.processTimeout = time.Duration(cfgProcessTimeout.(float64)) * time.Millisecond
} }
httpService.httpServer.Init(addr.(string), httpService, readTimeout, writeTimeout) httpService.httpServer.Init(addr.(string), httpService, readTimeout, writeTimeout)
//Set CAFile //Set CAFile
caFileList, ok := tcpCfg["CAFile"] caFileList, ok := httpCfg["CAFile"]
if ok == false { if ok == false {
return nil return nil
} }
@@ -395,10 +402,20 @@ func (httpService *HttpService) OnInit() error {
} }
} }
httpService.httpServer.SetCAFile(caFile) httpService.httpServer.SetCAFile(caFile)
httpService.httpServer.Start()
if httpService.manualStart == false {
httpService.httpServer.Start()
}
return nil return nil
} }
func (httpService *HttpService) StartListen() {
if httpService.manualStart {
httpService.httpServer.Start()
}
}
func (httpService *HttpService) SetAllowCORS(corsHeader *CORSHeader) { func (httpService *HttpService) SetAllowCORS(corsHeader *CORSHeader) {
httpService.corsHeader = corsHeader httpService.corsHeader = corsHeader
} }