forked from quasoft/websspi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_windows.go
51 lines (43 loc) · 1.05 KB
/
server_windows.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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/sorokinmax/websspi"
)
func main() {
config := websspi.NewConfig()
auth, err := websspi.New(config)
if err != nil {
panic(err)
}
server := &http.Server{Addr: "0.0.0.0:9000"}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username := ""
w.Write([]byte("Hello " + username))
})
http.Handle("/", auth.WithAuth(handler))
stop := make(chan os.Signal, 2)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %s\n", err)
}
}()
// Wait for SIGINT or SIGTERM
<-stop
log.Print("Shutting down and releasing resources...\n")
err = auth.Free()
if err != nil {
log.Printf("Error while releasing resources: %v\n", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Graceful shutdown timed out: %s\n", err)
}
}