Merge pull request #85 from fkautz/pr_out_http_server_package_now_follows_convention

This commit is contained in:
Frederick F. Kautz IV 2015-01-28 16:30:02 -08:00
commit 698f0b815f
2 changed files with 59 additions and 37 deletions

View file

@ -22,38 +22,41 @@ import (
"time"
)
type HttpServer struct {
type HttpServerConfig struct {
Address string
TLS bool
CertFile string
KeyFile string
}
func Start(handler http.Handler, srv HttpServer) (chan<- string, <-chan error) {
type HttpServer struct{}
func Start(handler http.Handler, config HttpServerConfig) (chan<- string, <-chan error, *HttpServer) {
ctrlChannel := make(chan string)
errorChannel := make(chan error)
go start(ctrlChannel, errorChannel, handler, srv)
return ctrlChannel, errorChannel
server := HttpServer{}
go start(ctrlChannel, errorChannel, handler, config, &server)
return ctrlChannel, errorChannel, &server
}
func start(ctrlChannel <-chan string, errorChannel chan<- error, router http.Handler, srv HttpServer) {
func start(ctrlChannel <-chan string, errorChannel chan<- error, router http.Handler, config HttpServerConfig, server *HttpServer) {
var err error
// Minio server config
server := &http.Server{
Addr: srv.Address,
httpServer := &http.Server{
Addr: config.Address,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Println("Starting HTTP Server on " + srv.Address)
log.Println("Starting HTTP Server on:", config.Address)
if srv.TLS {
server.TLSConfig = getDefaultTLSConfig()
err = server.ListenAndServeTLS(srv.CertFile, srv.KeyFile)
if config.TLS {
httpServer.TLSConfig = getDefaultTLSConfig()
err = httpServer.ListenAndServeTLS(config.CertFile, config.KeyFile)
} else {
err = server.ListenAndServe()
err = httpServer.ListenAndServe()
}
errorChannel <- err
close(errorChannel)

View file

@ -46,47 +46,66 @@ const (
)
func Start(config ServerConfig) {
// maintain a list of input and output channels for communicating with services
var ctrlChans []chan<- string
var statusChans []<-chan error
// a pair of control channels, we use these primarily to add to the lists above
var ctrlChan chan<- string
var statusChan <-chan error
// configure web server
var storage mstorage.Storage
var srv = httpserver.HttpServer{}
srv.Address = config.Address
srv.TLS = config.Tls
var httpConfig = httpserver.HttpServerConfig{}
httpConfig.Address = config.Address
httpConfig.TLS = config.Tls
if config.CertFile != "" {
srv.CertFile = config.CertFile
httpConfig.CertFile = config.CertFile
}
if config.KeyFile != "" {
srv.KeyFile = config.KeyFile
httpConfig.KeyFile = config.KeyFile
}
if config.StorageType == InMemoryStorage {
ctrlChan, statusChan, storage = inmemory.Start()
ctrlChans = append(ctrlChans, ctrlChan)
statusChans = append(statusChans, statusChan)
} else if config.StorageType == FileStorage {
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
// 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 {
case config.StorageType == InMemoryStorage:
{
ctrlChan, statusChan, storage = inmemory.Start()
ctrlChans = append(ctrlChans, ctrlChan)
statusChans = append(statusChans, statusChan)
}
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)
case config.StorageType == FileStorage:
{
// 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)
}
ctrlChan, statusChan, storage = fs.Start(rootPath)
ctrlChans = append(ctrlChans, ctrlChan)
statusChans = append(statusChans, statusChan)
} else {
default: // should never happen
log.Fatal("No storage driver found")
}
ctrlChan, statusChan = httpserver.Start(minioapi.HttpHandler(storage), srv)
// 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)