forked from remind101/acme-inc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (50 loc) · 1.2 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
package main
import (
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"time"
)
func main() {
var (
port = flag.String("port", env("PORT", "8080"), "The port")
)
flag.Parse()
args := flag.Args()
if len(args) < 1 {
log.Fatal("You must specify the `server` or `worker` subcommand.")
}
cmd := args[0]
webString := env("WEB_STRING", "Default header")
workerString := env("WORKER_STRING", "Default header")
hostname, _ := os.Hostname()
log.Printf("[%s] Starting %s process.\n", hostname, cmd)
switch cmd {
case "server":
log.Printf("Starting on %s", *port)
log.Fatal(http.ListenAndServe(":"+*port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
log.Printf("[%s] %s: %s - %s", hostname, webString, r.Method, r.URL)
w.WriteHeader(200)
io.WriteString(w, fmt.Sprintf("[%s %s] %s: Ok\n", now, hostname, webString))
})))
case "worker":
for {
<-time.After(1 * time.Second)
log.Printf("[%s] %s: Hard work %d...\n", hostname, workerString, rand.Int())
}
default:
log.Fatalf("Unknown subcommand: %s", cmd)
}
}
func env(key string, fallback string) string {
value := os.Getenv(key)
if value == "" {
value = fallback
}
return value
}