works now!
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@20 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"http"
|
||||
"json"
|
||||
"log"
|
||||
"mime"
|
||||
"strconv"
|
||||
@@ -18,10 +19,12 @@ var (
|
||||
publicServer = flag.String("pserver", "localhost:8080", "public server to serve data read")
|
||||
metaServer = flag.String("mserver", "localhost:9333", "metadata server to store mappings")
|
||||
|
||||
store *storage.Store
|
||||
store *storage.Store
|
||||
)
|
||||
|
||||
|
||||
func statusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
writeJson(w, r, store.Status())
|
||||
}
|
||||
func storeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
@@ -35,25 +38,52 @@ func storeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
n := new(storage.Needle)
|
||||
path := r.URL.Path
|
||||
sepIndex := strings.Index(path[1:], "/") + 1
|
||||
volumeId, _ := strconv.Atoui64(path[1:sepIndex])
|
||||
dotIndex := strings.LastIndex(path, ".")
|
||||
n.ParsePath(path[sepIndex+1 : dotIndex])
|
||||
ext := path[dotIndex:]
|
||||
sepIndex := strings.LastIndex(path, "/")
|
||||
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
||||
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
||||
fid := path[commaIndex+1:]
|
||||
if dotIndex > 0 {
|
||||
fid = path[commaIndex+1 : dotIndex]
|
||||
}
|
||||
volumeId, _ := strconv.Atoui64(path[sepIndex+1 : commaIndex])
|
||||
n.ParsePath(fid)
|
||||
|
||||
store.Read(volumeId, n)
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
||||
if dotIndex > 0 {
|
||||
ext := path[dotIndex:]
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
||||
}
|
||||
w.Write(n.Data)
|
||||
}
|
||||
func PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
volumeId, _ := strconv.Atoui64(r.FormValue("volumeId"))
|
||||
store.Write(volumeId, storage.NewNeedle(r))
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprint(w, "volumeId=", volumeId, "\n")
|
||||
path := r.URL.Path
|
||||
commaIndex := strings.LastIndex(path, ",")
|
||||
sepIndex := strings.LastIndex(path[:commaIndex], "/")
|
||||
volumeId, e := strconv.Atoui64(path[sepIndex+1 : commaIndex])
|
||||
if e != nil {
|
||||
writeJson(w, r, e)
|
||||
} else {
|
||||
store.Write(volumeId, storage.NewNeedle(r))
|
||||
writeJson(w, r, make(map[string]string))
|
||||
}
|
||||
}
|
||||
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
bytes, _ := json.Marshal(obj)
|
||||
callback := r.FormValue("callback")
|
||||
if callback == "" {
|
||||
w.Write(bytes)
|
||||
} else {
|
||||
w.Write([]uint8(callback))
|
||||
w.Write([]uint8("("))
|
||||
fmt.Fprint(w, string(bytes))
|
||||
w.Write([]uint8(")"))
|
||||
}
|
||||
//log.Println("JSON Response", string(bytes))
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
@@ -61,14 +91,15 @@ func main() {
|
||||
store = storage.NewStore(*port, *publicServer, *chunkFolder, 1024*1024*1024, *chunkCount)
|
||||
defer store.Close()
|
||||
http.HandleFunc("/", storeHandler)
|
||||
http.HandleFunc("/status", statusHandler)
|
||||
|
||||
store.Join(*metaServer)
|
||||
log.Println("store joined at", *metaServer)
|
||||
|
||||
log.Println("Start storage service at http://127.0.0.1:" + strconv.Itoa(*port))
|
||||
e := http.ListenAndServe(":"+strconv.Itoa(*port), nil)
|
||||
if e!=nil {
|
||||
log.Fatalf("Fail to start:",e.String())
|
||||
if e != nil {
|
||||
log.Fatalf("Fail to start:", e.String())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,15 @@ var (
|
||||
metaFolder = flag.String("mdir", "/tmp", "data directory to store mappings")
|
||||
capacity = flag.Int("capacity", 100, "maximum number of volumes to hold")
|
||||
mapper *directory.Mapper
|
||||
|
||||
)
|
||||
|
||||
func dirReadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
volumeId, _ := strconv.Atoui64(r.FormValue("volumeId"))
|
||||
func dirLookupHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vid := r.FormValue("volumeId")
|
||||
commaSep := strings.Index(vid, ",")
|
||||
if commaSep > 0 {
|
||||
vid = vid[0:commaSep]
|
||||
}
|
||||
volumeId, _ := strconv.Atoui64(vid)
|
||||
machine := mapper.Get(uint32(volumeId))
|
||||
writeJson(w, r, machine.Server)
|
||||
}
|
||||
@@ -29,9 +33,9 @@ func dirWriteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_, machine := mapper.PickForWrite()
|
||||
writeJson(w, r, machine)
|
||||
}
|
||||
func dirPickHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fid, machine := mapper.PickForWrite()
|
||||
writeJson(w, r, map[string]string{"fid":fid,"url":machine.Url})
|
||||
func dirAssignHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fid, machine := mapper.PickForWrite()
|
||||
writeJson(w, r, map[string]string{"fid": fid, "url": machine.Url})
|
||||
}
|
||||
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
||||
s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
|
||||
@@ -44,9 +48,7 @@ func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
||||
writeJson(w, r, vids)
|
||||
}
|
||||
func dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
bytes, _ := json.Marshal(mapper)
|
||||
fmt.Fprint(w, string(bytes))
|
||||
writeJson(w, r, mapper)
|
||||
}
|
||||
func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
@@ -67,9 +69,9 @@ func main() {
|
||||
flag.Parse()
|
||||
mapper = directory.NewMapper(*metaFolder, "directory", *capacity)
|
||||
defer mapper.Save()
|
||||
http.HandleFunc("/dir/read", dirReadHandler)
|
||||
http.HandleFunc("/dir/assign", dirAssignHandler)
|
||||
http.HandleFunc("/dir/lookup", dirLookupHandler)
|
||||
http.HandleFunc("/dir/write", dirWriteHandler)
|
||||
http.HandleFunc("/dir/pick", dirPickHandler)
|
||||
http.HandleFunc("/dir/join", dirJoinHandler)
|
||||
http.HandleFunc("/dir/status", dirStatusHandler)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user