Skip to content

Commit

Permalink
Merge pull request #1078 from Fenny/master
Browse files Browse the repository at this point in the history
📦 introduce ListenTLS
  • Loading branch information
Fenny authored Dec 16, 2020
2 parents d2903cb + 769645a commit 3f89490
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package fiber

import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -31,7 +33,7 @@ import (
)

// Version of current fiber package
const Version = "2.2.5"
const Version = "2.3.0"

// Handler defines a function to serve HTTP requests.
type Handler = func(*Ctx) error
Expand Down Expand Up @@ -540,8 +542,7 @@ func (app *App) Listener(ln net.Listener) error {
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), false, "")
}

// TODO: Detect TLS
// Start listening
return app.server.Serve(ln)
}

Expand All @@ -567,6 +568,44 @@ func (app *App) Listen(addr string) error {
return app.server.Serve(ln)
}

// ListenTLS serves HTTPs requests from the given addr.
// certFile and keyFile are the paths to TLS certificate and key file.

// app.Listen(":8080", "./cert.pem", "./cert.key")
// app.Listen(":8080", "./cert.pem", "./cert.key")
func (app *App) ListenTLS(addr, certFile, keyFile string) error {
// Check for valid cert/key path
if len(certFile) == 0 && len(keyFile) == 0 {
return errors.New("tls: provide a valid cert or key path")
}
// Prefork is supported
if app.config.Prefork {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %s", certFile, keyFile, err)
}
config := &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
Certificates: []tls.Certificate{
cert,
},
}
return app.prefork(addr, config)
}
// Setup listener
ln, err := net.Listen("tcp4", addr)
if err != nil {
return err
}
// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), true, "")
}
// Start listening
return app.server.ServeTLS(ln, certFile, keyFile)
}

// Config returns the app config as value ( read-only ).
func (app *App) Config() Config {
return app.config
Expand Down

0 comments on commit 3f89490

Please sign in to comment.