-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |