Merge: * web: log error messages from HTTP server as "debug"

Close #1167

* commit 'ee690e3e327f7e456de476f84e359faf52229b21':
  * web: log error messages from HTTP server as "debug"
This commit is contained in:
Simon Zolin 2020-05-27 14:30:18 +03:00
commit 5c2ca69018
1 changed files with 21 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package home
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
golog "log"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
@ -38,6 +39,17 @@ type Web struct {
portHTTPS int portHTTPS int
httpServer *http.Server // HTTP module httpServer *http.Server // HTTP module
httpsServer HTTPSServer // HTTPS module httpsServer HTTPSServer // HTTPS module
errLogger *golog.Logger
}
// Proxy between Go's "log" and "golibs/log"
type logWriter struct {
}
// HTTP server calls this function to log an error
func (w *logWriter) Write(p []byte) (int, error) {
log.Debug("Web: %s", string(p))
return 0, nil
} }
// CreateWeb - create module // CreateWeb - create module
@ -47,6 +59,9 @@ func CreateWeb(conf *WebConfig) *Web {
w := Web{} w := Web{}
w.conf = conf w.conf = conf
lw := logWriter{}
w.errLogger = golog.New(&lw, "", 0)
// Initialize and run the admin Web interface // Initialize and run the admin Web interface
box := packr.NewBox("../build/static") box := packr.NewBox("../build/static")
@ -115,7 +130,7 @@ func (web *Web) TLSConfigChanged(tlsConf tlsConfigSettings) {
// Start - start serving HTTP requests // Start - start serving HTTP requests
func (web *Web) Start() { func (web *Web) Start() {
// for https, we have a separate goroutine loop // for https, we have a separate goroutine loop
go web.httpServerLoop() go web.tlsServerLoop()
// this loop is used as an ability to change listening host and/or port // this loop is used as an ability to change listening host and/or port
for !web.httpsServer.shutdown { for !web.httpsServer.shutdown {
@ -124,6 +139,7 @@ func (web *Web) Start() {
// we need to have new instance, because after Shutdown() the Server is not usable // we need to have new instance, because after Shutdown() the Server is not usable
address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.BindPort)) address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.BindPort))
web.httpServer = &http.Server{ web.httpServer = &http.Server{
ErrorLog: web.errLogger,
Addr: address, Addr: address,
} }
err := web.httpServer.ListenAndServe() err := web.httpServer.ListenAndServe()
@ -151,7 +167,7 @@ func (web *Web) Close() {
log.Info("Stopped HTTP server") log.Info("Stopped HTTP server")
} }
func (web *Web) httpServerLoop() { func (web *Web) tlsServerLoop() {
for { for {
web.httpsServer.cond.L.Lock() web.httpsServer.cond.L.Lock()
if web.httpsServer.shutdown { if web.httpsServer.shutdown {
@ -173,6 +189,7 @@ func (web *Web) httpServerLoop() {
// prepare HTTPS server // prepare HTTPS server
address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.PortHTTPS)) address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.PortHTTPS))
web.httpsServer.server = &http.Server{ web.httpsServer.server = &http.Server{
ErrorLog: web.errLogger,
Addr: address, Addr: address,
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
Certificates: []tls.Certificate{web.httpsServer.cert}, Certificates: []tls.Certificate{web.httpsServer.cert},