HTTPS with TLS Example
April 30, 2026 ยท View on GitHub
This project demonstrates how to set up an HTTPS server with TLS in a Go application using the Fiber framework.
Prerequisites
Ensure you have the following installed:
- Golang
- Fiber package
- TLS certificates (self-signed or from a trusted CA)
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/https-tls -
Install dependencies:
go get -
Generate a self-signed certificate and key:
mkdir -p certs openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout certs/ssl.key -out certs/ssl.cert \ -subj "/CN=localhost"
Running the Application
-
Start the application:
go run main.go -
Access the application at
https://localhost:443.
Example
Here is an example of how to set up an HTTPS server with TLS in a Fiber application:
package main
import (
"crypto/tls"
"log"
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
return c.SendString(c.Protocol()) // => https
})
// Create tls certificate
cer, err := tls.LoadX509KeyPair("certs/ssl.cert", "certs/ssl.key")
if err != nil {
log.Fatal(err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
// Create custom listener
ln, err := tls.Listen("tcp", ":443", config)
if err != nil {
log.Fatal(err)
}
// Start server with https/ssl enabled on http://localhost:443
log.Fatal(app.Listener(ln))
}