Use goji.io pat package to extract params

This commit is contained in:
Zlatko Čalušić 2016-12-27 18:57:49 +01:00
parent 01ff183e12
commit 755cf10659

View file

@ -9,8 +9,9 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"goji.io/pat"
) )
func isHashed(dir string) bool { func isHashed(dir string) bool {
@ -113,8 +114,7 @@ func ListBlobs(w http.ResponseWriter, r *http.Request) {
if *debug { if *debug {
log.Println("ListBlobs()") log.Println("ListBlobs()")
} }
vars := strings.Split(r.RequestURI, "/") dir := pat.Param(r, "type")
dir := vars[1]
path := filepath.Join(*path, dir) path := filepath.Join(*path, dir)
items, err := ioutil.ReadDir(path) items, err := ioutil.ReadDir(path)
@ -154,9 +154,8 @@ func CheckBlob(w http.ResponseWriter, r *http.Request) {
if *debug { if *debug {
log.Println("CheckBlob()") log.Println("CheckBlob()")
} }
vars := strings.Split(r.RequestURI, "/") dir := pat.Param(r, "type")
dir := vars[1] name := pat.Param(r, "name")
name := vars[2]
if isHashed(dir) { if isHashed(dir) {
name = filepath.Join(name[:2], name) name = filepath.Join(name[:2], name)
@ -177,9 +176,8 @@ func GetBlob(w http.ResponseWriter, r *http.Request) {
if *debug { if *debug {
log.Println("GetBlob()") log.Println("GetBlob()")
} }
vars := strings.Split(r.RequestURI, "/") dir := pat.Param(r, "type")
dir := vars[1] name := pat.Param(r, "name")
name := vars[2]
if isHashed(dir) { if isHashed(dir) {
name = filepath.Join(name[:2], name) name = filepath.Join(name[:2], name)
@ -201,9 +199,8 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
if *debug { if *debug {
log.Println("SaveBlob()") log.Println("SaveBlob()")
} }
vars := strings.Split(r.RequestURI, "/") dir := pat.Param(r, "type")
dir := vars[1] name := pat.Param(r, "name")
name := vars[2]
if dir == "keys" { if dir == "keys" {
if _, err := os.Stat("keys"); err != nil && os.IsNotExist(err) { if _, err := os.Stat("keys"); err != nil && os.IsNotExist(err) {
@ -257,9 +254,8 @@ func DeleteBlob(w http.ResponseWriter, r *http.Request) {
if *debug { if *debug {
log.Println("DeleteBlob()") log.Println("DeleteBlob()")
} }
vars := strings.Split(r.RequestURI, "/") dir := pat.Param(r, "type")
dir := vars[1] name := pat.Param(r, "name")
name := vars[2]
if isHashed(dir) { if isHashed(dir) {
name = filepath.Join(name[:2], name) name = filepath.Join(name[:2], name)