Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (55 loc) · 1.49 KB
/
Copy pathmain.go
File metadata and controls
65 lines (55 loc) · 1.49 KB
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
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/appscodelabs/registry-redirect/pkg/redirect"
flag "github.com/spf13/pflag"
"golang.org/x/crypto/acme/autocert"
"knative.dev/pkg/logging"
)
func main() {
opts := redirect.NewOptions()
opts.AddFlags(flag.CommandLine)
flag.Parse()
logger := logging.FromContext(context.Background())
if !opts.EnableSSL {
http.Handle("/", redirect.New(opts))
logger.Infof("Listening on port %d", opts.Port)
logger.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", opts.Port), nil))
return
}
// ref:
// - https://goenning.net/2017/11/08/free-and-automated-ssl-certificates-with-go/
// - https://stackoverflow.com/a/40494806/244009
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(opts.CertDir),
HostPolicy: autocert.HostWhitelist(opts.Hosts...),
Email: opts.CertEmail,
}
server := &http.Server{
Addr: ":https",
Handler: redirect.New(opts),
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second,
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
go func() {
// does automatic http to https redirects
err := http.ListenAndServe(":http", certManager.HTTPHandler(nil))
if err != nil {
panic(err)
}
}()
logger.Fatal(server.ListenAndServeTLS("", "")) // Key and cert are coming from Let's Encrypt
}