parent
335c7827eb
commit
4addf7a996
@ -1,54 +0,0 @@ |
||||
/* |
||||
* Minimalist Object Storage, (C) 2014 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package api |
||||
|
||||
import router "github.com/gorilla/mux" |
||||
|
||||
type minioAPI struct{} |
||||
|
||||
// New api
|
||||
func New(config Config) API { |
||||
var api = minioAPI{} |
||||
|
||||
mux := router.NewRouter() |
||||
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET") |
||||
mux.HandleFunc("/{bucket}", api.listObjectsHandler).Methods("GET") |
||||
mux.HandleFunc("/{bucket}", api.putBucketHandler).Methods("PUT") |
||||
mux.HandleFunc("/{bucket}", api.headBucketHandler).Methods("HEAD") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.headObjectHandler).Methods("HEAD") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}").Methods("PUT") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.listObjectPartsHandler).Queries("uploadId", "{uploadId:.*}").Methods("GET") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.completeMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}").Methods("POST") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.newMultipartUploadHandler).Methods("POST") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.abortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}").Methods("DELETE") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT") |
||||
|
||||
// not implemented yet
|
||||
mux.HandleFunc("/{bucket}", api.deleteBucketHandler).Methods("DELETE") |
||||
|
||||
// unsupported API
|
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.deleteObjectHandler).Methods("DELETE") |
||||
|
||||
handler := validContentTypeHandler(mux) |
||||
handler = timeValidityHandler(handler) |
||||
handler = ignoreResourcesHandler(handler) |
||||
handler = validateAuthHeaderHandler(handler) |
||||
handler = rateLimitHandler(handler, config.RateLimit) |
||||
handler = loggingHandler(handler) |
||||
return API{config, handler} |
||||
} |
@ -1,135 +0,0 @@ |
||||
/* |
||||
* Minimalist Object Storage, (C) 2015 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package web |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/json" |
||||
"net/http" |
||||
"path/filepath" |
||||
|
||||
"github.com/gorilla/mux" |
||||
"github.com/minio/minio/pkg/api/config" |
||||
"github.com/minio/minio/pkg/iodine" |
||||
"github.com/minio/minio/pkg/utils/crypto/keys" |
||||
"github.com/minio/minio/pkg/utils/log" |
||||
) |
||||
|
||||
const ( |
||||
defaultWeb = "polygon" |
||||
) |
||||
|
||||
type webAPI struct { |
||||
conf config.Config |
||||
webPath string |
||||
} |
||||
|
||||
// No encoder interface exists, so we create one.
|
||||
type encoder interface { |
||||
Encode(v interface{}) error |
||||
} |
||||
|
||||
// HTTPHandler - http wrapper handler
|
||||
func HTTPHandler() http.Handler { |
||||
mux := mux.NewRouter() |
||||
var api = webAPI{} |
||||
|
||||
if err := api.conf.SetupConfig(); err != nil { |
||||
log.Fatal(iodine.New(err, nil)) |
||||
} |
||||
|
||||
api.webPath = filepath.Join(api.conf.GetConfigPath(), defaultWeb) |
||||
mux.Handle("/{polygon:.*}", http.FileServer(http.Dir(api.webPath))).Methods("GET") |
||||
mux.HandleFunc("/access", api.accessHandler).Methods("POST") |
||||
return mux |
||||
} |
||||
|
||||
func writeResponse(w http.ResponseWriter, response interface{}) []byte { |
||||
var bytesBuffer bytes.Buffer |
||||
var encoder encoder |
||||
w.Header().Set("Content-Type", "application/json") |
||||
encoder = json.NewEncoder(&bytesBuffer) |
||||
w.Header().Set("Server", "Minio Management Console") |
||||
w.Header().Set("Connection", "close") |
||||
encoder.Encode(response) |
||||
return bytesBuffer.Bytes() |
||||
} |
||||
|
||||
func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) { |
||||
var err error |
||||
var accesskey, secretkey []byte |
||||
username := req.FormValue("username") |
||||
if len(username) <= 0 { |
||||
w.WriteHeader(http.StatusBadRequest) |
||||
return |
||||
} |
||||
|
||||
err = web.conf.ReadConfig() |
||||
if err != nil { |
||||
log.Error.Println(iodine.New(err, nil)) |
||||
w.WriteHeader(http.StatusInternalServerError) |
||||
w.Write([]byte(err.Error())) |
||||
return |
||||
} |
||||
|
||||
if web.conf.IsUserExists(username) { |
||||
w.WriteHeader(http.StatusConflict) |
||||
return |
||||
} |
||||
|
||||
var user = config.User{} |
||||
user.Name = username |
||||
|
||||
accesskey, err = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID) |
||||
if err != nil { |
||||
log.Error.Println(iodine.New(err, nil)) |
||||
w.WriteHeader(http.StatusInternalServerError) |
||||
w.Write([]byte(err.Error())) |
||||
return |
||||
} |
||||
user.AccessKey = string(accesskey) |
||||
|
||||
secretkey, err = keys.GenerateRandomBase64(keys.MinioSecretID) |
||||
if err != nil { |
||||
log.Error.Println(iodine.New(err, nil)) |
||||
w.WriteHeader(http.StatusInternalServerError) |
||||
w.Write([]byte(err.Error())) |
||||
return |
||||
} |
||||
user.SecretKey = string(secretkey) |
||||
|
||||
web.conf.AddUser(user) |
||||
err = web.conf.WriteConfig() |
||||
if err != nil { |
||||
log.Error.Println(iodine.New(err, nil)) |
||||
w.WriteHeader(http.StatusInternalServerError) |
||||
w.Write([]byte(err.Error())) |
||||
return |
||||
} |
||||
|
||||
err = web.conf.ReadConfig() |
||||
if err != nil { |
||||
log.Error.Println(iodine.New(err, nil)) |
||||
w.WriteHeader(http.StatusInternalServerError) |
||||
w.Write([]byte(err.Error())) |
||||
return |
||||
} |
||||
|
||||
// Get user back for sending it over HTTP reply
|
||||
user = web.conf.GetUser(username) |
||||
w.Write(writeResponse(w, user)) |
||||
} |
@ -0,0 +1,77 @@ |
||||
/* |
||||
* Minimalist Object Storage, (C) 2014 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package server |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
router "github.com/gorilla/mux" |
||||
jsonRPC "github.com/gorilla/rpc/v2" |
||||
"github.com/minio/minio/pkg/server/api" |
||||
"github.com/minio/minio/pkg/server/rpc" |
||||
) |
||||
|
||||
func registerAPI(mux *router.Router) http.Handler { |
||||
api := api.MinioAPI{} |
||||
|
||||
mux.HandleFunc("/", api.ListBucketsHandler).Methods("GET") |
||||
mux.HandleFunc("/{bucket}", api.ListObjectsHandler).Methods("GET") |
||||
mux.HandleFunc("/{bucket}", api.PutBucketHandler).Methods("PUT") |
||||
mux.HandleFunc("/{bucket}", api.HeadBucketHandler).Methods("HEAD") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.HeadObjectHandler).Methods("HEAD") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}").Methods("PUT") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}").Methods("GET") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}").Methods("POST") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.NewMultipartUploadHandler).Methods("POST") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}").Methods("DELETE") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.GetObjectHandler).Methods("GET") |
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.PutObjectHandler).Methods("PUT") |
||||
|
||||
// not implemented yet
|
||||
mux.HandleFunc("/{bucket}", api.DeleteBucketHandler).Methods("DELETE") |
||||
|
||||
// unsupported API
|
||||
mux.HandleFunc("/{bucket}/{object:.*}", api.DeleteObjectHandler).Methods("DELETE") |
||||
|
||||
return mux |
||||
} |
||||
|
||||
func registerOthers(mux http.Handler, conf api.Config) http.Handler { |
||||
mux = api.ValidContentTypeHandler(mux) |
||||
mux = api.TimeValidityHandler(mux) |
||||
mux = api.IgnoreResourcesHandler(mux) |
||||
mux = api.ValidateAuthHeaderHandler(mux) |
||||
mux = api.RateLimitHandler(mux, conf.RateLimit) |
||||
mux = api.LoggingHandler(mux) |
||||
return mux |
||||
} |
||||
|
||||
func registerRPC(mux *router.Router, r *jsonRPC.Server) http.Handler { |
||||
mux.Handle("/rpc", r) |
||||
return mux |
||||
} |
||||
|
||||
// APIHandler api handler
|
||||
func APIHandler(conf api.Config) http.Handler { |
||||
mux := router.NewRouter() |
||||
return registerOthers(registerAPI(mux), conf) |
||||
} |
||||
|
||||
// RPCHandler rpc handler
|
||||
func RPCHandler() http.Handler { |
||||
return registerRPC(router.NewRouter(), rpc.HelloServiceHandler()) |
||||
} |
@ -0,0 +1,38 @@ |
||||
/* |
||||
* Minimalist Object Storage, (C) 2015 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package rpc |
||||
|
||||
import "net/http" |
||||
|
||||
// HelloArgs - hello args
|
||||
type HelloArgs struct { |
||||
Who string |
||||
} |
||||
|
||||
// HelloReply - hello reply
|
||||
type HelloReply struct { |
||||
Message string |
||||
} |
||||
|
||||
// HelloService -
|
||||
type HelloService struct{} |
||||
|
||||
// Say -
|
||||
func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error { |
||||
reply.Message = "Hello, " + args.Who + "!" |
||||
return nil |
||||
} |
@ -0,0 +1,30 @@ |
||||
/* |
||||
* Minimalist Object Storage, (C) 2015 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package rpc |
||||
|
||||
import ( |
||||
"github.com/gorilla/rpc/v2" |
||||
"github.com/gorilla/rpc/v2/json" |
||||
) |
||||
|
||||
// HelloServiceHandler -
|
||||
func HelloServiceHandler() *rpc.Server { |
||||
s := rpc.NewServer() |
||||
s.RegisterCodec(json.NewCodec(), "application/json") |
||||
s.RegisterService(new(HelloService), "") |
||||
return s |
||||
} |
Loading…
Reference in new issue