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.

200 lines
5.3 KiB

10 years ago
10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. "github.com/chrislusf/seaweedfs/weed/security"
  15. "github.com/chrislusf/seaweedfs/weed/stats"
  16. "github.com/chrislusf/seaweedfs/weed/storage"
  17. "github.com/chrislusf/seaweedfs/weed/util"
  18. _ "github.com/chrislusf/seaweedfs/weed/statik"
  19. statik "github.com/rakyll/statik/fs"
  20. )
  21. var serverStats *stats.ServerStats
  22. var startTime = time.Now()
  23. var statikFS http.FileSystem
  24. func init() {
  25. serverStats = stats.NewServerStats()
  26. go serverStats.Start()
  27. statikFS, _ = statik.New()
  28. }
  29. func writeJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
  30. var bytes []byte
  31. if r.FormValue("pretty") != "" {
  32. bytes, err = json.MarshalIndent(obj, "", " ")
  33. } else {
  34. bytes, err = json.Marshal(obj)
  35. }
  36. if err != nil {
  37. return
  38. }
  39. callback := r.FormValue("callback")
  40. if callback == "" {
  41. w.Header().Set("Content-Type", "application/json")
  42. w.WriteHeader(httpStatus)
  43. _, err = w.Write(bytes)
  44. } else {
  45. w.Header().Set("Content-Type", "application/javascript")
  46. w.WriteHeader(httpStatus)
  47. if _, err = w.Write([]uint8(callback)); err != nil {
  48. return
  49. }
  50. if _, err = w.Write([]uint8("(")); err != nil {
  51. return
  52. }
  53. fmt.Fprint(w, string(bytes))
  54. if _, err = w.Write([]uint8(")")); err != nil {
  55. return
  56. }
  57. }
  58. return
  59. }
  60. // wrapper for writeJson - just logs errors
  61. func writeJsonQuiet(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) {
  62. if err := writeJson(w, r, httpStatus, obj); err != nil {
  63. glog.V(0).Infof("error writing JSON %s: %v", obj, err)
  64. }
  65. }
  66. func writeJsonError(w http.ResponseWriter, r *http.Request, httpStatus int, err error) {
  67. m := make(map[string]interface{})
  68. m["error"] = err.Error()
  69. writeJsonQuiet(w, r, httpStatus, m)
  70. }
  71. func debug(params ...interface{}) {
  72. glog.V(4).Infoln(params...)
  73. }
  74. func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
  75. jwt := security.GetJwt(r)
  76. m := make(map[string]interface{})
  77. if r.Method != "POST" {
  78. writeJsonError(w, r, http.StatusMethodNotAllowed, errors.New("Only submit via POST!"))
  79. return
  80. }
  81. debug("parsing upload file...")
  82. fname, data, mimeType, pairMap, isGzipped, lastModified, _, _, pe := storage.ParseUpload(r)
  83. if pe != nil {
  84. writeJsonError(w, r, http.StatusBadRequest, pe)
  85. return
  86. }
  87. debug("assigning file id for", fname)
  88. r.ParseForm()
  89. ar := &operation.VolumeAssignRequest{
  90. Count: 1,
  91. Replication: r.FormValue("replication"),
  92. Collection: r.FormValue("collection"),
  93. Ttl: r.FormValue("ttl"),
  94. }
  95. assignResult, ae := operation.Assign(masterUrl, ar)
  96. if ae != nil {
  97. writeJsonError(w, r, http.StatusInternalServerError, ae)
  98. return
  99. }
  100. url := "http://" + assignResult.Url + "/" + assignResult.Fid
  101. if lastModified != 0 {
  102. url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
  103. }
  104. debug("upload file to store", url)
  105. uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType, pairMap, jwt)
  106. if err != nil {
  107. writeJsonError(w, r, http.StatusInternalServerError, err)
  108. return
  109. }
  110. m["fileName"] = fname
  111. m["fid"] = assignResult.Fid
  112. m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
  113. m["size"] = uploadResult.Size
  114. m["eTag"] = uploadResult.ETag
  115. writeJsonQuiet(w, r, http.StatusCreated, m)
  116. return
  117. }
  118. func deleteForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
  119. r.ParseForm()
  120. fids := r.Form["fid"]
  121. ret, err := operation.DeleteFiles(masterUrl, fids)
  122. if err != nil {
  123. writeJsonError(w, r, http.StatusInternalServerError, err)
  124. return
  125. }
  126. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  127. }
  128. func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
  129. switch strings.Count(path, "/") {
  130. case 3:
  131. parts := strings.Split(path, "/")
  132. vid, fid, filename = parts[1], parts[2], parts[3]
  133. ext = filepath.Ext(filename)
  134. case 2:
  135. parts := strings.Split(path, "/")
  136. vid, fid = parts[1], parts[2]
  137. dotIndex := strings.LastIndex(fid, ".")
  138. if dotIndex > 0 {
  139. ext = fid[dotIndex:]
  140. fid = fid[0:dotIndex]
  141. }
  142. default:
  143. sepIndex := strings.LastIndex(path, "/")
  144. commaIndex := strings.LastIndex(path[sepIndex:], ",")
  145. if commaIndex <= 0 {
  146. vid, isVolumeIdOnly = path[sepIndex+1:], true
  147. return
  148. }
  149. dotIndex := strings.LastIndex(path[sepIndex:], ".")
  150. vid = path[sepIndex+1 : commaIndex]
  151. fid = path[commaIndex+1:]
  152. ext = ""
  153. if dotIndex > 0 {
  154. fid = path[commaIndex+1 : dotIndex]
  155. ext = path[dotIndex:]
  156. }
  157. }
  158. return
  159. }
  160. func statsHealthHandler(w http.ResponseWriter, r *http.Request) {
  161. m := make(map[string]interface{})
  162. m["Version"] = util.VERSION
  163. writeJsonQuiet(w, r, http.StatusOK, m)
  164. }
  165. func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
  166. m := make(map[string]interface{})
  167. m["Version"] = util.VERSION
  168. m["Counters"] = serverStats
  169. writeJsonQuiet(w, r, http.StatusOK, m)
  170. }
  171. func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
  172. m := make(map[string]interface{})
  173. m["Version"] = util.VERSION
  174. m["Memory"] = stats.MemStat()
  175. writeJsonQuiet(w, r, http.StatusOK, m)
  176. }
  177. func handleStaticResources(defaultMux *http.ServeMux) {
  178. defaultMux.Handle("/favicon.ico", http.FileServer(statikFS))
  179. defaultMux.Handle("/seaweedfsstatic/", http.StripPrefix("/seaweedfsstatic", http.FileServer(statikFS)))
  180. }