-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.go
141 lines (131 loc) · 3.46 KB
/
server.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
133
134
135
136
137
138
139
140
141
package melon
import (
"os"
"os/signal"
"github.com/goburrow/melon/core"
)
const (
maxBannerSize = 50 * 1024 // 50KB
)
// serverCommand implements Command.
type serverCommand struct {
configurationCommand
}
// Name returns name of the serverCommand.
func (command *serverCommand) Name() string {
return "server"
}
// Description returns description of the serverCommand.
func (command *serverCommand) Description() string {
return "runs the application as an HTTP server"
}
// Run runs the command with the given bootstrap.
func (command *serverCommand) Run(bootstrap *core.Bootstrap) error {
// Parse configuration
err := command.configurationCommand.Run(bootstrap)
if err != nil {
logger().Errorf("could not run server: %v", err)
return err
}
// Create environment
environment := core.NewEnvironment()
environment.Validator = command.configurationCommand.validator
defer environment.Stop()
// Config other factories that affect this environment.
configuration := command.configurationCommand.configuration.(core.Configuration)
err = configuration.LoggingFactory().ConfigureLogging(environment)
if err != nil {
logger().Errorf("could not run server: %v", err)
return err
}
err = configuration.MetricsFactory().ConfigureMetrics(environment)
if err != nil {
logger().Errorf("could not run server: %v", err)
return err
}
// Always run Stop() method on managed objects.
// Build server
server, err := configuration.ServerFactory().BuildServer(environment)
if err != nil {
logger().Errorf("could not run server: %v", err)
return err
}
// Now can start everything
printBanner()
// Run all bundles in bootstrap
err = bootstrap.Run(command.configurationCommand.configuration, environment)
if err != nil {
logger().Errorf("could not run bootstrap: %v", err)
return err
}
// Run application
err = bootstrap.Application.Run(command.configurationCommand.configuration, environment)
if err != nil {
logger().Errorf("could not run application: %v", err)
return err
}
err = environment.Start()
if err != nil {
logger().Errorf("could not start environment: %v", err)
return err
}
// Handle signal
sigCh := make(chan os.Signal, 1)
defer close(sigCh)
signal.Notify(sigCh, os.Interrupt)
go func() {
for sig := range sigCh {
logger().Debugf("received signal %v", sig)
err := server.Stop()
if err != nil {
logger().Errorf("could not stop server: %v", err)
}
return
}
}()
// Start is blocking
err = server.Start()
if err != nil {
logger().Errorf("could not start server: %v", err)
return err
}
return nil
}
// printBanner prints application banner to the given logger
func printBanner() {
banner := readBanner()
if banner == "" {
logger().Infof("starting")
} else {
logger().Infof("starting\n%s", banner)
}
}
// readBanner read contents of a banner found in the current directory.
// A banner is a .txt file which has the same name with the running application.
func readBanner() string {
banner, err := readFileContents(os.Args[0]+".txt", maxBannerSize)
if err != nil {
return ""
}
return banner
}
// readFileContents read contents with a limit of maximum bytes
func readFileContents(file string, maxBytes int) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
n := maxBytes
if fi, err := f.Stat(); err == nil {
if int(fi.Size()) < n {
n = int(fi.Size())
}
}
buf := make([]byte, n)
n, err = f.Read(buf)
if err != nil {
return "", err
}
return string(buf[0:n]), nil
}