-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (36 loc) · 1004 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
package main
import (
"log"
"net/http"
"sync"
)
var TheDumbo http.Server
var StopIt chan struct {}
var WorkerSync sync.WaitGroup
func main() {
// Create a ServeMux
mux := http.NewServeMux();
// Register the endpoints
mux.HandleFunc("/hash", CalculateHash)
mux.HandleFunc("/hash/", GetHash)
mux.HandleFunc("/stats", GetStatistics)
mux.HandleFunc("/shutdown", Shutdown)
// Create a channel for graceful shutdown; nothing will ever be written to it!
StopIt = make(chan struct {}, 1)
// Create the http server
TheDumbo := &http.Server { Addr: ":8080", Handler: mux }
// Initalize the wait group for worker thread synchronization
WorkerSync.Add(GetNumOfWorkers())
// Start the http server in a separate thread
go func() {
if err := TheDumbo.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
} ()
// Wait for gracious shutdown
<- StopIt
// All done!
close(RequestChannel)
WorkerSync.Wait()
TheDumbo.Close()
}