-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
65 lines (52 loc) · 1.53 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
//
// simplecert
//
// Created by Philipp Mieden
// Contact: dreadl0ck@protonmail.ch
// Copyright © 2018 bestbytes. All rights reserved.
//
package main
import (
"fmt"
"log"
"net/http"
"github.com/foomo/simplecert"
"github.com/foomo/tlsconfig"
)
type Handler struct{}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello from simplecert"))
}
func main() {
// do the cert magic
cfg := simplecert.Default
cfg.Domains = []string{"yourdomain.com", "www.yourdomain.com"}
cfg.CacheDir = "letsencrypt"
cfg.SSLEmail = "you@emailprovider.com"
cfg.Local = true
certReloader, err := simplecert.Init(cfg, func() {
// this function will be called upon receiving the syscall.SIGINT or syscall.SIGABRT signal
// and can be used to stop your backend gracefully
fmt.Println("cleaning up...")
})
if err != nil {
log.Fatal("simplecert init failed: ", err)
}
// redirect HTTP to HTTPS
log.Println("starting HTTP Listener on Port 80")
go http.ListenAndServe(":80", http.HandlerFunc(simplecert.Redirect))
// init strict tlsConfig with certReloader
tlsconf := tlsconfig.NewServerTLSConfig(tlsconfig.TLSModeServerStrict)
// now set GetCertificate to the reloaders GetCertificateFunc to enable hot reload
tlsconf.GetCertificate = certReloader.GetCertificateFunc()
// init server
s := &http.Server{
Addr: ":443",
TLSConfig: tlsconf,
Handler: Handler{},
}
log.Println("now visit: https://" + cfg.Domains[0])
// lets go
log.Fatal(s.ListenAndServeTLS("", ""))
}