github.com/letsencrypt/boulder@v0.20251208.0/web/server.go (about)

     1  package web
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"time"
     9  
    10  	blog "github.com/letsencrypt/boulder/log"
    11  )
    12  
    13  type errorWriter struct {
    14  	blog.Logger
    15  }
    16  
    17  func (ew errorWriter) Write(p []byte) (n int, err error) {
    18  	// log.Logger will append a newline to all messages before calling
    19  	// Write. Our log checksum checker doesn't like newlines, because
    20  	// syslog will strip them out so the calculated checksums will
    21  	// differ. So that we don't hit this corner case for every line
    22  	// logged from inside net/http.Server we strip the newline before
    23  	// we get to the checksum generator.
    24  	p = bytes.TrimRight(p, "\n")
    25  	ew.Logger.Err(fmt.Sprintf("net/http.Server: %s", string(p)))
    26  	return
    27  }
    28  
    29  // NewServer returns an http.Server which will listen on the given address, when
    30  // started, for each path in the handler. Errors are sent to the given logger.
    31  func NewServer(listenAddr string, handler http.Handler, logger blog.Logger) http.Server {
    32  	return http.Server{
    33  		ReadTimeout:  30 * time.Second,
    34  		WriteTimeout: 120 * time.Second,
    35  		IdleTimeout:  120 * time.Second,
    36  		Addr:         listenAddr,
    37  		ErrorLog:     log.New(errorWriter{logger}, "", 0),
    38  		Handler:      handler,
    39  	}
    40  }