2015-01-21 08:16:06 +01:00
|
|
|
/*
|
2015-03-19 22:35:50 +01:00
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
2015-01-21 08:16:06 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
package api
|
2015-01-18 22:31:22 +01:00
|
|
|
|
|
|
|
import (
|
2015-06-04 03:27:13 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-01-18 22:31:22 +01:00
|
|
|
"net/http"
|
2015-06-04 03:27:13 +02:00
|
|
|
"strings"
|
2015-01-18 22:31:22 +01:00
|
|
|
)
|
|
|
|
|
2015-03-06 06:07:19 +01:00
|
|
|
// Config - http server config
|
|
|
|
type Config struct {
|
2015-06-07 02:01:39 +02:00
|
|
|
Address string
|
|
|
|
TLS bool
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
RateLimit int
|
2015-01-26 02:15:18 +01:00
|
|
|
}
|
|
|
|
|
2015-03-03 11:36:12 +01:00
|
|
|
// Start http server
|
2015-06-30 23:42:29 +02:00
|
|
|
func Start(a API) <-chan error {
|
|
|
|
errCh := make(chan error)
|
|
|
|
go start(errCh, a)
|
|
|
|
return errCh
|
2015-01-18 22:31:22 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
func start(errCh chan error, a API) {
|
|
|
|
defer close(errCh)
|
2015-01-26 02:15:18 +01:00
|
|
|
|
2015-06-29 08:53:53 +02:00
|
|
|
var err error
|
2015-01-21 09:50:23 +01:00
|
|
|
// Minio server config
|
2015-01-29 01:29:47 +01:00
|
|
|
httpServer := &http.Server{
|
2015-06-30 23:42:29 +02:00
|
|
|
Addr: a.config.Address,
|
|
|
|
Handler: a.handler,
|
2015-01-21 09:50:23 +01:00
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}
|
2015-06-04 03:27:13 +02:00
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
host, port, err := net.SplitHostPort(a.config.Address)
|
2015-06-29 08:53:53 +02:00
|
|
|
if err != nil {
|
2015-06-30 23:42:29 +02:00
|
|
|
errCh <- err
|
2015-06-29 08:53:53 +02:00
|
|
|
return
|
|
|
|
}
|
2015-06-04 03:27:13 +02:00
|
|
|
|
|
|
|
var hosts []string
|
|
|
|
switch {
|
|
|
|
case host != "":
|
|
|
|
hosts = append(hosts, host)
|
|
|
|
default:
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
2015-06-29 09:12:28 +02:00
|
|
|
if err != nil {
|
2015-06-30 23:42:29 +02:00
|
|
|
errCh <- err
|
2015-06-29 09:12:28 +02:00
|
|
|
return
|
|
|
|
}
|
2015-06-04 03:27:13 +02:00
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr.Network() == "ip+net" {
|
2015-06-29 08:53:53 +02:00
|
|
|
host := strings.Split(addr.String(), "/")[0]
|
|
|
|
if ip := net.ParseIP(host); ip.To4() != nil {
|
|
|
|
hosts = append(hosts, host)
|
2015-06-04 03:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 09:14:58 +02:00
|
|
|
switch {
|
|
|
|
default:
|
2015-06-04 03:27:13 +02:00
|
|
|
for _, host := range hosts {
|
|
|
|
fmt.Printf("Starting minio server on: http://%s:%s\n", host, port)
|
|
|
|
}
|
2015-05-07 09:14:58 +02:00
|
|
|
err = httpServer.ListenAndServe()
|
2015-06-30 23:42:29 +02:00
|
|
|
case a.config.TLS == true:
|
2015-06-09 05:10:59 +02:00
|
|
|
for _, host := range hosts {
|
|
|
|
fmt.Printf("Starting minio server on: https://%s:%s\n", host, port)
|
|
|
|
}
|
2015-06-30 23:42:29 +02:00
|
|
|
err = httpServer.ListenAndServeTLS(a.config.CertFile, a.config.KeyFile)
|
2015-01-26 02:15:18 +01:00
|
|
|
}
|
2015-06-29 08:53:53 +02:00
|
|
|
if err != nil {
|
2015-06-30 23:42:29 +02:00
|
|
|
errCh <- err
|
2015-06-29 08:53:53 +02:00
|
|
|
}
|
2015-06-30 23:42:29 +02:00
|
|
|
errCh <- nil
|
|
|
|
return
|
|
|
|
}
|
2015-06-29 08:53:53 +02:00
|
|
|
|
2015-06-30 23:42:29 +02:00
|
|
|
// API is used to build api server
|
|
|
|
type API struct {
|
|
|
|
config Config
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartServer APIFactory builds api server
|
|
|
|
func StartServer(conf Config) error {
|
|
|
|
for err := range Start(New(conf)) {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-01-18 22:31:22 +01:00
|
|
|
}
|