forked from gofiber/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (49 loc) · 1.19 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
57
58
59
60
61
62
63
64
package main
import (
"crypto"
"crypto/tls"
"fmt"
"os"
"log"
"github.com/gofiber/fiber/v2"
"golang.org/x/crypto/pkcs12"
)
func initFiberApp() *fiber.App {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("This page is being served over TLS using a PKCS12 store type!")
})
return app
}
func initTLSConfig(path string, password string) (*tls.Certificate, error) {
pkcs12Data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
key, cert, err := pkcs12.Decode(pkcs12Data, password)
if err != nil {
return nil, err
}
tlsCert := tls.Certificate{
Certificate: [][]byte{cert.Raw},
PrivateKey: key.(crypto.PrivateKey),
Leaf: cert,
}
return &tlsCert, nil
}
func main() {
path := "./security/server.p12"
password := "changeit"
tlsCert, error := initTLSConfig(path, password)
if error != nil {
fmt.Println("Unable to initialize TLS configuration object. Check your configuration and try again. Program will STOP.")
} else {
config := &tls.Config{Certificates: []tls.Certificate{*tlsCert}}
app := initFiberApp()
ln, err := tls.Listen("tcp", ":443", config)
if err != nil {
panic(err)
}
log.Fatal(app.Listener(ln))
}
}