-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (104 loc) · 2.82 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"gogo/mathsutil"
"gogo/stringsutil"
"gogo/timesutil"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
mode := flag.String("mode", "client", "Mode to run: 'server' or 'client'")
hostname := flag.String("host", "localhost", "Hostname to bind or connect to")
port := flag.Int("port", 8080, "Port to bind or connect to")
path := flag.String("path", "/", "Path to request (for client mode)")
flag.Parse()
log.Printf("Starting\n- mode: %s\n- hostname: %s\n- port: %d\n- path: %s", *mode, *hostname, *port, *path)
if *mode == "server" {
go func() {
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
<-sigChannel
log.Println("Received shutdown signal, exiting...")
os.Exit(0)
}()
startServer(*hostname, *port)
} else if *mode == "client" {
startClient(*hostname, *port, *path)
} else if *mode == "test" {
doSomeTests()
} else {
log.Fatalf("Invalid mode: %s. Use 'server' or 'client'.\n", *mode)
}
}
func startServer(hostname string, port int) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
_, err := fmt.Fprintf(w, "Welcome to the main page!")
if err != nil {
log.Printf("Failed to handle path %s: %v\n", r.URL.Path, err)
return
}
case "/helloworld":
_, err := fmt.Fprintf(w, "Hello, World!")
if err != nil {
log.Printf("Failed to handle path %s: %v\n", r.URL.Path, err)
return
}
case "/info":
_, err := fmt.Fprintf(w, "This is the info page.")
if err != nil {
log.Printf("Failed to handle path %s: %v\n", r.URL.Path, err)
return
}
case "/shutdown":
_, err := fmt.Fprintf(w, "Server shutting down...")
if err != nil {
log.Printf("Failed to handle path %s: %v\n", r.URL.Path, err)
return
}
go func() {
time.Sleep(1 * time.Second)
os.Exit(0)
}()
default:
http.NotFound(w, r) // Return a 404 for unrecognized paths
}
})
address := fmt.Sprintf("%s:%d", hostname, port)
log.Printf("Starting server at %s\n", address)
if err := http.ListenAndServe(address, nil); err != nil {
log.Fatalf("Server failed: %s\n", err)
}
}
func startClient(hostname string, port int, path string) {
url := fmt.Sprintf("http://%s:%d%s", hostname, port, path)
resp, err := http.Get(url)
if err != nil {
log.Printf("Failed to make GET request to %s: %v\n", url, err)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("Failed to close GET request to %s: %v\n", url, err)
return
}
}(resp.Body)
body, _ := io.ReadAll(resp.Body)
log.Printf("Response from %s:\n%s\n", path, string(body))
}
func doSomeTests() {
stringsutil.PrintTests()
timesutil.TimeTests()
stringsutil.SwitchTests()
stringsutil.PalindromeTests()
mathsutil.FibonacciTests()
}