/
44.go
58 lines (49 loc) · 987 Bytes
/
44.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
package main
import (
. "fmt"
. "net/http"
"os"
"sync"
)
var (
address string
secure_address string
certificate string
key string
)
var servers sync.WaitGroup
func init() {
if address = os.Getenv("SERVE_HTTP"); address == "" {
address = ":1024"
}
if secure_address = os.Getenv("SERVE_HTTPS"); secure_address == "" {
secure_address = ":1025"
}
if certificate = os.Getenv("SERVE_CERT"); certificate == "" {
certificate = "cert.pem"
}
if key = os.Getenv("SERVE_KEY"); key == "" {
key = "key.pem"
}
}
func main() {
message := "hello world"
HandleFunc("/hello", func(w ResponseWriter, r *Request) {
w.Header().Set("Content-Type", "text/plain")
Fprintf(w, message)
})
Launch(func() {
ListenAndServe(address, nil)
})
Launch(func() {
ListenAndServeTLS(secure_address, certificate, key, nil)
})
servers.Wait()
}
func Launch(f func()) {
servers.Add(1)
go func() {
defer servers.Done()
f()
}()
}