You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
5.9 KiB
213 lines
5.9 KiB
![]()
8 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"net/rpc"
|
||
|
"strconv"
|
||
|
|
||
![]()
8 years ago
|
"github.com/Sirupsen/logrus"
|
||
![]()
8 years ago
|
router "github.com/gorilla/mux"
|
||
|
)
|
||
|
|
||
|
// Storage server implements rpc primitives to facilitate exporting a
|
||
|
// disk over a network.
|
||
|
type storageServer struct {
|
||
|
storage StorageAPI
|
||
|
}
|
||
|
|
||
|
/// Volume operations handlers
|
||
|
|
||
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
||
|
func (s *storageServer) MakeVolHandler(arg *string, reply *GenericReply) error {
|
||
![]()
8 years ago
|
err := s.storage.MakeVol(*arg)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": *arg,
|
||
|
}).Debugf("MakeVol failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
![]()
8 years ago
|
}
|
||
|
|
||
|
// ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
|
||
|
func (s *storageServer) ListVolsHandler(arg *string, reply *ListVolsReply) error {
|
||
|
vols, err := s.storage.ListVols()
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.Debugf("Listsvols failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
|
reply.Vols = vols
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// StatVolHandler - stat vol handler is a rpc wrapper for StatVol operation.
|
||
|
func (s *storageServer) StatVolHandler(arg *string, reply *VolInfo) error {
|
||
|
volInfo, err := s.storage.StatVol(*arg)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": *arg,
|
||
|
}).Debugf("StatVol failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
|
*reply = volInfo
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// DeleteVolHandler - delete vol handler is a rpc wrapper for
|
||
|
// DeleteVol operation.
|
||
|
func (s *storageServer) DeleteVolHandler(arg *string, reply *GenericReply) error {
|
||
![]()
8 years ago
|
err := s.storage.DeleteVol(*arg)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": *arg,
|
||
|
}).Debugf("DeleteVol failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
![]()
8 years ago
|
}
|
||
|
|
||
|
/// File operations
|
||
|
|
||
|
// ListFilesHandler - list files handler.
|
||
|
func (s *storageServer) ListFilesHandler(arg *ListFilesArgs, reply *ListFilesReply) error {
|
||
|
files, eof, err := s.storage.ListFiles(arg.Vol, arg.Prefix, arg.Marker, arg.Recursive, arg.Count)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": arg.Vol,
|
||
|
"prefix": arg.Prefix,
|
||
|
"marker": arg.Marker,
|
||
|
"recursive": arg.Recursive,
|
||
|
"count": arg.Count,
|
||
|
}).Debugf("ListFiles failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
// Fill reply structure.
|
||
![]()
8 years ago
|
reply.Files = files
|
||
|
reply.EOF = eof
|
||
![]()
8 years ago
|
|
||
|
// Return success.
|
||
![]()
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
// StatFileHandler - stat file handler is rpc wrapper to stat file.
|
||
|
func (s *storageServer) StatFileHandler(arg *StatFileArgs, reply *FileInfo) error {
|
||
|
fileInfo, err := s.storage.StatFile(arg.Vol, arg.Path)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": arg.Vol,
|
||
|
"path": arg.Path,
|
||
|
}).Debugf("StatFile failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
|
*reply = fileInfo
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// DeleteFileHandler - delete file handler is rpc wrapper to delete file.
|
||
|
func (s *storageServer) DeleteFileHandler(arg *DeleteFileArgs, reply *GenericReply) error {
|
||
![]()
8 years ago
|
err := s.storage.DeleteFile(arg.Vol, arg.Path)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": arg.Vol,
|
||
|
"path": arg.Path,
|
||
|
}).Debugf("DeleteFile failed with error %s", err)
|
||
![]()
8 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
// Initialize new storage rpc.
|
||
![]()
8 years ago
|
func newRPCServer(exportPath string) (*storageServer, error) {
|
||
|
// Initialize posix storage API.
|
||
|
storage, err := newPosix(exportPath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
return &storageServer{
|
||
|
storage: storage,
|
||
|
}, nil
|
||
![]()
8 years ago
|
}
|
||
|
|
||
|
// registerStorageRPCRouter - register storage rpc router.
|
||
|
func registerStorageRPCRouter(mux *router.Router, stServer *storageServer) {
|
||
![]()
8 years ago
|
storageRPCServer := rpc.NewServer()
|
||
![]()
8 years ago
|
storageRPCServer.RegisterName("Storage", stServer)
|
||
|
storageRouter := mux.NewRoute().PathPrefix(reservedBucket).Subrouter()
|
||
![]()
8 years ago
|
// Add minio storage routes.
|
||
![]()
8 years ago
|
storageRouter.Path("/storage").Handler(storageRPCServer)
|
||
![]()
8 years ago
|
// StreamUpload - stream upload handler.
|
||
![]()
8 years ago
|
storageRouter.Methods("POST").Path("/storage/upload/{volume}/{path:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
![]()
8 years ago
|
vars := router.Vars(r)
|
||
|
volume := vars["volume"]
|
||
|
path := vars["path"]
|
||
|
writeCloser, err := stServer.storage.CreateFile(volume, path)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
|
}).Debugf("CreateFile failed with error %s", err)
|
||
![]()
8 years ago
|
httpErr := http.StatusInternalServerError
|
||
|
if err == errVolumeNotFound {
|
||
|
httpErr = http.StatusNotFound
|
||
|
} else if err == errIsNotRegular {
|
||
|
httpErr = http.StatusConflict
|
||
|
}
|
||
|
http.Error(w, err.Error(), httpErr)
|
||
![]()
8 years ago
|
return
|
||
|
}
|
||
|
reader := r.Body
|
||
|
if _, err = io.Copy(writeCloser, reader); err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
|
}).Debugf("Copying incoming reader to writer failed %s", err)
|
||
![]()
8 years ago
|
safeCloseAndRemove(writeCloser)
|
||
![]()
8 years ago
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
writeCloser.Close()
|
||
|
reader.Close()
|
||
|
})
|
||
|
// StreamDownloadHandler - stream download handler.
|
||
![]()
8 years ago
|
storageRouter.Methods("GET").Path("/storage/download/{volume}/{path:.+}").Queries("offset", "{offset:.*}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
![]()
8 years ago
|
vars := router.Vars(r)
|
||
|
volume := vars["volume"]
|
||
|
path := vars["path"]
|
||
|
offset, err := strconv.ParseInt(r.URL.Query().Get("offset"), 10, 64)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
|
}).Debugf("Parse offset failure with error %s", err)
|
||
![]()
8 years ago
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
readCloser, err := stServer.storage.ReadFile(volume, path, offset)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
|
}).Debugf("ReadFile failed with error %s", err)
|
||
![]()
8 years ago
|
httpErr := http.StatusBadRequest
|
||
![]()
8 years ago
|
if err == errVolumeNotFound {
|
||
|
httpErr = http.StatusNotFound
|
||
|
} else if err == errFileNotFound {
|
||
![]()
8 years ago
|
httpErr = http.StatusNotFound
|
||
|
}
|
||
|
http.Error(w, err.Error(), httpErr)
|
||
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
// Copy reader to writer.
|
||
![]()
8 years ago
|
io.Copy(w, readCloser)
|
||
![]()
8 years ago
|
|
||
|
// Flush out any remaining buffers to client.
|
||
![]()
8 years ago
|
w.(http.Flusher).Flush()
|
||
![]()
8 years ago
|
|
||
|
// Close the reader.
|
||
![]()
8 years ago
|
readCloser.Close()
|
||
|
})
|
||
![]()
8 years ago
|
}
|