-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.54 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
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"os"
"github.com/QLife-Inc/sorry/lib"
)
var (
Version string
Revision string
)
func getEnvOrDefault(key string, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func getHttpPort() string {
return getEnvOrDefault("PORT", "80")
}
func getHttpsPort() string {
return getEnvOrDefault("HTTPS_PORT", "443")
}
func createHttpsListener(contents *lib.ResponseContents) (func(), error) {
certs, err := lib.GetCertificatePairs()
if err != nil {
return nil, err
}
if len(certs) == 0 {
return nil, nil
}
var port = getHttpsPort()
listener, err := lib.CreateHttpsListener(port, certs)
if err != nil {
return nil, err
}
return func() {
log.Printf("Start HTTPS Listener on port %s\n", port)
lib.CreateServer(contents, "https").Serve(listener)
}, nil
}
func createHttpListener(contents *lib.ResponseContents) func() {
var server = lib.CreateServer(contents, "http")
var port = getHttpPort()
server.Addr = ":" + port
return func() {
log.Printf("Start HTTP Listener on port %s\n", port)
server.ListenAndServe()
}
}
func createListeners(contents *lib.ResponseContents) (func(), func(), error) {
httpListener := createHttpListener(contents)
httpsListener, err := createHttpsListener(contents)
return httpListener, httpsListener, err
}
func main() {
contents, err := lib.NewResponseContents()
http, https, err := createListeners(contents)
if err != nil {
log.Fatal(err)
panic(err)
}
finish := make(chan bool)
go http()
go https()
<-finish
}