This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
74 lines (63 loc) · 1.83 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
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"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/findy-network/findy-agent-vault/agency/findy"
"github.com/findy-network/findy-agent-vault/resolver"
"github.com/findy-network/findy-agent-vault/server"
"github.com/findy-network/findy-agent-vault/utils"
"github.com/golang/glog"
)
func main() {
utils.SetLogDefaults()
config := utils.LoadConfig()
if len(os.Args) > 1 && os.Args[1] == "version" {
log.Printf("Vault version %s\n", config.Version)
return
}
gqlResolver := resolver.InitResolver(config, &findy.Agency{})
defer gqlResolver.Close()
srv := server.NewServer(gqlResolver, config.JWTKey)
http.Handle("/query", srv.Handle())
if config.UsePlayground {
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
}
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
if utils.LogTrace() {
glog.Infof("health check %s %s", r.URL.Path, config.Version)
}
_, _ = w.Write([]byte(config.Version))
})
startServer(config.Address)
}
func startServer(address string) {
const serverTimeout = 5 * time.Second
ourServer := &http.Server{
Addr: address,
ReadHeaderTimeout: serverTimeout,
}
go func() {
if err := ourServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
glog.Errorf("HTTP server error: %v", err)
} else {
glog.Infoln("Stopped serving new connections.")
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), serverTimeout)
defer shutdownRelease()
if err := ourServer.Shutdown(shutdownCtx); err != nil {
glog.Errorf("HTTP shutdown error: %v", err)
} else {
glog.Infoln("Graceful shutdown complete.")
}
}