-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (74 loc) · 2.66 KB
/
main.go
File metadata and controls
92 lines (74 loc) · 2.66 KB
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
package main
import (
"context"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"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"
)
const apiV1Path = "/api/v1"
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
env := models.MustParseEnv()
ctx = models.InitZerologger(ctx, env.LogLevel, env.LogPrettyPrint)
log.Info().Msgf("Starting Radix Vulnerability Scanner API in %s environment", env.APIEnvironment)
log.Info().Interface("env", env).Msg("configuration")
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", env.Port),
Handler: http.TimeoutHandler(router, 10*time.Second, "Request timeout"),
}
go func() {
log.Info().Msgf("API is serving on port %s, see the API on http://localhost:%s/swaggerui/", env.Port, env.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.DbDSN)
if err != nil {
log.Fatal().Err(err).Msg("Failed to connect to database")
}
return repository.NewGormRepository(gormdb)
}