-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
83 lines (71 loc) · 2.19 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/facebookgo/inject"
cache "github.com/patrickmn/go-cache"
"github.com/salestock/sersan/config"
"github.com/salestock/sersan/lib"
)
func main() {
conf := config.Get()
// Display some important configuration items
log.Printf("[INIT] Node selector: %s:%s", conf.NodeSelectorKey, conf.NodeSelectorValue)
log.Printf("[INIT] Cpu request - limit: %s-%s", conf.CPURequest, conf.CPULimit)
log.Printf("[INIT] Memory request - limit: %s-%s", conf.MemoryRequest, conf.MemoryLimit)
// Load grid config
gridConfig := lib.GetGridConfig()
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Printf("Could not get current directory:%s", err)
}
log.Printf("Current directory: %v", dir)
err = gridConfig.Load(filepath.Join(dir, conf.GridConfigFile))
if err != nil {
log.Printf("Could not load grid config file: %v", err)
}
// Tuned http round tripper
defaultRoundTripper := http.DefaultTransport
defaultTransportPointer, ok := defaultRoundTripper.(*http.Transport)
if !ok {
log.Printf("defaultRoundTripper not an *http.Transport")
}
tunedTransport := *defaultTransportPointer
tunedTransport.MaxIdleConns = conf.MaxIdleConns
tunedTransport.MaxIdleConnsPerHost = conf.MaxIdleConnsPerHost
tunedTransport.MaxConnsPerHost = conf.MaxConnsPerHost
// Setup dependency injection
var rh RootHandler
c := cache.New(time.Duration(conf.CacheTimeout)*time.Minute, time.Duration(conf.CacheTimeout)*time.Duration(2)*time.Minute)
err = inject.Populate(&rh, c, &tunedTransport)
if err != nil {
log.Printf("%v", err)
}
// Setup router
r := CreateRouter(rh)
// Serve
var srv http.Server
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGTERM)
<-sigint
if err = srv.Shutdown(context.Background()); err != nil {
log.Printf("Sersan API shutdown: %v", err)
}
close(idleConnsClosed)
}()
srv.Addr = ":" + conf.Port
srv.Handler = r
log.Printf("Sersan API started in port: %v", conf.Port)
if err = srv.ListenAndServe(); err != nil {
log.Printf("Error starting Sersan API: %v", err)
}
<-idleConnsClosed
}