-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (55 loc) · 1.68 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
65
66
67
68
package main
import (
"crypto/tls"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"github.com/dorianneto/burn-secret/cmd/api"
"github.com/dorianneto/burn-secret/internal"
"github.com/joho/godotenv"
"golang.org/x/crypto/acme/autocert"
)
func getSelfSignedOrLetsEncryptCert(certManager *autocert.Manager) func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
dirCache, ok := certManager.Cache.(autocert.DirCache)
if !ok {
dirCache = "certs"
}
keyFile := filepath.Join(string(dirCache), hello.ServerName+".key")
crtFile := filepath.Join(string(dirCache), hello.ServerName+".crt")
certificate, err := tls.LoadX509KeyPair(crtFile, keyFile)
if err != nil {
return certManager.GetCertificate(hello)
}
return &certificate, err
}
}
func main() {
godotenv.Load(".env." + os.Getenv("APP_ENV"))
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
database, err := internal.NewDatabase(logger)
if err != nil {
logger.Error(err.Error())
}
app := api.NewApp(logger, database)
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(os.Getenv("APP_DOMAIN")),
Cache: autocert.DirCache("certs"),
}
tlsConfig := certManager.TLSConfig()
tlsConfig.GetCertificate = getSelfSignedOrLetsEncryptCert(&certManager)
server := http.Server{
Addr: ":443",
Handler: app.Routes(),
TLSConfig: tlsConfig,
}
logger.Info(fmt.Sprintf("server running on port :%s", server.Addr))
go http.ListenAndServe(":80", certManager.HTTPHandler(nil))
if err := server.ListenAndServeTLS("", ""); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}