-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
58 lines (46 loc) · 981 Bytes
/
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 (
"fmt"
"net"
"net/http"
"os"
"time"
)
func getColor() string {
return os.Getenv("COLOR")
}
func tcpHandler(c net.Conn) {
for {
c.Write([]byte(fmt.Sprintf("The color is #%s", getColor())))
c.Write([]byte(fmt.Sprintln()))
time.Sleep(5 * time.Second)
}
}
func serveTCP() {
ln, err := net.Listen("tcp", ":8081")
if err != nil {
// handle error but not today
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error but not today
}
go tcpHandler(conn)
}
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
color := getColor()
fmt.Printf("Serving color: #%s", color)
fmt.Println()
fmt.Fprintf(w, "<body bgcolor=\"#%s\"><h1>#%s</h1></body>", color, color)
}
func main() {
color := getColor()
fmt.Printf("Booted with color: #%s", color)
fmt.Println()
go serveTCP()
http.HandleFunc("/", httpHandler)
fmt.Println("listening with http on :8080 and tcp on :8081")
http.ListenAndServe(":8080", nil)
}