seaweedfs/weed/server/common.go

269 lines
7.3 KiB
Go
Raw Normal View History

package weed_server
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"path/filepath"
"strconv"
"strings"
2015-03-22 19:50:04 +00:00
"time"
2020-02-11 04:23:04 +00:00
"github.com/valyala/fasthttp"
2019-12-06 14:57:32 +00:00
"google.golang.org/grpc"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/stats"
"github.com/chrislusf/seaweedfs/weed/storage/needle"
"github.com/chrislusf/seaweedfs/weed/util"
2018-10-07 17:54:05 +00:00
"github.com/gorilla/mux"
2018-10-14 07:30:20 +00:00
statik "github.com/rakyll/statik/fs"
2019-12-06 14:57:32 +00:00
_ "github.com/chrislusf/seaweedfs/weed/statik"
)
var serverStats *stats.ServerStats
2015-03-22 19:50:04 +00:00
var startTime = time.Now()
2018-10-07 17:54:05 +00:00
var statikFS http.FileSystem
func init() {
serverStats = stats.NewServerStats()
go serverStats.Start()
2018-10-07 17:54:05 +00:00
statikFS, _ = statik.New()
}
2020-02-11 04:23:04 +00:00
func writeJson(ctx *fasthttp.RequestCtx, httpStatus int, obj interface{}) (err error) {
var bytes []byte
if ctx.FormValue("pretty") != nil {
bytes, err = json.MarshalIndent(obj, "", " ")
} else {
bytes, err = json.Marshal(obj)
}
if err != nil {
return
}
callback := ctx.FormValue("callback")
if callback == nil {
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.SetStatusCode(httpStatus)
if httpStatus == http.StatusNotModified {
return
}
_, err = ctx.Response.BodyWriter().Write(bytes)
} else {
ctx.Response.Header.Set("Content-Type", "application/javascript")
ctx.SetStatusCode(httpStatus)
if httpStatus == http.StatusNotModified {
return
}
if _, err = ctx.Response.BodyWriter().Write([]uint8(callback)); err != nil {
return
}
if _, err = ctx.Response.BodyWriter().Write([]uint8("(")); err != nil {
return
}
fmt.Fprint(ctx.Response.BodyWriter(), string(bytes))
if _, err = ctx.Response.BodyWriter().Write([]uint8(")")); err != nil {
return
}
}
return
}
func oldWriteJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
var bytes []byte
if r.FormValue("pretty") != "" {
bytes, err = json.MarshalIndent(obj, "", " ")
} else {
bytes, err = json.Marshal(obj)
}
if err != nil {
return
}
callback := r.FormValue("callback")
if callback == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)
if httpStatus == http.StatusNotModified {
return
}
_, err = w.Write(bytes)
} else {
w.Header().Set("Content-Type", "application/javascript")
w.WriteHeader(httpStatus)
if httpStatus == http.StatusNotModified {
return
}
if _, err = w.Write([]uint8(callback)); err != nil {
return
}
if _, err = w.Write([]uint8("(")); err != nil {
return
}
fmt.Fprint(w, string(bytes))
if _, err = w.Write([]uint8(")")); err != nil {
return
}
}
return
}
2020-02-11 04:23:04 +00:00
// wrapper for oldWriteJson - just logs errors
func oldWriteJsonQuiet(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) {
if err := oldWriteJson(w, r, httpStatus, obj); err != nil {
glog.V(0).Infof("error writing JSON status %d: %v", httpStatus, err)
glog.V(1).Infof("JSON content: %+v", obj)
}
}
// wrapper for oldWriteJson - just logs errors
func writeJsonQuiet(ctx *fasthttp.RequestCtx, httpStatus int, obj interface{}) {
if err := writeJson(ctx, httpStatus, obj); err != nil {
2020-01-22 06:45:50 +00:00
glog.V(0).Infof("error writing JSON status %d: %v", httpStatus, err)
glog.V(1).Infof("JSON content: %+v", obj)
}
}
2020-02-11 04:23:04 +00:00
func oldWriteJsonError(w http.ResponseWriter, r *http.Request, httpStatus int, err error) {
m := make(map[string]interface{})
m["error"] = err.Error()
oldWriteJsonQuiet(w, r, httpStatus, m)
}
func writeJsonError(ctx *fasthttp.RequestCtx, httpStatus int, err error) {
m := make(map[string]interface{})
m["error"] = err.Error()
2020-02-11 04:23:04 +00:00
writeJsonQuiet(ctx, httpStatus, m)
}
func debug(params ...interface{}) {
2018-06-12 06:51:36 +00:00
glog.V(4).Infoln(params...)
}
2019-02-18 20:11:52 +00:00
func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string, grpcDialOption grpc.DialOption) {
m := make(map[string]interface{})
if r.Method != "POST" {
2020-02-11 04:23:04 +00:00
oldWriteJsonError(w, r, http.StatusMethodNotAllowed, errors.New("Only submit via POST!"))
return
}
debug("parsing upload file...")
2020-02-11 04:23:04 +00:00
fname, data, mimeType, pairMap, isGzipped, originalDataSize, lastModified, _, _, pe := needle.OldParseUpload(r, 256*1024*1024)
if pe != nil {
2020-02-11 04:23:04 +00:00
oldWriteJsonError(w, r, http.StatusBadRequest, pe)
return
}
debug("assigning file id for", fname)
r.ParseForm()
count := uint64(1)
if r.FormValue("count") != "" {
count, pe = strconv.ParseUint(r.FormValue("count"), 10, 32)
if pe != nil {
2020-02-11 04:23:04 +00:00
oldWriteJsonError(w, r, http.StatusBadRequest, pe)
return
}
}
2016-06-26 02:50:18 +00:00
ar := &operation.VolumeAssignRequest{
Count: count,
DataCenter: r.FormValue("dataCenter"),
2016-06-26 02:50:18 +00:00
Replication: r.FormValue("replication"),
Collection: r.FormValue("collection"),
Ttl: r.FormValue("ttl"),
}
2019-02-18 20:11:52 +00:00
assignResult, ae := operation.Assign(masterUrl, grpcDialOption, ar)
if ae != nil {
2020-02-11 04:23:04 +00:00
oldWriteJsonError(w, r, http.StatusInternalServerError, ae)
return
}
url := "http://" + assignResult.Url + "/" + assignResult.Fid
if lastModified != 0 {
url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
}
debug("upload file to store", url)
2019-02-15 08:09:19 +00:00
uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType, pairMap, assignResult.Auth)
if err != nil {
2020-02-11 04:23:04 +00:00
oldWriteJsonError(w, r, http.StatusInternalServerError, err)
return
}
m["fileName"] = fname
m["fid"] = assignResult.Fid
m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
m["size"] = originalDataSize
2018-09-23 05:12:21 +00:00
m["eTag"] = uploadResult.ETag
2020-02-11 04:23:04 +00:00
oldWriteJsonQuiet(w, r, http.StatusCreated, m)
return
}
func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
switch strings.Count(path, "/") {
case 3:
parts := strings.Split(path, "/")
vid, fid, filename = parts[1], parts[2], parts[3]
ext = filepath.Ext(filename)
case 2:
parts := strings.Split(path, "/")
vid, fid = parts[1], parts[2]
dotIndex := strings.LastIndex(fid, ".")
if dotIndex > 0 {
ext = fid[dotIndex:]
fid = fid[0:dotIndex]
}
default:
sepIndex := strings.LastIndex(path, "/")
commaIndex := strings.LastIndex(path[sepIndex:], ",")
if commaIndex <= 0 {
vid, isVolumeIdOnly = path[sepIndex+1:], true
return
}
dotIndex := strings.LastIndex(path[sepIndex:], ".")
vid = path[sepIndex+1 : commaIndex]
fid = path[commaIndex+1:]
ext = ""
if dotIndex > 0 {
fid = path[commaIndex+1 : dotIndex]
ext = path[dotIndex:]
}
}
return
}
2018-05-07 07:37:33 +00:00
func statsHealthHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{})
m["Version"] = util.VERSION
2020-02-11 04:23:04 +00:00
oldWriteJsonQuiet(w, r, http.StatusOK, m)
2018-05-07 07:37:33 +00:00
}
func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{})
m["Version"] = util.VERSION
2014-03-26 20:22:27 +00:00
m["Counters"] = serverStats
2020-02-11 04:23:04 +00:00
oldWriteJsonQuiet(w, r, http.StatusOK, m)
}
2014-03-26 20:22:27 +00:00
func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
m := make(map[string]interface{})
m["Version"] = util.VERSION
2014-03-26 20:22:27 +00:00
m["Memory"] = stats.MemStat()
2020-02-11 04:23:04 +00:00
oldWriteJsonQuiet(w, r, http.StatusOK, m)
}
2018-05-28 09:35:58 +00:00
2018-10-07 17:54:05 +00:00
func handleStaticResources(defaultMux *http.ServeMux) {
defaultMux.Handle("/favicon.ico", http.FileServer(statikFS))
defaultMux.Handle("/seaweedfsstatic/", http.StripPrefix("/seaweedfsstatic", http.FileServer(statikFS)))
2018-05-28 09:35:58 +00:00
}
func handleStaticResources2(r *mux.Router) {
r.Handle("/favicon.ico", http.FileServer(statikFS))
r.PathPrefix("/seaweedfsstatic/").Handler(http.StripPrefix("/seaweedfsstatic", http.FileServer(statikFS)))
}