2015-01-21 02:16:06 -05:00
|
|
|
/*
|
|
|
|
* Mini Object Storage, (C) 2014 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.
|
|
|
|
*/
|
|
|
|
|
2015-01-18 16:31:22 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2015-01-27 20:47:11 -05:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
2015-01-18 16:31:22 -05:00
|
|
|
"reflect"
|
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
"github.com/minio-io/minio/pkg/api/minioapi"
|
|
|
|
"github.com/minio-io/minio/pkg/api/webuiapi"
|
2015-01-18 16:31:22 -05:00
|
|
|
"github.com/minio-io/minio/pkg/httpserver"
|
2015-01-21 15:44:09 -05:00
|
|
|
mstorage "github.com/minio-io/minio/pkg/storage"
|
2015-01-27 20:47:11 -05:00
|
|
|
"github.com/minio-io/minio/pkg/storage/fs"
|
2015-01-21 15:44:09 -05:00
|
|
|
"github.com/minio-io/minio/pkg/storage/inmemory"
|
2015-01-18 16:31:22 -05:00
|
|
|
)
|
|
|
|
|
2015-01-28 19:07:53 -05:00
|
|
|
type ServerConfig struct {
|
2015-02-01 05:35:00 -05:00
|
|
|
Address string
|
|
|
|
Tls bool
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
ApiType interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MinioApi struct {
|
2015-01-28 19:07:53 -05:00
|
|
|
StorageType StorageType
|
|
|
|
}
|
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
type WebUIApi struct {
|
|
|
|
Websocket bool
|
|
|
|
}
|
|
|
|
|
2015-01-28 19:07:53 -05:00
|
|
|
type StorageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
InMemoryStorage = iota
|
|
|
|
FileStorage
|
|
|
|
)
|
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
func getHttpChannels(configs []ServerConfig) (ctrlChans []chan<- string, statusChans []<-chan error) {
|
2015-01-28 19:29:47 -05:00
|
|
|
// a pair of control channels, we use these primarily to add to the lists above
|
2015-01-21 15:44:09 -05:00
|
|
|
var ctrlChan chan<- string
|
|
|
|
var statusChan <-chan error
|
2015-01-28 19:29:47 -05:00
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
for _, config := range configs {
|
|
|
|
switch k := config.ApiType.(type) {
|
|
|
|
case MinioApi:
|
|
|
|
{
|
|
|
|
// configure web server
|
|
|
|
var storage mstorage.Storage
|
|
|
|
var httpConfig = httpserver.HttpServerConfig{}
|
|
|
|
httpConfig.Address = config.Address
|
|
|
|
httpConfig.Websocket = false
|
|
|
|
httpConfig.TLS = config.Tls
|
2015-01-25 20:15:18 -05:00
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
if config.CertFile != "" {
|
|
|
|
httpConfig.CertFile = config.CertFile
|
|
|
|
}
|
|
|
|
if config.KeyFile != "" {
|
|
|
|
httpConfig.KeyFile = config.KeyFile
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrlChans, statusChans, storage = getStorageChannels(k.StorageType)
|
|
|
|
// start minio api in a web server, pass storage driver into it
|
|
|
|
ctrlChan, statusChan, _ = httpserver.Start(minioapi.HttpHandler(storage), httpConfig)
|
|
|
|
|
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
|
|
|
|
|
|
|
}
|
|
|
|
case WebUIApi:
|
|
|
|
{
|
|
|
|
var httpConfig = httpserver.HttpServerConfig{}
|
|
|
|
httpConfig.Address = config.Address
|
|
|
|
httpConfig.TLS = config.Tls
|
|
|
|
httpConfig.CertFile = config.CertFile
|
|
|
|
httpConfig.KeyFile = config.KeyFile
|
|
|
|
|
|
|
|
httpConfig.Websocket = k.Websocket
|
|
|
|
ctrlChan, statusChan, _ = httpserver.Start(webuiapi.HttpHandler(), httpConfig)
|
|
|
|
|
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Fatal("Invalid api type")
|
|
|
|
}
|
2015-01-25 20:15:18 -05:00
|
|
|
}
|
2015-02-01 05:35:00 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStorageChannels(storageType StorageType) (ctrlChans []chan<- string, statusChans []<-chan error, storage mstorage.Storage) {
|
|
|
|
// a pair of control channels, we use these primarily to add to the lists above
|
|
|
|
var ctrlChan chan<- string
|
|
|
|
var statusChan <-chan error
|
2015-01-21 15:44:09 -05:00
|
|
|
|
2015-01-28 19:29:47 -05:00
|
|
|
// instantiate storage
|
|
|
|
// preconditions:
|
|
|
|
// - storage type specified
|
|
|
|
// - any configuration for storage is populated
|
|
|
|
// postconditions:
|
|
|
|
// - storage driver is initialized
|
|
|
|
// - ctrlChans has channel to communicate to storage
|
|
|
|
// - statusChans has channel for messages coming from storage
|
|
|
|
switch {
|
2015-02-01 05:35:00 -05:00
|
|
|
case storageType == InMemoryStorage:
|
2015-01-28 19:29:47 -05:00
|
|
|
{
|
|
|
|
ctrlChan, statusChan, storage = inmemory.Start()
|
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-02-01 05:35:00 -05:00
|
|
|
case storageType == FileStorage:
|
2015-01-28 19:29:47 -05:00
|
|
|
{
|
|
|
|
// TODO Replace this with a more configurable and robust version
|
|
|
|
currentUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rootPath := path.Join(currentUser.HomeDir, "minio-storage")
|
|
|
|
_, err = os.Stat(rootPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.Mkdir(rootPath, 0700)
|
|
|
|
} else if err != nil {
|
|
|
|
log.Fatal("Could not create $HOME/minio-storage", err)
|
|
|
|
}
|
|
|
|
ctrlChan, statusChan, storage = fs.Start(rootPath)
|
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-01-28 19:29:47 -05:00
|
|
|
default: // should never happen
|
|
|
|
log.Fatal("No storage driver found")
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-02-01 05:35:00 -05:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 16:31:22 -05:00
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
func Start(configs []ServerConfig) {
|
2015-01-28 19:33:20 -05:00
|
|
|
// listen for critical errors
|
|
|
|
// TODO Handle critical errors appropriately when they arise
|
|
|
|
// reflected looping is necessary to remove dead channels from loop and not flood switch
|
2015-02-01 05:35:00 -05:00
|
|
|
_, statusChans := getHttpChannels(configs)
|
2015-01-18 16:31:22 -05:00
|
|
|
cases := createSelectCases(statusChans)
|
2015-01-18 19:59:23 -05:00
|
|
|
for len(cases) > 0 {
|
2015-01-18 16:31:22 -05:00
|
|
|
chosen, value, recvOk := reflect.Select(cases)
|
|
|
|
if recvOk == true {
|
|
|
|
// Status Message Received
|
|
|
|
log.Println(chosen, value.Interface(), recvOk)
|
|
|
|
} else {
|
|
|
|
// Channel closed, remove from list
|
2015-01-20 17:07:39 -05:00
|
|
|
var aliveStatusChans []<-chan error
|
2015-01-18 16:31:22 -05:00
|
|
|
for i, ch := range statusChans {
|
|
|
|
if i != chosen {
|
|
|
|
aliveStatusChans = append(aliveStatusChans, ch)
|
|
|
|
}
|
|
|
|
}
|
2015-01-18 18:50:53 -05:00
|
|
|
// create new select cases without defunct channel
|
2015-01-18 16:31:22 -05:00
|
|
|
statusChans = aliveStatusChans
|
|
|
|
cases = createSelectCases(statusChans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 19:33:20 -05:00
|
|
|
// creates select cases for reflect to switch over dynamically
|
|
|
|
// this is necessary in order to remove dead channels and not flood
|
|
|
|
// the loop with closed channel errors
|
2015-01-18 16:31:22 -05:00
|
|
|
func createSelectCases(channels []<-chan error) []reflect.SelectCase {
|
|
|
|
cases := make([]reflect.SelectCase, len(channels))
|
|
|
|
for i, ch := range channels {
|
|
|
|
cases[i] = reflect.SelectCase{
|
|
|
|
Dir: reflect.SelectRecv,
|
|
|
|
Chan: reflect.ValueOf(ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cases
|
|
|
|
}
|