2015-01-18 16:31:22 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2015-01-18 18:50:53 -05:00
|
|
|
"net/http"
|
2015-01-18 16:31:22 -05:00
|
|
|
"reflect"
|
|
|
|
|
2015-01-18 18:50:53 -05:00
|
|
|
"github.com/gorilla/mux"
|
2015-01-18 16:31:22 -05:00
|
|
|
"github.com/minio-io/minio/pkg/httpserver"
|
2015-01-18 19:10:48 -05:00
|
|
|
storageModule "github.com/minio-io/minio/pkg/storage"
|
2015-01-18 16:31:22 -05:00
|
|
|
)
|
|
|
|
|
2015-01-18 19:10:48 -05:00
|
|
|
var storage *storageModule.Storage
|
|
|
|
|
2015-01-18 16:31:22 -05:00
|
|
|
func Start() {
|
|
|
|
ctrlChans := make([]chan<- string, 0)
|
|
|
|
statusChans := make([]<-chan error, 0)
|
|
|
|
|
2015-01-18 19:10:48 -05:00
|
|
|
ctrlChan, statusChan, storageSystem := storageModule.Start()
|
2015-01-18 16:31:22 -05:00
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
2015-01-18 19:10:48 -05:00
|
|
|
storage = storageSystem
|
2015-01-18 16:31:22 -05:00
|
|
|
|
2015-01-18 18:50:53 -05:00
|
|
|
ctrlChan, statusChan = httpserver.Start(getHttpHandler())
|
2015-01-18 16:31:22 -05:00
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
|
|
|
|
|
|
|
cases := createSelectCases(statusChans)
|
|
|
|
|
|
|
|
for {
|
|
|
|
chosen, value, recvOk := reflect.Select(cases)
|
|
|
|
if recvOk == true {
|
|
|
|
// Status Message Received
|
|
|
|
log.Println(chosen, value.Interface(), recvOk)
|
|
|
|
} else {
|
|
|
|
// Channel closed, remove from list
|
|
|
|
aliveStatusChans := make([]<-chan error, 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-01-18 18:50:53 -05:00
|
|
|
|
|
|
|
func getHttpHandler() http.Handler {
|
|
|
|
mux := mux.NewRouter()
|
2015-01-18 19:23:17 -05:00
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", getObjectHandler).Methods("GET")
|
2015-01-18 18:50:53 -05:00
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2015-01-18 19:23:17 -05:00
|
|
|
func getObjectHandler(w http.ResponseWriter, req *http.Request) {
|
2015-01-18 19:10:48 -05:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
object := vars["object"]
|
|
|
|
storage.CopyObjectToWriter(w, bucket, object)
|
2015-01-18 18:50:53 -05:00
|
|
|
}
|