Skip to content

Instantly share code, notes, and snippets.

@bchapuis
Created August 4, 2015 13:12
Show Gist options
  • Save bchapuis/16d9dd0a8ae1ba80bcdd to your computer and use it in GitHub Desktop.
Save bchapuis/16d9dd0a8ae1ba80bcdd to your computer and use it in GitHub Desktop.
Simple Restic REST backend build for testing purposes
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/restic/restic/backend"
"gopkg.in/tylerb/graceful.v1"
)
func die(message string, args ...interface{}) {
fmt.Fprintf(os.Stderr, message, args...)
os.Exit(1)
}
// Simple Restic REST backend build for testing purposes
func main() {
// Initializing a temporary repository
path, err := ioutil.TempDir("", "restic-repository-")
if err != nil {
die("TempDir(): %v\n", err)
}
dirs := []string{
path,
filepath.Join(path, "data"),
filepath.Join(path, "snapshot"),
filepath.Join(path, "index"),
filepath.Join(path, "lock"),
filepath.Join(path, "key"),
filepath.Join(path, "temp"),
}
for _, d := range dirs {
fmt.Println(d)
os.MkdirAll(d, backend.Modes.Dir)
}
// Routing the repository requests
r := mux.NewRouter()
// Exists
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
file := filepath.Join(path, "config")
if _, err := os.Stat(file); err != nil {
http.Error(w, "No repository here", 404)
}
}).Methods("HEAD")
// List blobs
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
file := filepath.Join(path, "config")
if _, err := os.Stat(file); err != nil {
http.Error(w, "No repository here", 404)
}
}).Methods("GET")
// Head file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err != nil {
http.Error(w, "File not found", 404)
}
}).Methods("HEAD")
// Get file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err == nil {
bytes, _ := ioutil.ReadFile(file)
w.Write(bytes)
} else {
http.Error(w, "File not found", 404)
}
}).Methods("GET")
// Put file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err == nil {
fmt.Fprintf(w, "Blob already uploaded", 404)
} else {
bytes, _ := ioutil.ReadAll(r.Body)
ioutil.WriteFile(file, bytes, 0600)
}
}).Methods("POST")
// Delete file
r.HandleFunc("/{file}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file := filepath.Join(path, vars["file"])
if _, err := os.Stat(file); err == nil {
os.Remove(file)
} else {
fmt.Fprintf(w, "File not found", 404)
}
}).Methods("DELETE")
// List blobs
r.HandleFunc("/{type}/", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
path := filepath.Join(path, vars["type"])
files, _ := ioutil.ReadDir(path)
names := make([]string, len(files))
for i, f := range files {
names[i] = f.Name()
}
data, _ := json.Marshal(names)
w.Write(data)
}).Methods("GET")
// Head blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err != nil {
http.Error(w, "File not found", 404)
}
}).Methods("HEAD")
// Get blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err == nil {
bytes, _ := ioutil.ReadFile(blob)
w.Write(bytes)
} else {
http.Error(w, "Blob not found", 404)
}
}).Methods("GET")
// Put blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err == nil {
fmt.Fprintf(w, "Blob already uploaded", 404)
} else {
bytes, _ := ioutil.ReadAll(r.Body)
ioutil.WriteFile(blob, bytes, 0600)
}
}).Methods("POST")
// Delete blob
r.HandleFunc("/{type}/{blob}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blob := filepath.Join(path, vars["type"], vars["blob"])
if _, err := os.Stat(blob); err == nil {
os.Remove(blob)
} else {
fmt.Fprintf(w, "Blob not found", 404)
}
}).Methods("DELETE")
// Starting the http server
n := negroni.Classic()
n.UseHandler(r)
n.Run(":8000")
graceful.Run(addr, 5*time.Second, n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment