forked from gofiber/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (47 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package main
import (
"crypto/tls"
"log"
"github.com/gofiber/fiber/v2"
"golang.org/x/crypto/acme/autocert"
)
func main() {
// Fiber instance
app := fiber.New()
// Routes
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("This is a secure server 👮")
})
// Let’s Encrypt has rate limits: https://letsencrypt.org/docs/rate-limits/
// It's recommended to use it's staging environment to test the code:
// https://letsencrypt.org/docs/staging-environment/
// Certificate manager
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
// Replace with your domain
HostPolicy: autocert.HostWhitelist("example.com"),
// Folder to store the certificates
Cache: autocert.DirCache("./certs"),
}
// TLS Config
cfg := &tls.Config{
// Get Certificate from Let's Encrypt
GetCertificate: m.GetCertificate,
// By default NextProtos contains the "h2"
// This has to be removed since Fasthttp does not support HTTP/2
// Or it will cause a flood of PRI method logs
// http://webconcepts.info/concepts/http-method/PRI
NextProtos: []string{
"http/1.1", "acme-tls/1",
},
}
ln, err := tls.Listen("tcp", ":443", cfg)
if err != nil {
panic(err)
}
// Start server
log.Fatal(app.Listener(ln))
}