rest-server/server.go

169 lines
3.9 KiB
Go
Raw Normal View History

2015-08-11 14:14:07 +02:00
package main
2015-08-15 10:06:10 +02:00
import (
2015-09-16 23:34:11 +02:00
"encoding/json"
2015-09-07 15:29:24 +02:00
"flag"
2015-09-16 23:34:11 +02:00
"io/ioutil"
2015-08-15 10:06:10 +02:00
"log"
"net/http"
2015-09-16 23:34:11 +02:00
"os"
2015-09-07 15:29:24 +02:00
"path/filepath"
2015-09-16 23:34:11 +02:00
"strings"
"time"
"github.com/restic/restic/backend"
2015-08-15 10:06:10 +02:00
)
2015-08-11 14:14:07 +02:00
2015-09-07 15:29:24 +02:00
const (
HTTP = ":8000"
HTTPS = ":8443"
)
2015-08-15 10:06:10 +02:00
2015-09-07 15:29:24 +02:00
func main() {
2015-09-16 23:34:11 +02:00
// Parse command-line args
2015-09-07 15:29:24 +02:00
var path = flag.String("path", "/tmp/restic", "specifies the path of the data directory")
var tls = flag.Bool("tls", false, "turns on tls support")
flag.Parse()
2015-08-15 10:06:10 +02:00
2015-09-16 23:34:11 +02:00
// Create all the necessary subdirectories
dirs := []string{
backend.Paths.Data,
backend.Paths.Snapshots,
backend.Paths.Index,
backend.Paths.Locks,
backend.Paths.Keys,
}
for _, d := range dirs {
os.MkdirAll(filepath.Join(*path, d), backend.Modes.Dir)
}
2015-09-17 13:46:29 +02:00
router := NewRouter()
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Head("/config", func(w http.ResponseWriter, r *http.Request) {
config := filepath.Join(*path, "config")
if _, err := os.Stat(config); err != nil {
http.Error(w, "404 not found", 404)
return
}
w.Write([]byte("200 ok"))
})
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Get("/config", func(w http.ResponseWriter, r *http.Request) {
config := filepath.Join(*path, "config")
bytes, err := ioutil.ReadFile(config)
if err != nil {
http.Error(w, "404 not found", 404)
return
}
w.Write(bytes)
})
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Post("/config", func(w http.ResponseWriter, r *http.Request) {
config := filepath.Join(*path, "config")
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "400 bad request", 400)
2015-09-16 23:34:11 +02:00
return
}
2015-09-17 13:46:29 +02:00
errw := ioutil.WriteFile(config, bytes, 0600)
if errw != nil {
http.Error(w, "500 internal server error", 500)
return
}
w.Write([]byte("200 ok"))
})
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Get("/:dir/", func(w http.ResponseWriter, r *http.Request) {
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
path := filepath.Join(*path, dir)
files, err := ioutil.ReadDir(path)
if err != nil {
http.Error(w, "404 not found", 404)
2015-09-16 23:34:11 +02:00
return
}
2015-09-17 13:46:29 +02:00
names := make([]string, len(files))
for i, f := range files {
names[i] = f.Name()
}
data, err := json.Marshal(names)
if err != nil {
http.Error(w, "500 internal server error", 500)
return
}
w.Write(data)
})
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Head("/:dir/:name", func(w http.ResponseWriter, r *http.Request) {
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
path := filepath.Join(*path, dir, name)
_, err := os.Stat(path)
if err != nil {
http.Error(w, "404 not found", 404)
2015-09-16 23:34:11 +02:00
return
}
2015-09-17 13:46:29 +02:00
w.Write([]byte("200 ok"))
})
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Get("/:type/:name", func(w http.ResponseWriter, r *http.Request) {
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
path := filepath.Join(*path, dir, name)
file, err := os.Open(path)
if err != nil {
http.Error(w, "404 not found", 404)
return
}
defer file.Close()
http.ServeContent(w, r, "", time.Unix(0, 0), file)
2015-09-16 23:34:11 +02:00
})
2015-09-17 13:46:29 +02:00
router.Post("/:type/:name", func(w http.ResponseWriter, r *http.Request) {
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
path := filepath.Join(*path, dir, name)
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "400 bad request", 400)
return
}
errw := ioutil.WriteFile(path, bytes, 0600)
if errw != nil {
http.Error(w, "500 internal server error", 500)
return
}
w.Write([]byte("200 ok"))
})
2015-09-16 23:34:11 +02:00
2015-09-17 13:46:29 +02:00
router.Delete("/:type/:name", func(w http.ResponseWriter, r *http.Request) {
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
path := filepath.Join(*path, dir, name)
err := os.Remove(path)
if err != nil {
http.Error(w, "500 internal server error", 500)
return
}
w.Write([]byte("200 ok"))
})
2015-09-16 23:34:11 +02:00
// start the server
2015-09-07 15:29:24 +02:00
if !*tls {
log.Printf("start server on port %s", HTTP)
http.ListenAndServe(HTTP, router)
} else {
log.Printf("start server on port %s", HTTPS)
privateKey := filepath.Join(*path, "private_key")
publicKey := filepath.Join(*path, "public_key")
2015-09-07 16:17:24 +02:00
log.Printf("private key: %s", privateKey)
log.Printf("public key: %s", publicKey)
http.ListenAndServeTLS(HTTPS, publicKey, privateKey, router)
2015-09-07 15:29:24 +02:00
}
2015-08-11 14:14:07 +02:00
}