mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
Update with changes from github.com/restic/restic tree
Uncompilable right now, due to bad import paths.
This commit is contained in:
parent
07fae00e7d
commit
80196e6df6
5 changed files with 122 additions and 31 deletions
51
handlers.go
51
handlers.go
|
@ -1,19 +1,29 @@
|
|||
// +build go1.4
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"restic/fs"
|
||||
)
|
||||
|
||||
// Context contains repository meta-data.
|
||||
type Context struct {
|
||||
path string
|
||||
}
|
||||
|
||||
// AuthHandler wraps h with a http.HandlerFunc that performs basic
|
||||
// authentication against the user/passwords pairs stored in f and returns the
|
||||
// http.HandlerFunc.
|
||||
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
|
@ -29,16 +39,22 @@ func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// CheckConfig returns a http.HandlerFunc that checks whether
|
||||
// a configuration exists.
|
||||
func CheckConfig(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
config := filepath.Join(c.path, "config")
|
||||
if _, err := os.Stat(config); err != nil {
|
||||
st, err := os.Stat(config)
|
||||
if err != nil {
|
||||
http.Error(w, "404 not found", 404)
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
|
||||
}
|
||||
}
|
||||
|
||||
// GetConfig returns a http.HandlerFunc that allows for a
|
||||
// config to be retrieved.
|
||||
func GetConfig(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
config := filepath.Join(c.path, "config")
|
||||
|
@ -51,6 +67,8 @@ func GetConfig(c *Context) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// SaveConfig returns a http.HandlerFunc that allows for a
|
||||
// config to be saved.
|
||||
func SaveConfig(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
config := filepath.Join(c.path, "config")
|
||||
|
@ -68,6 +86,8 @@ func SaveConfig(c *Context) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// ListBlobs returns a http.HandlerFunc that lists
|
||||
// all blobs of a given type in an arbitrary order.
|
||||
func ListBlobs(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := strings.Split(r.RequestURI, "/")
|
||||
|
@ -91,27 +111,32 @@ func ListBlobs(c *Context) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// CheckBlob reutrns a http.HandlerFunc that tests whether a blob exists
|
||||
// and returns 200, if it does, or 404 otherwise.
|
||||
func CheckBlob(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := strings.Split(r.RequestURI, "/")
|
||||
dir := vars[1]
|
||||
name := vars[2]
|
||||
path := filepath.Join(c.path, dir, name)
|
||||
_, err := os.Stat(path)
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
http.Error(w, "404 not found", 404)
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
|
||||
}
|
||||
}
|
||||
|
||||
// GetBlob returns a http.HandlerFunc that retrieves a blob
|
||||
// from the repository.
|
||||
func GetBlob(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := strings.Split(r.RequestURI, "/")
|
||||
dir := vars[1]
|
||||
name := vars[2]
|
||||
path := filepath.Join(c.path, dir, name)
|
||||
file, err := os.Open(path)
|
||||
file, err := fs.Open(path)
|
||||
if err != nil {
|
||||
http.Error(w, "404 not found", 404)
|
||||
return
|
||||
|
@ -121,19 +146,29 @@ func GetBlob(c *Context) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// SaveBlob returns a http.HandlerFunc that saves a blob to the repository.
|
||||
func SaveBlob(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := strings.Split(r.RequestURI, "/")
|
||||
dir := vars[1]
|
||||
name := vars[2]
|
||||
path := filepath.Join(c.path, dir, name)
|
||||
bytes, err := ioutil.ReadAll(r.Body)
|
||||
tmp := path + "_tmp"
|
||||
tf, err := fs.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
http.Error(w, "400 bad request", 400)
|
||||
http.Error(w, "500 internal server error", 500)
|
||||
return
|
||||
}
|
||||
errw := ioutil.WriteFile(path, bytes, 0600)
|
||||
if errw != nil {
|
||||
if _, err := io.Copy(tf, r.Body); err != nil {
|
||||
http.Error(w, "400 bad request", 400)
|
||||
tf.Close()
|
||||
os.Remove(tmp)
|
||||
return
|
||||
}
|
||||
if err := tf.Close(); err != nil {
|
||||
http.Error(w, "500 internal server error", 500)
|
||||
}
|
||||
if err := os.Rename(tmp, path); err != nil {
|
||||
http.Error(w, "500 internal server error", 500)
|
||||
return
|
||||
}
|
||||
|
@ -141,6 +176,8 @@ func SaveBlob(c *Context) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// DeleteBlob returns a http.HandlerFunc that deletes a blob from the
|
||||
// repository.
|
||||
func DeleteBlob(c *Context) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := strings.Split(r.RequestURI, "/")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue