From 8f88b4a98560a405929d4de2195616ed2d92adc7 Mon Sep 17 00:00:00 2001 From: Tom Pughe <147526382+TomPughe@users.noreply.github.com> Date: Tue, 18 Jun 2024 09:43:36 +0100 Subject: [PATCH] Upload exercise files for 03_06 --- hello-server.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ test.sh | 10 ++++++++ 2 files changed, 72 insertions(+) create mode 100644 hello-server.go create mode 100644 test.sh diff --git a/hello-server.go b/hello-server.go new file mode 100644 index 0000000..cea39fc --- /dev/null +++ b/hello-server.go @@ -0,0 +1,62 @@ +// Credits: +// https://yourbasic.org/golang/http-server-example/ +// https://stackoverflow.com/a/42533360/2308522 +// https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04 +package main + +import ( + "context" + "fmt" + "log" + "net/http" + "sync" + "time" +) + +func startHttpServer(wg *sync.WaitGroup) *http.Server { + srv := &http.Server{Addr: ":11000"} + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) + }) + + go func() { + defer wg.Done() // let main know we are done cleaning up + + // always returns error. ErrServerClosed on graceful close + if err := srv.ListenAndServe(); err != http.ErrServerClosed { + // unexpected error. port in use? + log.Fatalf("ListenAndServe(): %v", err) + } + }() + + // returning reference so caller can call Shutdown() + return srv +} + +func main() { + log.Printf("main: starting HTTP server") + + httpServerExitDone := &sync.WaitGroup{} + + httpServerExitDone.Add(1) + srv := startHttpServer(httpServerExitDone) + + log.Printf("main: serving for 10 seconds") + + time.Sleep(10 * time.Second) + + log.Printf("main: stopping HTTP server") + + // now close the server gracefully ("shutdown") + // timeout could be given with a proper context + // (in real world you shouldn't use TODO()). + if err := srv.Shutdown(context.TODO()); err != nil { + panic(err) // failure/timeout shutting down the server gracefully + } + + // wait for goroutine started in startHttpServer() to stop + httpServerExitDone.Wait() + + log.Printf("main: done. exiting") +} diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..6609869 --- /dev/null +++ b/test.sh @@ -0,0 +1,10 @@ +chmod +x ./linux/hello-server + +./linux/hello-server & + +sleep 3 + +for i in Anakin Luke Leia Kylo Rey; +do + echo "$(date): $(curl -s http://localhost:11000/${i})" +done