summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test_container/Dockerfile.builder2
-rw-r--r--test_container/server.go68
-rw-r--r--test_container/udp_client.go30
3 files changed, 95 insertions, 5 deletions
diff --git a/test_container/Dockerfile.builder b/test_container/Dockerfile.builder
index 9b1a797..8495cdc 100644
--- a/test_container/Dockerfile.builder
+++ b/test_container/Dockerfile.builder
@@ -156,6 +156,8 @@ RUN mkdir -p rootfs/usr/bin; \
156 cp /usr/local/bin/curl rootfs/usr/bin/curl 156 cp /usr/local/bin/curl rootfs/usr/bin/curl
157 157
158ADD ./server.go . 158ADD ./server.go .
159ADD ./udp_client.go .
159RUN go build -ldflags "-linkmode external -extldflags -static" -o rootfs/usr/bin/helloserver server.go 160RUN go build -ldflags "-linkmode external -extldflags -static" -o rootfs/usr/bin/helloserver server.go
161RUN go build -ldflags "-linkmode external -extldflags -static" -o rootfs/usr/bin/udp_client udp_client.go
160RUN mkdir -p rootfs/etc/ssl/certs \ 162RUN mkdir -p rootfs/etc/ssl/certs \
161 && cp /etc/ssl/certs/ca-certificates.crt rootfs/etc/ssl/certs/ca-certificates.crt 163 && cp /etc/ssl/certs/ca-certificates.crt rootfs/etc/ssl/certs/ca-certificates.crt
diff --git a/test_container/server.go b/test_container/server.go
index 71bacc0..bee5d00 100644
--- a/test_container/server.go
+++ b/test_container/server.go
@@ -3,9 +3,13 @@ package main
3import ( 3import (
4 "fmt" 4 "fmt"
5 "log" 5 "log"
6 "net"
6 "net/http" 7 "net/http"
7 "os" 8 "os"
9 "runtime"
10 "strconv"
8 "strings" 11 "strings"
12 "sync"
9) 13)
10 14
11func handler(w http.ResponseWriter, r *http.Request) { 15func handler(w http.ResponseWriter, r *http.Request) {
@@ -16,22 +20,63 @@ func handler(w http.ResponseWriter, r *http.Request) {
16 } 20 }
17} 21}
18 22
19func main() { 23func send_udp_response(conn *net.UDPConn, addr *net.UDPAddr) {
20 http.HandleFunc("/", handler) 24 hostname, err := os.Hostname()
25 if err == nil {
26 resp_str := fmt.Sprintf("%s: HELLO! I AM ALIVE!!!\n", hostname)
27 _, err := conn.WriteToUDP([]byte(resp_str), addr)
28 if err != nil {
29 log.Println("Failed to reply to client")
30 }
31 }
32}
33
34func run_udp_server(port int) {
35
36 p := make([]byte, 2048)
37
38 log.Println("Running UDP server")
39 ser, _ := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: port, Zone: ""})
40 defer ser.Close()
41
42 for {
43 _, remoteaddr, err := ser.ReadFromUDP(p)
44 if err != nil {
45 log.Println("We got an Error on reading")
46 continue
47 }
48 log.Println("Received UDP request")
49 send_udp_response(ser, remoteaddr)
50 }
51}
52
53func udp_handling(wg sync.WaitGroup) {
54 udpPort, udpPortPresent := os.LookupEnv("UDP_PORT")
21 55
56 var port_num int = 9090
57 if udpPortPresent {
58 port_num, _ = strconv.Atoi(strings.TrimSpace(udpPort))
59 }
60 run_udp_server(port_num)
61}
62
63func http_handling(wg sync.WaitGroup) {
64 defer wg.Done()
65
66 http.HandleFunc("/", handler)
22 httpsPort, httpsPortPresent := os.LookupEnv("HTTPS_PORT") 67 httpsPort, httpsPortPresent := os.LookupEnv("HTTPS_PORT")
23 68
24 var port string 69 var port string
70
25 if httpsPortPresent { 71 if httpsPortPresent {
26 port = ":" + strings.TrimSpace(httpsPort) 72 port = ":" + strings.TrimSpace(httpsPort)
27
28 cert, certPresent := os.LookupEnv("HTTPS_CERT_PATH") 73 cert, certPresent := os.LookupEnv("HTTPS_CERT_PATH")
29 key, keyPresent := os.LookupEnv("HTTPS_KEY_PATH") 74 key, keyPresent := os.LookupEnv("HTTPS_KEY_PATH")
30 75
31 if !certPresent || !keyPresent { 76 if !certPresent || !keyPresent {
32 log.Fatal("HTTPS_PORT configured but missing HTTPS_CERT_PATH and/or HTTPS_KEY_PATH") 77 log.Fatal("HTTPS_PORT configured but missing HTTPS_CERT_PATH and/or HTTPS_KEY_PATH")
33 } 78 }
34 79 log.Println("Running HTTPS server")
35 log.Fatal(http.ListenAndServeTLS(port, cert, key, nil)) 80 log.Fatal(http.ListenAndServeTLS(port, cert, key, nil))
36 } else { 81 } else {
37 httpPort, confPresent := os.LookupEnv("HTTP_PORT") 82 httpPort, confPresent := os.LookupEnv("HTTP_PORT")
@@ -40,7 +85,20 @@ func main() {
40 } else { 85 } else {
41 port = ":8080" 86 port = ":8080"
42 } 87 }
43 88 log.Println("Running HTTP server")
44 log.Fatal(http.ListenAndServe(port, nil)) 89 log.Fatal(http.ListenAndServe(port, nil))
45 } 90 }
91 log.Println("Exit HTTP server...")
92
93}
94
95func main() {
96
97 runtime.GOMAXPROCS(2)
98 var wg sync.WaitGroup
99 wg.Add(2)
100
101 go http_handling(wg)
102 go udp_handling(wg)
103 wg.Wait()
46} 104}
diff --git a/test_container/udp_client.go b/test_container/udp_client.go
new file mode 100644
index 0000000..3f87d51
--- /dev/null
+++ b/test_container/udp_client.go
@@ -0,0 +1,30 @@
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "net"
7 "os"
8)
9
10// udp_client.go syntax : udp_client <server_IP> <server_port>
11
12func main() {
13
14 server_ip_port := os.Args[1] + ":" + os.Args[2]
15
16 p := make([]byte, 2048)
17 conn, err := net.Dial("udp", server_ip_port)
18 if err != nil {
19 fmt.Printf("Some error %v", err)
20 return
21 }
22 fmt.Fprintf(conn, "Hi UDP Server, How are you?")
23 _, err = bufio.NewReader(conn).Read(p)
24 if err == nil {
25 fmt.Printf("%s\n", p)
26 } else {
27 fmt.Printf("Some error %v\n", err)
28 }
29 conn.Close()
30}