Skip to content

Commit

Permalink
Merge: * web: log error messages from HTTP server as "debug"
Browse files Browse the repository at this point in the history
Close AdguardTeam#1167

* commit 'ee690e3e327f7e456de476f84e359faf52229b21':
  * web: log error messages from HTTP server as "debug"
  • Loading branch information
szolin committed May 27, 2020
2 parents 355e634 + ee690e3 commit 5c2ca69
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions home/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package home
import (
"context"
"crypto/tls"
golog "log"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -38,6 +39,17 @@ type Web struct {
portHTTPS int
httpServer *http.Server // HTTP 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
Expand All @@ -47,6 +59,9 @@ func CreateWeb(conf *WebConfig) *Web {
w := Web{}
w.conf = conf

lw := logWriter{}
w.errLogger = golog.New(&lw, "", 0)

// Initialize and run the admin Web interface
box := packr.NewBox("../build/static")

Expand Down Expand Up @@ -115,7 +130,7 @@ func (web *Web) TLSConfigChanged(tlsConf tlsConfigSettings) {
// Start - start serving HTTP requests
func (web *Web) Start() {
// 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
for !web.httpsServer.shutdown {
Expand All @@ -124,7 +139,8 @@ func (web *Web) Start() {
// 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))
web.httpServer = &http.Server{
Addr: address,
ErrorLog: web.errLogger,
Addr: address,
}
err := web.httpServer.ListenAndServe()
if err != http.ErrServerClosed {
Expand All @@ -151,7 +167,7 @@ func (web *Web) Close() {
log.Info("Stopped HTTP server")
}

func (web *Web) httpServerLoop() {
func (web *Web) tlsServerLoop() {
for {
web.httpsServer.cond.L.Lock()
if web.httpsServer.shutdown {
Expand All @@ -173,7 +189,8 @@ func (web *Web) httpServerLoop() {
// prepare HTTPS server
address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.PortHTTPS))
web.httpsServer.server = &http.Server{
Addr: address,
ErrorLog: web.errLogger,
Addr: address,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{web.httpsServer.cert},
MinVersion: tls.VersionTLS12,
Expand Down

0 comments on commit 5c2ca69

Please sign in to comment.