mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-15 16:07:51 +03:00
Added ListenAndServe and ListenAndServeTLS helper functions to Server
This commit is contained in:
@@ -2,6 +2,7 @@ package fasthttp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -277,6 +278,33 @@ func (ctx *RequestCtx) TimeoutError(msg string) {
|
||||
ctx.timeoutErrMsg = msg
|
||||
}
|
||||
|
||||
// ListenAndServe serves HTTP requests from the given TCP addr.
|
||||
func (s *Server) ListenAndServe(addr string) error {
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Serve(ln)
|
||||
}
|
||||
|
||||
// ListenAndServeTLS serves HTTPS requests from the given TCP addr.
|
||||
//
|
||||
// certFile and keyFile are paths to TLS certificate and key files.
|
||||
func (s *Server) ListenAndServeTLS(addr, certFile, keyFile string) error {
|
||||
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tlsConfig := &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
ln, err := tls.Listen("tcp", addr, tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Serve(ln)
|
||||
}
|
||||
|
||||
// Default concurrency used by Server.Serve().
|
||||
const DefaultConcurrency = 256 * 1024
|
||||
|
||||
|
||||
Reference in New Issue
Block a user