/
main.go
99 lines (82 loc) · 1.78 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
package main
import (
"fmt"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"github.com/335is/config"
"github.com/335is/log"
"github.com/335is/server/internal/router"
uuid "github.com/satori/go.uuid"
)
// top level config structure
type cfg struct {
HTTP http `yaml:"http"`
CPU profiling `yaml:"cpu"`
Memory profiling `yaml:"memory"`
}
// HTTP holds web server configuration
//
// HTTP_ADDRESS
// HTTP_PORT
// HTTP_CONTENT
type http struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
Content string `yaml:"content"`
}
// CPU and Memory profiling settings
//
// CPU_ENABLED
// CPU_FILENAME
// MEMORY_ENABLED
// MEMORY_FILENAME
type profiling struct {
Enabled bool `yaml:"enabled"`
Filename string `yaml:"filename"`
}
const (
appName = "server"
appVersion = "0.0.2"
)
var (
appInstance string
)
func init() {
appInstance = fmt.Sprintf("%s", uuid.NewV4())
}
func main() {
log.Infof("Starting %s %s %s LOG_LEVEL=%s", appName, appVersion, appInstance, log.GetLevel().String())
c := cfg{}
config.Load("", &c)
// CPU profiling
if c.CPU.Enabled {
f, err := os.Create(c.CPU.Filename)
if err != nil {
log.Errorf(err.Error())
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Memory profiling
if c.Memory.Enabled {
f, err := os.Create(c.Memory.Filename)
if err != nil {
log.Errorf(err.Error())
}
defer func() {
pprof.WriteHeapProfile(f)
f.Close()
}()
}
go router.ServeHTTP(c.HTTP.Port, c.HTTP.Content)
waitForExit()
log.Infof("Stopping %s %s %s", appName, appVersion, appInstance)
}
func waitForExit() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT, syscall.SIGKILL, syscall.SIGTERM)
sig := <-sigs
log.Infof("Received signal %s, exiting...", sig)
}