mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
Allow multiple repositories
This commit is contained in:
parent
755cf10659
commit
f14b15ee31
2 changed files with 42 additions and 10 deletions
31
handlers.go
31
handlers.go
|
@ -9,8 +9,10 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"goji.io/middleware"
|
||||||
"goji.io/pat"
|
"goji.io/pat"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,6 +20,14 @@ func isHashed(dir string) bool {
|
||||||
return dir == "data"
|
return dir == "data"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRepo(r *http.Request) string {
|
||||||
|
if strings.HasPrefix(fmt.Sprintf("%s", middleware.Pattern(r.Context())), "/:repo/") {
|
||||||
|
return filepath.Join(*path, pat.Param(r, "repo"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return *path
|
||||||
|
}
|
||||||
|
|
||||||
func createDirectories(path string) {
|
func createDirectories(path string) {
|
||||||
log.Println("Creating repository directories")
|
log.Println("Creating repository directories")
|
||||||
|
|
||||||
|
@ -65,7 +75,7 @@ func CheckConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if *debug {
|
if *debug {
|
||||||
log.Println("CheckConfig()")
|
log.Println("CheckConfig()")
|
||||||
}
|
}
|
||||||
config := filepath.Join(*path, "config")
|
config := filepath.Join(getRepo(r), "config")
|
||||||
st, err := os.Stat(config)
|
st, err := os.Stat(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "404 not found", 404)
|
http.Error(w, "404 not found", 404)
|
||||||
|
@ -80,7 +90,7 @@ func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if *debug {
|
if *debug {
|
||||||
log.Println("GetConfig()")
|
log.Println("GetConfig()")
|
||||||
}
|
}
|
||||||
config := filepath.Join(*path, "config")
|
config := filepath.Join(getRepo(r), "config")
|
||||||
bytes, err := ioutil.ReadFile(config)
|
bytes, err := ioutil.ReadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "404 not found", 404)
|
http.Error(w, "404 not found", 404)
|
||||||
|
@ -95,7 +105,7 @@ func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if *debug {
|
if *debug {
|
||||||
log.Println("SaveConfig()")
|
log.Println("SaveConfig()")
|
||||||
}
|
}
|
||||||
config := filepath.Join(*path, "config")
|
config := filepath.Join(getRepo(r), "config")
|
||||||
bytes, err := ioutil.ReadAll(r.Body)
|
bytes, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "400 bad request", 400)
|
http.Error(w, "400 bad request", 400)
|
||||||
|
@ -115,7 +125,7 @@ func ListBlobs(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("ListBlobs()")
|
log.Println("ListBlobs()")
|
||||||
}
|
}
|
||||||
dir := pat.Param(r, "type")
|
dir := pat.Param(r, "type")
|
||||||
path := filepath.Join(*path, dir)
|
path := filepath.Join(getRepo(r), dir)
|
||||||
|
|
||||||
items, err := ioutil.ReadDir(path)
|
items, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -160,7 +170,7 @@ func CheckBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if isHashed(dir) {
|
if isHashed(dir) {
|
||||||
name = filepath.Join(name[:2], name)
|
name = filepath.Join(name[:2], name)
|
||||||
}
|
}
|
||||||
path := filepath.Join(*path, dir, name)
|
path := filepath.Join(getRepo(r), dir, name)
|
||||||
|
|
||||||
st, err := os.Stat(path)
|
st, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -182,7 +192,7 @@ func GetBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if isHashed(dir) {
|
if isHashed(dir) {
|
||||||
name = filepath.Join(name[:2], name)
|
name = filepath.Join(name[:2], name)
|
||||||
}
|
}
|
||||||
path := filepath.Join(*path, dir, name)
|
path := filepath.Join(getRepo(r), dir, name)
|
||||||
|
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -199,16 +209,17 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if *debug {
|
if *debug {
|
||||||
log.Println("SaveBlob()")
|
log.Println("SaveBlob()")
|
||||||
}
|
}
|
||||||
|
repo := getRepo(r)
|
||||||
dir := pat.Param(r, "type")
|
dir := pat.Param(r, "type")
|
||||||
name := pat.Param(r, "name")
|
name := pat.Param(r, "name")
|
||||||
|
|
||||||
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) {
|
||||||
createDirectories(*path)
|
createDirectories(repo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := filepath.Join(*path, "tmp", name)
|
tmp := filepath.Join(repo, "tmp", name)
|
||||||
|
|
||||||
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)
|
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -237,7 +248,7 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if isHashed(dir) {
|
if isHashed(dir) {
|
||||||
name = filepath.Join(name[:2], name)
|
name = filepath.Join(name[:2], name)
|
||||||
}
|
}
|
||||||
path := filepath.Join(*path, dir, name)
|
path := filepath.Join(repo, dir, name)
|
||||||
|
|
||||||
if err := os.Rename(tmp, path); err != nil {
|
if err := os.Rename(tmp, path); err != nil {
|
||||||
os.Remove(tmp)
|
os.Remove(tmp)
|
||||||
|
@ -260,7 +271,7 @@ func DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if isHashed(dir) {
|
if isHashed(dir) {
|
||||||
name = filepath.Join(name[:2], name)
|
name = filepath.Join(name[:2], name)
|
||||||
}
|
}
|
||||||
path := filepath.Join(*path, dir, name)
|
path := filepath.Join(getRepo(r), dir, name)
|
||||||
|
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
http.Error(w, "500 internal server error", 500)
|
http.Error(w, "500 internal server error", 500)
|
||||||
|
|
21
main.go
21
main.go
|
@ -20,16 +20,37 @@ var (
|
||||||
debug = flag.Bool("debug", false, "output debug messages")
|
debug = flag.Bool("debug", false, "output debug messages")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func debugHandler(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("%s %s", r.Method, r.URL)
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func setupMux() *goji.Mux {
|
func setupMux() *goji.Mux {
|
||||||
mux := goji.NewMux()
|
mux := goji.NewMux()
|
||||||
|
|
||||||
|
if *debug {
|
||||||
|
mux.Use(debugHandler)
|
||||||
|
}
|
||||||
|
|
||||||
mux.HandleFunc(pat.Head("/config"), CheckConfig)
|
mux.HandleFunc(pat.Head("/config"), CheckConfig)
|
||||||
|
mux.HandleFunc(pat.Head("/:repo/config"), CheckConfig)
|
||||||
mux.HandleFunc(pat.Get("/config"), GetConfig)
|
mux.HandleFunc(pat.Get("/config"), GetConfig)
|
||||||
|
mux.HandleFunc(pat.Get("/:repo/config"), GetConfig)
|
||||||
mux.HandleFunc(pat.Post("/config"), SaveConfig)
|
mux.HandleFunc(pat.Post("/config"), SaveConfig)
|
||||||
|
mux.HandleFunc(pat.Post("/:repo/config"), SaveConfig)
|
||||||
mux.HandleFunc(pat.Get("/:type/"), ListBlobs)
|
mux.HandleFunc(pat.Get("/:type/"), ListBlobs)
|
||||||
|
mux.HandleFunc(pat.Get("/:repo/:type/"), ListBlobs)
|
||||||
mux.HandleFunc(pat.Head("/:type/:name"), CheckBlob)
|
mux.HandleFunc(pat.Head("/:type/:name"), CheckBlob)
|
||||||
|
mux.HandleFunc(pat.Head("/:repo/:type/:name"), CheckBlob)
|
||||||
mux.HandleFunc(pat.Get("/:type/:name"), GetBlob)
|
mux.HandleFunc(pat.Get("/:type/:name"), GetBlob)
|
||||||
|
mux.HandleFunc(pat.Get("/:repo/:type/:name"), GetBlob)
|
||||||
mux.HandleFunc(pat.Post("/:type/:name"), SaveBlob)
|
mux.HandleFunc(pat.Post("/:type/:name"), SaveBlob)
|
||||||
|
mux.HandleFunc(pat.Post("/:repo/:type/:name"), SaveBlob)
|
||||||
mux.HandleFunc(pat.Delete("/:type/:name"), DeleteBlob)
|
mux.HandleFunc(pat.Delete("/:type/:name"), DeleteBlob)
|
||||||
|
mux.HandleFunc(pat.Delete("/:repo/:type/:name"), DeleteBlob)
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue