-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# distribute-object-storage | ||
Simple distributed object storage implemented by go | ||
|
||
### 1 单机版对象存储 | ||
mkdir /tmp/objects | ||
|
||
`LISTEN_ADDRESS=:12345 STORAGE_ROOT=/tmp go run server.go` | ||
|
||
存对象`curl -v 127.0.0.1:12345/objects/test -XPUT -d "this is a test object"` | ||
取对象`curl -v 127.0.0.1:12345/objects/test` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package objects | ||
|
||
import "net/http" | ||
|
||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
m := r.Method | ||
|
||
if m == http.MethodPut { | ||
put(w, r) | ||
} else if m == http.MethodGet { | ||
get(w, r) | ||
} else { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package objects | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
STORAGE_ROOT = "STORAGE_ROOT" | ||
) | ||
|
||
func put(w http.ResponseWriter, r *http.Request) { | ||
f, err := os.Create(os.Getenv(STORAGE_ROOT) + "/objects/" + | ||
strings.Split(r.URL.EscapedPath(), "/")[2]) | ||
if err != nil { | ||
log.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
defer f.Close() | ||
io.Copy(f, r.Body) | ||
} | ||
|
||
func get(w http.ResponseWriter, r *http.Request) { | ||
f, err := os.Open(os.Getenv(STORAGE_ROOT) + "/objects/" + | ||
strings.Split(r.URL.EscapedPath(), "/")[2]) | ||
if err != nil { | ||
log.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
defer f.Close() | ||
io.Copy(w, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
LISTEN_ADDRESS=:12345 | ||
STORAGE_ROOT=/tmp/storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/pojiang20/distribute-object-storage/objects" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
const ( | ||
listenAddress = "LISTEN_ADDRESS" | ||
ObjectsPattern = "/objects/" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc(ObjectsPattern, objects.Handler) | ||
log.Print(http.ListenAndServe(os.Getenv(listenAddress), nil)) | ||
} |