-
Notifications
You must be signed in to change notification settings - Fork 86
/
api-server.go
271 lines (241 loc) · 7.31 KB
/
api-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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"net"
"net/http"
"os"
"strconv"
"strings"
"syscall"
"time"
router "github.com/gorilla/mux"
"github.com/journeymidnight/yig/api"
"github.com/journeymidnight/yig/helper"
"github.com/journeymidnight/yig/log"
"github.com/journeymidnight/yig/storage"
)
type ServerConfig struct {
Address string
KeyFilePath string // path for SSL key file
CertFilePath string // path for SSL certificate file
Logger log.Logger // global logger
ObjectLayer *storage.YigStorage
}
// configureServer handler returns final handler for the http server.
func configureServerHandler(c *ServerConfig) http.Handler {
// Initialize API.
apiHandlers := api.ObjectAPIHandlers{
ObjectAPI: c.ObjectLayer,
}
// Initialize router.
mux := router.NewRouter()
// Register all routers.
api.RegisterAPIRouter(mux, apiHandlers)
// Add new routers here.
// List of some generic handlers which are applied for all
// incoming requests, ** in reverse order **
var handlerFns = []api.HandlerFunc{
// Limits the number of concurrent http requests.
api.SetCommonHeaderHandler,
// CORS setting for all browser API requests.
api.SetCorsHandler,
// Validates all incoming URL resources, for invalid/unsupported
// resources client receives a HTTP error.
api.SetIgnoreResourcesHandler,
// graceful shutdown
api.SetGracefulStopHandler,
// Add new handlers here.
api.SetLogHandler,
api.NewAccessLogHandler,
api.SetGenerateContextHandler,
api.SetRequestIdHandler,
}
// Register rest of the handlers.
return api.RegisterHandlers(mux, c.ObjectLayer.MetaStorage, handlerFns...)
}
// configureServer configure a new server instance
func configureServer(c *ServerConfig) *api.Server {
apiServer := &api.Server{
Server: &http.Server{
Addr: c.Address,
// Adding timeout of 10 minutes for unresponsive client connections.
ReadTimeout: 10 * time.Minute,
WriteTimeout: 10 * time.Minute,
Handler: configureServerHandler(c),
MaxHeaderBytes: 1 << 20,
},
}
apiServer.Server.SetKeepAlivesEnabled(helper.CONFIG.KeepAlive)
// Returns configured HTTP server.
return apiServer
}
// getListenIPs - gets all the ips to listen on.
func getListenIPs(httpServerConf *http.Server) (hosts []string, port string) {
host, port, err := net.SplitHostPort(httpServerConf.Addr)
helper.PanicOnError(err, "Unable to parse host port.")
switch {
case host != "":
hosts = append(hosts, host)
default:
addrs, err := net.InterfaceAddrs()
helper.PanicOnError(err, "Unable to determine network interface address.")
for _, addr := range addrs {
if addr.Network() == "ip+net" {
host := strings.Split(addr.String(), "/")[0]
if ip := net.ParseIP(host); ip.To4() != nil {
hosts = append(hosts, host)
}
}
}
}
return hosts, port
}
// Print listen ips.
func printListenIPs(tls bool, hosts []string, port string) {
for _, host := range hosts {
if tls {
helper.Logger.Info(fmt.Sprintf("https://%s:%s", host, port))
} else {
helper.Logger.Info(fmt.Sprintf("http://%s:%s", host, port))
}
}
}
// Extract port number from address address should be of the form host:port.
func getPort(address string) int {
_, portStr, err := net.SplitHostPort(address)
helper.PanicOnError(err, "Unable to parse host port.")
portInt, err := strconv.Atoi(portStr)
helper.PanicOnError(err, "Invalid port number.")
return portInt
}
// Make sure that none of the other processes are listening on the
// specified port on any of the interfaces.
//
// On linux if a process is listening on 127.0.0.1:9000 then Listen()
// on ":9000" fails with the error "port already in use".
// However on macOS Listen() on ":9000" falls back to the IPv6 address.
// This causes confusion on macOS that minio server is not reachable
// on 127.0.0.1 even though minio server is running. So before we start
// the minio server we make sure that the port is free on all the IPs.
func checkPortAvailability(port int) {
isAddrInUse := func(err error) bool {
// Check if the syscall error is EADDRINUSE.
// EADDRINUSE is the system call error if another process is
// already listening at the specified port.
neterr, ok := err.(*net.OpError)
if !ok {
return false
}
osErr, ok := neterr.Err.(*os.SyscallError)
if !ok {
return false
}
sysErr, ok := osErr.Err.(syscall.Errno)
if !ok {
return false
}
if sysErr != syscall.EADDRINUSE {
return false
}
return true
}
ifcs, err := net.Interfaces()
if err != nil {
helper.PanicOnError(err, "Unable to list interfaces.")
}
for _, ifc := range ifcs {
addrs, err := ifc.Addrs()
if err != nil {
helper.PanicOnError(err,
fmt.Sprintf("Unable to list addresses on interface %s.", ifc.Name))
}
for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok {
helper.Logger.Error("Failed to assert type on (*net.IPNet) interface.")
continue
}
ip := ipnet.IP
network := "tcp4"
if ip.To4() == nil {
network = "tcp6"
}
tcpAddr := net.TCPAddr{IP: ip, Port: port, Zone: ifc.Name}
l, err := net.ListenTCP(network, &tcpAddr)
if err != nil {
if isAddrInUse(err) {
// Fail if port is already in use.
helper.PanicOnError(err,
fmt.Sprintf("Unable to listen on %s:%.d.",
tcpAddr.IP, tcpAddr.Port))
} else {
// Ignore other errors.
continue
}
}
err = l.Close()
helper.PanicOnError(err,
fmt.Sprintf("Unable to close listener on %s:%.d.",
tcpAddr.IP, tcpAddr.Port))
}
}
}
func isSSL(c *ServerConfig) bool {
if helper.FileExists(c.KeyFilePath) && helper.FileExists(c.CertFilePath) {
return true
}
return false
}
var ApiServer *api.Server
// blocks after server started
func startApiServer(c *ServerConfig) {
serverAddress := c.Address
host, port, _ := net.SplitHostPort(serverAddress)
// If port empty, default to port '80'
if port == "" {
port = "80"
// if SSL is enabled, choose port as "443" instead.
if isSSL(c) {
port = "443"
}
}
// Check if requested port is available.
checkPortAvailability(getPort(net.JoinHostPort(host, port)))
// Configure server.
apiServer := configureServer(c)
hosts, port := getListenIPs(apiServer.Server) // get listen ips and port.
tls := apiServer.Server.TLSConfig != nil // 'true' if TLS is enabled.
helper.Logger.Info("S3 Object Storage:")
// Print api listen ips.
printListenIPs(tls, hosts, port)
go func() {
listener, err := helper.ReusePortListener(host, port)
helper.PanicOnError(err, "API server error.")
// Configure TLS if certs are available.
if isSSL(c) {
err = apiServer.Server.ServeTLS(listener, c.CertFilePath, c.KeyFilePath)
} else {
// Fallback to http.
err = apiServer.Server.Serve(listener)
}
helper.PanicOnError(err, "API server error.")
}()
}
func stopApiServer() {
ApiServer.Stop()
}