diff --git a/main.go b/main.go index 26db6db65..b59530386 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,15 @@ package main import ( + "log" + "github.com/minio-io/minio/pkg/server" "github.com/spf13/cobra" ) func main() { var tls bool - var inmemory bool + var storageTypeStr string var address string var certFile string var keyFile string @@ -16,13 +18,36 @@ func main() { Short: "minio is a minimal object storage system", Long: "", Run: func(cmd *cobra.Command, args []string) { - server.Start(address, tls, certFile, keyFile, inmemory) + storageType := getStorageType(storageTypeStr) + serverConfig := server.ServerConfig{ + Address: address, + Tls: tls, + CertFile: certFile, + KeyFile: keyFile, + StorageType: storageType, + } + server.Start(serverConfig) }, } minioCommand.PersistentFlags().BoolVarP(&tls, "tls", "t", false, "enable tls") - minioCommand.PersistentFlags().BoolVarP(&inmemory, "inmemory", "m", false, "in memory object storage") + minioCommand.PersistentFlags().StringVarP(&storageTypeStr, "storage-type", "s", "file", "file,inmemory") minioCommand.PersistentFlags().StringVarP(&address, "http-address", "a", ":8080", "http address") minioCommand.PersistentFlags().StringVarP(&certFile, "cert", "c", "", "cert file path") minioCommand.PersistentFlags().StringVarP(&keyFile, "key", "k", "", "key file path") minioCommand.Execute() } + +func getStorageType(input string) server.StorageType { + switch { + case input == "file": + return server.InMemoryStorage + case input == "inmemory": + return server.InMemoryStorage + default: + { + log.Fatal("Unknown server type:", input) + // needed for compile, should never return after fatal log msg + return server.InMemoryStorage + } + } +} diff --git a/pkg/server/server.go b/pkg/server/server.go index b797f1c27..45a239d6b 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -30,7 +30,22 @@ import ( "github.com/minio-io/minio/pkg/webapi/minioapi" ) -func Start(hostname string, tls bool, certFile, keyFile string, inMemoryStorage bool) { +type ServerConfig struct { + Address string + Tls bool + CertFile string + KeyFile string + StorageType StorageType +} + +type StorageType int + +const ( + InMemoryStorage = iota + FileStorage +) + +func Start(config ServerConfig) { var ctrlChans []chan<- string var statusChans []<-chan error @@ -38,21 +53,21 @@ func Start(hostname string, tls bool, certFile, keyFile string, inMemoryStorage var statusChan <-chan error var storage mstorage.Storage var srv = httpserver.HttpServer{} - srv.Address = hostname - srv.TLS = tls + srv.Address = config.Address + srv.TLS = config.Tls - if certFile != "" { - srv.CertFile = certFile + if config.CertFile != "" { + srv.CertFile = config.CertFile } - if keyFile != "" { - srv.KeyFile = keyFile + if config.KeyFile != "" { + srv.KeyFile = config.KeyFile } - if inMemoryStorage { + if config.StorageType == InMemoryStorage { ctrlChan, statusChan, storage = inmemory.Start() ctrlChans = append(ctrlChans, ctrlChan) statusChans = append(statusChans, statusChan) - } else { + } else if config.StorageType == FileStorage { currentUser, err := user.Current() if err != nil { log.Fatal(err) @@ -67,6 +82,8 @@ func Start(hostname string, tls bool, certFile, keyFile string, inMemoryStorage ctrlChan, statusChan, storage = fs.Start(rootPath) ctrlChans = append(ctrlChans, ctrlChan) statusChans = append(statusChans, statusChan) + } else { + } ctrlChan, statusChan = httpserver.Start(minioapi.HttpHandler(storage), srv)