-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (92 loc) · 3.2 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/handlers"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"github.com/lescactus/clamav-api-go/internal/clamav"
"github.com/lescactus/clamav-api-go/internal/config"
"github.com/lescactus/clamav-api-go/internal/controllers"
"github.com/lescactus/clamav-api-go/internal/logger"
"github.com/rs/zerolog/hlog"
)
func main() {
// Get application configuration
cfg, err := config.New()
if err != nil {
log.Fatalf("unable to build a new app config: %v", err)
}
logger := logger.New(
cfg.LoggerLogLevel,
cfg.LoggerDurationFieldUnit,
cfg.LoggerFormat,
)
client := clamav.NewClamavClient(
cfg.ClamavAddr,
cfg.ClamavNetwork,
cfg.ClamavTimeout,
cfg.ClamavKeepAlive,
)
// Create http router, server and handler controller
r := httprouter.New()
h := controllers.NewHandler(logger, client)
c := alice.New()
s := &http.Server{
Addr: cfg.ServerAddr,
Handler: handlers.RecoveryHandler(handlers.PrintRecoveryStack(true))(r), // recover from panics and print recovery stack
ReadTimeout: cfg.ServerReadTimeout,
ReadHeaderTimeout: cfg.ServerReadHeaderTimeout,
WriteTimeout: cfg.ServerWriteTimeout,
}
// logger fields
*logger = logger.With().Str("svc", config.AppName).Logger()
// Register logging middleware
c = c.Append(hlog.NewHandler(*logger))
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
}))
c = c.Append(hlog.RefererHandler("referer"))
c = c.Append(hlog.RemoteAddrHandler("remote_client"))
c = c.Append(hlog.UserAgentHandler("user_agent"))
c = c.Append(hlog.RequestIDHandler("req_id", "X-Request-ID"))
c = c.Append(controllers.MaxReqSize(cfg.ServerMaxRequestSize))
r.Handler(http.MethodGet, "/rest/v1/ping", c.ThenFunc(h.Ping))
r.Handler(http.MethodGet, "/rest/v1/version", c.ThenFunc(h.Version))
r.Handler(http.MethodGet, "/rest/v1/stats", c.ThenFunc(h.Stats))
r.Handler(http.MethodGet, "/rest/v1/versioncommands", c.ThenFunc(h.VersionCommands))
r.Handler(http.MethodPost, "/rest/v1/reload", c.ThenFunc(h.Reload))
r.Handler(http.MethodPost, "/rest/v1/shutdown", c.ThenFunc(h.Shutdown))
r.Handler(http.MethodPost, "/rest/v1/scan", c.ThenFunc(h.InStream))
// Start server
go func() {
logger.Info().Msgf("Starting server %s on address %s ...", config.AppName, cfg.ServerAddr)
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal().Err(err).Msg("Startup failed")
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
// Blocking until receiving a shutdown signal
sig := <-sigChan
logger.Info().Msgf("Server received %s signal. Shutting down...", sig)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer func() {
cancel()
}()
// Attempting to gracefully shutdown the server
if err := s.Shutdown(ctx); err != nil {
logger.Warn().Msg("Failed to gracefully shutdown the server")
}
}