-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (108 loc) · 3.38 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/equinor/radix-vulnerability-scanner-api/repository"
"github.com/equinor/radix-vulnerability-scanner-api/service"
"github.com/equinor/radix-vulnerability-scanner-api/utils/auth"
"github.com/equinor/radix-vulnerability-scanner-api/api/vulnerability"
"github.com/equinor/radix-vulnerability-scanner-api/models"
"github.com/equinor/radix-vulnerability-scanner-api/router"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
)
const apiV1Path = "/api/v1"
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
env, ctx, err := models.NewEnv(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error:\n%s\n\n", err.Error())
os.Exit(3)
}
fs := initializeFlagSet()
port := fs.StringP("port", "p", defaultPort(), "Port where API will be served")
log.Debug().Msgf("Port: %s", *port)
log.Debug().Msgf("Cluster: %s", env.ClusterName)
parseFlagsFromArgs(fs)
radixAPIClient := service.NewRadixAPIService(env)
authProvider := auth.NewAuthProvider(ctx, env.OidcIssuer, env.OidcAudience)
repo := getRepository(env)
router := router.NewServer(
env.ClusterName,
apiV1Path,
authProvider,
vulnerability.NewController(vulnerability.NewHandler(radixAPIClient, repo)),
)
srv := &http.Server{
Addr: fmt.Sprintf(":%s", *port),
Handler: http.TimeoutHandler(router, 10*time.Second, "Request timeout"),
}
go func() {
log.Info().Msgf("API is serving on port %s", *port)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Msgf("listen: %s", err)
}
}()
if env.UseProfiler {
go func() {
log.Info().Msgf("Profiler endpoint is serving on port 7070")
if err := http.ListenAndServe("localhost:7070", nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Msgf("listen: %s", err)
}
}()
}
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
log.Info().Msg("shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("Server forced to shutdown")
}
log.Info().Msg("Server exiting")
}
func getRepository(env *models.Env) repository.Interface {
gormdb, err := repository.OpenGormSqlServerDB(env.DbCredentials.Server, env.DbCredentials.Database)
if err != nil {
log.Fatal().Msg(err.Error())
}
return repository.NewGormRepository(gormdb)
}
func initializeFlagSet() *pflag.FlagSet {
// Flag domain.
fs := pflag.NewFlagSet("default", pflag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "DESCRIPTION\n")
fmt.Fprintf(os.Stderr, "Radix vulnerability scanner api server.\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "FLAGS\n")
fs.PrintDefaults()
}
return fs
}
func parseFlagsFromArgs(fs *pflag.FlagSet) {
err := fs.Parse(os.Args[1:])
switch {
case errors.Is(err, pflag.ErrHelp):
os.Exit(0)
case err != nil:
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n\n", err.Error())
fs.Usage()
os.Exit(2)
}
}
func defaultPort() string {
return "3003"
}