From c3484e9d5be354d42b8dfa5314976bfe758cf700 Mon Sep 17 00:00:00 2001 From: orgin Date: Mon, 24 Oct 2022 11:03:32 +0800 Subject: [PATCH] =?UTF-8?q?httpservice=E6=96=B0=E5=A2=9EManualStart?= =?UTF-8?q?=E5=BC=80=E5=85=B3=EF=BC=8C=E6=94=AF=E6=8C=81=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network/http_server.go | 5 +++++ sysservice/httpservice/httpservice.go | 31 +++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/network/http_server.go b/network/http_server.go index a9da037..f22b4af 100644 --- a/network/http_server.go +++ b/network/http_server.go @@ -2,6 +2,7 @@ package network import ( "crypto/tls" + "errors" "github.com/duanhf2012/origin/log" "net/http" "time" @@ -37,6 +38,10 @@ func (slf *HttpServer) Start() { } func (slf *HttpServer) startListen() error { + if slf.httpServer != nil { + return errors.New("Duplicate start not allowed") + } + var tlsCaList []tls.Certificate var tlsConfig *tls.Config for _, caFile := range slf.caFileList { diff --git a/sysservice/httpservice/httpservice.go b/sysservice/httpservice/httpservice.go index a3c755d..be1a1ef 100644 --- a/sysservice/httpservice/httpservice.go +++ b/sysservice/httpservice/httpservice.go @@ -93,6 +93,7 @@ type HttpService struct { listenAddr string corsHeader *CORSHeader processTimeout time.Duration + manualStart bool } type HttpFiltrate func(session *HttpSession) bool //true is pass @@ -347,30 +348,36 @@ func (httpService *HttpService) OnInit() error { if iConfig == nil { return fmt.Errorf("%s service config is error!", httpService.GetName()) } - tcpCfg := iConfig.(map[string]interface{}) - addr, ok := tcpCfg["ListenAddr"] + httpCfg := iConfig.(map[string]interface{}) + addr, ok := httpCfg["ListenAddr"] if ok == false { return fmt.Errorf("%s service config is error!", httpService.GetName()) } var readTimeout time.Duration = DefaultReadTimeout 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 } - if cfgWrite, ok := tcpCfg["WriteTimeout"]; ok == true { + if cfgWrite, ok := httpCfg["WriteTimeout"]; ok == true { writeTimeout = time.Duration(cfgWrite.(float64)) * time.Millisecond } + if manualStart, ok := httpCfg["ManualStart"]; ok == true { + httpService.manualStart = manualStart.(bool) + }else{ + manualStart =false + } + 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.httpServer.Init(addr.(string), httpService, readTimeout, writeTimeout) //Set CAFile - caFileList, ok := tcpCfg["CAFile"] + caFileList, ok := httpCfg["CAFile"] if ok == false { return nil } @@ -395,10 +402,20 @@ func (httpService *HttpService) OnInit() error { } } httpService.httpServer.SetCAFile(caFile) - httpService.httpServer.Start() + + if httpService.manualStart == false { + httpService.httpServer.Start() + } + return nil } +func (httpService *HttpService) StartListen() { + if httpService.manualStart { + httpService.httpServer.Start() + } +} + func (httpService *HttpService) SetAllowCORS(corsHeader *CORSHeader) { httpService.corsHeader = corsHeader }