-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
53 lines (44 loc) · 1.22 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
zip_streamer "github.com/scosman/zipstreamer/zip_streamer"
)
func main() {
zipServer := zip_streamer.NewServer()
zipServer.Compression = (os.Getenv("ZS_COMPRESSION") == "DEFLATE")
zipServer.ListfileUrlPrefix = os.Getenv("ZS_LISTFILE_URL_PREFIX")
port := os.Getenv("PORT")
if port == "" {
port = "4008"
}
httpServer := &http.Server{
Addr: ":" + port,
Handler: zipServer,
ReadTimeout: 10 * time.Second,
}
shutdownChannel := make(chan os.Signal, 10)
go func() {
log.Printf("Server starting on port %s", port)
err := httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Printf("Server Error: %s", err)
}
shutdownChannel <- syscall.SIGUSR1
}()
// Listen for os signal for graceful shutdown
signal.Notify(shutdownChannel, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
// Wait for shutdown signal, then shut down
shutdownSignal := <-shutdownChannel
log.Printf("Received signal (%s), shutting down...", shutdownSignal.String())
httpServer.Shutdown(context.Background())
// Exit was not expected, return non 0 exit code
if shutdownSignal == syscall.SIGUSR1 {
os.Exit(1)
}
}