Update of https://gist.github.com/bchapuis/16d9dd0a8ae1ba80bcdd
Last active
November 23, 2015 23:12
-
-
Save xor-gate/fa3e7c9dedac1f39c42d to your computer and use it in GitHub Desktop.
Restic HTTP backend test server
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"io" | |
"log" | |
"path/filepath" | |
"github.com/gorilla/mux" | |
"github.com/restic/restic/backend" | |
) | |
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, "keys"), | |
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) | |
} | |
log.Println("HEAD %s", file) | |
}).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) | |
} | |
log.Println("GET %s", file) | |
}).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) | |
} | |
log.Println("HEAD %s", file) | |
}).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 { | |
log.Println("GET %s", file) | |
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 { | |
log.Println("POST %s", file) | |
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0666) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer f.Close() | |
io.Copy(f, r.Body) | |
} | |
}).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 { | |
log.Println("GET %s", blob) | |
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 { | |
log.Println("POST %s", blob) | |
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 { | |
log.Println("DELETE %s", blob) | |
os.Remove(blob) | |
} else { | |
fmt.Fprintf(w, "Blob not found", 404) | |
} | |
}).Methods("DELETE") | |
http.ListenAndServe(":8080", r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment