2015-02-15 20:03:27 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
2015-02-15 20:03:27 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-03-18 20:49:33 -04:00
|
|
|
package api
|
2015-02-15 20:03:27 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2015-04-28 23:23:17 -04:00
|
|
|
"bytes"
|
2015-04-28 21:06:09 -04:00
|
|
|
"encoding/json"
|
2015-03-19 01:49:02 -04:00
|
|
|
router "github.com/gorilla/mux"
|
|
|
|
"github.com/minio-io/minio/pkg/api/config"
|
2015-04-24 23:45:44 -04:00
|
|
|
"github.com/minio-io/minio/pkg/api/quota"
|
2015-04-08 21:02:03 -04:00
|
|
|
"github.com/minio-io/minio/pkg/iodine"
|
2015-04-08 19:28:14 -04:00
|
|
|
"github.com/minio-io/minio/pkg/storage/drivers"
|
2015-04-28 23:23:17 -04:00
|
|
|
"io"
|
2015-04-28 21:06:09 -04:00
|
|
|
"os"
|
|
|
|
"time"
|
2015-02-15 20:03:27 -05:00
|
|
|
)
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// private use
|
2015-03-06 00:07:19 -05:00
|
|
|
type minioAPI struct {
|
2015-03-23 23:40:21 -04:00
|
|
|
domain string
|
|
|
|
driver drivers.Driver
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// Path based routing
|
2015-03-19 01:49:02 -04:00
|
|
|
func pathMux(api minioAPI, mux *router.Router) *router.Router {
|
2015-02-23 05:11:27 -05:00
|
|
|
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}", api.listObjectsHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}", api.putBucketHandler).Methods("PUT")
|
2015-04-07 03:44:01 -04:00
|
|
|
mux.HandleFunc("/{bucket}", api.headBucketHandler).Methods("HEAD")
|
2015-02-23 05:11:27 -05:00
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", api.headObjectHandler).Methods("HEAD")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT")
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// Domain based routing
|
2015-03-19 01:49:02 -04:00
|
|
|
func domainMux(api minioAPI, mux *router.Router) *router.Router {
|
2015-02-23 05:11:27 -05:00
|
|
|
mux.HandleFunc("/",
|
|
|
|
api.listObjectsHandler).Host("{bucket}" + "." + api.domain).Methods("GET")
|
|
|
|
mux.HandleFunc("/{object:.*}",
|
|
|
|
api.getObjectHandler).Host("{bucket}" + "." + api.domain).Methods("GET")
|
|
|
|
mux.HandleFunc("/{object:.*}",
|
|
|
|
api.headObjectHandler).Host("{bucket}" + "." + api.domain).Methods("HEAD")
|
|
|
|
mux.HandleFunc("/{object:.*}",
|
|
|
|
api.putObjectHandler).Host("{bucket}" + "." + api.domain).Methods("PUT")
|
|
|
|
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}", api.putBucketHandler).Methods("PUT")
|
2015-04-07 03:44:01 -04:00
|
|
|
mux.HandleFunc("/{bucket}", api.headBucketHandler).Methods("HEAD")
|
2015-02-23 05:11:27 -05:00
|
|
|
|
|
|
|
return mux
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// Get proper router based on domain availability
|
2015-03-19 01:49:02 -04:00
|
|
|
func getMux(api minioAPI, mux *router.Router) *router.Router {
|
2015-02-23 05:11:27 -05:00
|
|
|
switch true {
|
|
|
|
case api.domain == "":
|
|
|
|
return pathMux(api, mux)
|
|
|
|
case api.domain != "":
|
|
|
|
s := mux.Host(api.domain).Subrouter()
|
|
|
|
return domainMux(api, s)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
// HTTPHandler - http wrapper handler
|
2015-03-23 23:40:21 -04:00
|
|
|
func HTTPHandler(domain string, driver drivers.Driver) http.Handler {
|
2015-03-19 01:49:02 -04:00
|
|
|
var mux *router.Router
|
2015-03-06 00:07:19 -05:00
|
|
|
var api = minioAPI{}
|
2015-03-23 23:40:21 -04:00
|
|
|
api.driver = driver
|
2015-02-23 05:11:27 -05:00
|
|
|
api.domain = domain
|
|
|
|
|
2015-03-19 01:49:02 -04:00
|
|
|
r := router.NewRouter()
|
2015-02-23 05:11:27 -05:00
|
|
|
mux = getMux(api, r)
|
2015-02-15 20:03:27 -05:00
|
|
|
|
|
|
|
var conf = config.Config{}
|
|
|
|
if err := conf.SetupConfig(); err != nil {
|
2015-04-03 21:06:30 -04:00
|
|
|
log.Fatal(iodine.New(err, map[string]string{"domain": domain}))
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
h := timeValidityHandler(mux)
|
|
|
|
h = ignoreResourcesHandler(h)
|
|
|
|
h = validateRequestHandler(conf, h)
|
2015-04-28 00:56:43 -04:00
|
|
|
// h = quota.BandwidthCap(h, 25*1024*1024, time.Duration(30*time.Minute))
|
|
|
|
// h = quota.BandwidthCap(h, 100*1024*1024, time.Duration(24*time.Hour))
|
|
|
|
// h = quota.RequestLimit(h, 100, time.Duration(30*time.Minute))
|
|
|
|
// h = quota.RequestLimit(h, 1000, time.Duration(24*time.Hour))
|
2015-04-26 23:06:55 -04:00
|
|
|
h = quota.ConnectionLimit(h, 5)
|
2015-04-28 21:06:09 -04:00
|
|
|
h = LogHandler(h)
|
2015-04-26 17:20:24 -04:00
|
|
|
return h
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-04-28 21:06:09 -04:00
|
|
|
|
|
|
|
type logHandler struct {
|
|
|
|
http.Handler
|
2015-04-28 23:23:17 -04:00
|
|
|
Logger chan<- string
|
2015-04-28 21:06:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogMessage is a serializable json log message
|
|
|
|
type LogMessage struct {
|
|
|
|
Request *http.Request
|
|
|
|
StartTime time.Time
|
|
|
|
Duration time.Duration
|
|
|
|
Status int
|
|
|
|
ResponseHeaders http.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogWriter is used to capture status for log messages
|
|
|
|
type LogWriter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
LogMessage *LogMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader writes headers and stores status in LogMessage
|
|
|
|
func (w *LogWriter) WriteHeader(status int) {
|
|
|
|
w.LogMessage.Status = status
|
|
|
|
w.ResponseWriter.WriteHeader(status)
|
|
|
|
w.ResponseWriter.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
logMessage := &LogMessage{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
}
|
|
|
|
logWriter := &LogWriter{ResponseWriter: w, LogMessage: logMessage}
|
|
|
|
h.Handler.ServeHTTP(logWriter, req)
|
|
|
|
logMessage.ResponseHeaders = w.Header()
|
|
|
|
logMessage.Request = req
|
|
|
|
logMessage.Duration = time.Now().Sub(logMessage.StartTime)
|
|
|
|
js, _ := json.Marshal(logMessage)
|
2015-04-28 23:23:17 -04:00
|
|
|
h.Logger <- string(js)
|
2015-04-28 21:06:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogHandler logs requests
|
|
|
|
func LogHandler(h http.Handler) http.Handler {
|
2015-04-28 23:23:17 -04:00
|
|
|
logger, _ := FileLogger("access.log")
|
|
|
|
return &logHandler{Handler: h, Logger: logger}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileLogger returns a channel that is used to write to the logger
|
|
|
|
func FileLogger(filename string) (chan<- string, error) {
|
|
|
|
ch := make(chan string)
|
|
|
|
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, map[string]string{"logfile": filename})
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for message := range ch {
|
|
|
|
if _, err := io.Copy(file, bytes.NewBufferString(message+"\n")); err != nil {
|
|
|
|
log.Println(iodine.New(err, nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch, nil
|
2015-04-28 21:06:09 -04:00
|
|
|
}
|