HTTP Server package now follows convention

master
Frederick F. Kautz IV 10 years ago
parent e8399a6d05
commit f15e2c4e74
  1. 27
      pkg/httpserver/httpserver.go
  2. 69
      pkg/server/server.go

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

@ -46,47 +46,66 @@ const (
) )
func Start(config ServerConfig) { func Start(config ServerConfig) {
// maintain a list of input and output channels for communicating with services
var ctrlChans []chan<- string var ctrlChans []chan<- string
var statusChans []<-chan error var statusChans []<-chan error
// a pair of control channels, we use these primarily to add to the lists above
var ctrlChan chan<- string var ctrlChan chan<- string
var statusChan <-chan error var statusChan <-chan error
// configure web server
var storage mstorage.Storage var storage mstorage.Storage
var srv = httpserver.HttpServer{} var httpConfig = httpserver.HttpServerConfig{}
srv.Address = config.Address httpConfig.Address = config.Address
srv.TLS = config.Tls httpConfig.TLS = config.Tls
if config.CertFile != "" { if config.CertFile != "" {
srv.CertFile = config.CertFile httpConfig.CertFile = config.CertFile
} }
if config.KeyFile != "" { if config.KeyFile != "" {
srv.KeyFile = config.KeyFile httpConfig.KeyFile = config.KeyFile
} }
if config.StorageType == InMemoryStorage { // instantiate storage
ctrlChan, statusChan, storage = inmemory.Start() // preconditions:
ctrlChans = append(ctrlChans, ctrlChan) // - storage type specified
statusChans = append(statusChans, statusChan) // - any configuration for storage is populated
} else if config.StorageType == FileStorage { // postconditions:
currentUser, err := user.Current() // - storage driver is initialized
if err != nil { // - ctrlChans has channel to communicate to storage
log.Fatal(err) // - 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") case config.StorageType == FileStorage:
_, err = os.Stat(rootPath) {
if os.IsNotExist(err) { // TODO Replace this with a more configurable and robust version
err = os.Mkdir(rootPath, 0700) currentUser, err := user.Current()
} else if err != nil { if err != nil {
log.Fatal("Could not create $HOME/minio-storage", err) 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) default: // should never happen
ctrlChans = append(ctrlChans, ctrlChan) log.Fatal("No storage driver found")
statusChans = append(statusChans, statusChan)
} else {
} }
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) ctrlChans = append(ctrlChans, ctrlChan)
statusChans = append(statusChans, statusChan) statusChans = append(statusChans, statusChan)

Loading…
Cancel
Save