-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver_windows.go
74 lines (63 loc) · 1.6 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/quasoft/websspi"
)
var helloTemplate = template.Must(template.New("index.html").Parse(`
{{- if . -}}
<h2>Hello {{ .Username }}!</h2>
{{ if .Groups -}}
Groups:
<ul>
{{- range .Groups}}
<li>{{ . }}</li>
{{end -}}
</ul>
{{- end }}
{{- else -}}
<h2>Hello!</h2>
{{- end -}}
`))
func main() {
config := websspi.NewConfig()
config.EnumerateGroups = true // If groups should be resolved
// config.ServerName = "..." // If static instead of dynamic group membership should be resolved
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) {
info := r.Context().Value(websspi.UserInfoKey)
userInfo, _ := info.(*websspi.UserInfo)
w.Header().Add("Content-Type", "text/html; encoding=utf-8")
helloTemplate.Execute(w, userInfo)
})
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)
}
}