forked from code-golf/code-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (75 loc) · 2.08 KB
/
main.go
File metadata and controls
93 lines (75 loc) · 2.08 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"crypto/tls"
"log"
"math/rand"
"net/http"
"os"
"syscall"
"time"
"github.com/JRaspass/code-golf/routes"
brotli "github.com/cv-library/negroni-brotli"
"github.com/urfave/negroni"
"golang.org/x/crypto/acme/autocert"
)
type err500 struct{}
func (*err500) FormatPanicError(w http.ResponseWriter, r *http.Request, _ *negroni.PanicInformation) {
http.Error(w, "500: It's Dead, Jim.", 500)
}
func main() {
os.Setenv("GODEBUG", "tls13=1")
rand.Seed(time.Now().UTC().UnixNano())
certManager := autocert.Manager{
Cache: autocert.DirCache("certs"),
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(
"code-golf.io", "www.code-golf.io",
),
}
logger := negroni.NewLogger()
logger.ALogger = log.New(os.Stdout, "", 0)
logger.SetFormat("{{.StartTime}} {{.Status}} {{.Method}} {{.Request.URL}} {{.Request.UserAgent}}")
recovery := negroni.NewRecovery()
recovery.Formatter = &err500{}
recovery.Logger = log.New(os.Stderr, "<1>", 0)
server := &http.Server{
Handler: negroni.New(
logger,
brotli.New(5),
recovery,
negroni.Wrap(routes.Router),
),
TLSConfig: &tls.Config{
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, // HTTP/2-required.
},
CurvePreferences: []tls.CurveID{tls.CurveP256, tls.X25519},
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
},
}
_, dev := syscall.Getenv("DEV")
if !dev {
server.TLSConfig.GetCertificate = certManager.GetCertificate
}
server.TLSConfig.BuildNameToCertificate()
go func() {
ticker := time.NewTicker(time.Minute)
for {
<-ticker.C
routes.Stars()
}
}()
println("Listening…")
// Redirect HTTP to HTTPS and handle ACME challenges.
go func() { panic(http.ListenAndServe(":80", certManager.HTTPHandler(nil))) }()
// Serve HTTPS.
if dev {
panic(server.ListenAndServeTLS("localhost.pem", "localhost-key.pem"))
} else {
panic(server.ListenAndServeTLS("", ""))
}
}