-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
73 lines (67 loc) · 1.57 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
)
// DefaultPort is the default port to use if once is not specified by the SERVER_PORT environment variable
const HTTPPort = 8888
const HTTPSPort = 8443
type context struct {
httpPort int
httpsPort int
verbose bool
hostname string
}
func (c *context) getParams() {
httpPtr := flag.Int("http", HTTPPort, "http port value")
httpsPtr := flag.Int("https", HTTPSPort, "https port value")
verbose := flag.Bool("v", false, "verbose logging")
flag.Parse()
c.httpPort = *httpPtr
c.httpsPort = *httpsPtr
c.verbose = *verbose
}
func listenAndServceTLS(port string) {
cmd := exec.Command("./generate-cert.sh")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
_, err = os.Stat("server.crt")
if os.IsNotExist(err) {
log.Fatal("server.crt: ", err)
}
_, err = os.Stat("server.key")
if os.IsNotExist(err) {
log.Fatal("server.key: ", err)
}
err = http.ListenAndServeTLS(":"+port, "server.crt", "server.key", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func main() {
var err error
ctx := context{}
ctx.hostname, err = os.Hostname()
if err != nil {
log.Println(err)
}
ctx.getParams()
http.HandleFunc("/hostname", ctx.echoHostname)
http.HandleFunc("/", ctx.echoAll)
log.Printf("starting echo server, listening on ports HTTP:%d/HTTPS:%d", ctx.httpPort, ctx.httpsPort)
// HTTPS
go func() {
listenAndServceTLS(fmt.Sprintf("%d", ctx.httpsPort))
}()
// HTTP
err = http.ListenAndServe(fmt.Sprintf(":%d", ctx.httpPort), nil)
if err != nil {
log.Fatal("Echo server (HTTP): ", err)
}
}