rest-server/main.go

105 lines
2.4 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-07 15:29:24 +02:00
"flag"
"fmt"
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"
2016-11-06 19:38:41 +01:00
"runtime/pprof"
2015-08-15 10:06:10 +02:00
)
2015-08-11 14:14:07 +02:00
2016-11-11 01:24:06 +01:00
func createDirectories(path string) {
2016-11-06 19:20:58 +01:00
log.Println("Creating repository directories")
2016-11-11 01:24:06 +01:00
2015-09-16 23:34:11 +02:00
dirs := []string{
2015-09-19 14:28:26 +02:00
"data",
"index",
"keys",
2016-11-05 18:30:44 +01:00
"locks",
"snapshots",
"tmp",
2015-09-16 23:34:11 +02:00
}
2016-11-11 01:24:06 +01:00
2015-09-16 23:34:11 +02:00
for _, d := range dirs {
2016-11-11 01:24:06 +01:00
if err := os.MkdirAll(filepath.Join(path, d), 0700); err != nil {
2016-11-11 00:55:13 +01:00
log.Fatal(err)
}
2015-09-16 23:34:11 +02:00
}
2016-11-11 01:24:06 +01:00
for i := 0; i < 256; i++ {
2016-11-11 01:24:06 +01:00
if err := os.MkdirAll(filepath.Join(path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
2016-11-11 00:55:13 +01:00
log.Fatal(err)
}
}
2016-11-11 01:24:06 +01:00
}
func setupRoutes(path string) *Router {
context := &Context{path}
2015-09-16 23:34:11 +02:00
2015-09-18 17:13:38 +02:00
router := NewRouter()
router.HeadFunc("/config", CheckConfig(context))
router.GetFunc("/config", GetConfig(context))
router.PostFunc("/config", SaveConfig(context))
router.GetFunc("/:dir/", ListBlobs(context))
router.HeadFunc("/:dir/:name", CheckBlob(context))
router.GetFunc("/:type/:name", GetBlob(context))
router.PostFunc("/:type/:name", SaveBlob(context))
router.DeleteFunc("/:type/:name", DeleteBlob(context))
2015-09-16 23:34:11 +02:00
2016-11-11 01:24:06 +01:00
return router
}
func main() {
log.SetFlags(0)
var cpuprofile = flag.String("cpuprofile", "", "write CPU profile to file")
var listen = flag.String("listen", ":8000", "listen address")
var path = flag.String("path", "/tmp/restic", "data directory")
var tls = flag.Bool("tls", false, "turn on TLS support")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
log.Println("CPU profiling enabled")
defer pprof.StopCPUProfile()
}
createDirectories(*path)
router := setupRoutes(*path)
2015-09-19 14:52:50 +02:00
var handler http.Handler
htpasswdFile, err := NewHtpasswdFromFile(filepath.Join(*path, ".htpasswd"))
if err != nil {
handler = router
2016-11-11 01:24:06 +01:00
log.Println("Authentication disabled")
2015-09-19 14:52:50 +02:00
} else {
handler = AuthHandler(htpasswdFile, router)
2016-11-11 01:24:06 +01:00
log.Println("Authentication enabled")
2015-09-19 14:52:50 +02:00
}
2015-09-07 15:29:24 +02:00
if !*tls {
2016-11-06 12:20:09 +01:00
log.Printf("Starting server on %s\n", *listen)
err = http.ListenAndServe(*listen, handler)
2015-09-07 15:29:24 +02:00
} else {
privateKey := filepath.Join(*path, "private_key")
publicKey := filepath.Join(*path, "public_key")
2015-09-19 14:52:50 +02:00
log.Println("TLS enabled")
2016-11-06 12:11:32 +01:00
log.Printf("Private key: %s", privateKey)
log.Printf("Public key: %s", publicKey)
2016-11-06 12:20:09 +01:00
log.Printf("Starting server on %s\n", *listen)
err = http.ListenAndServeTLS(*listen, publicKey, privateKey, handler)
2016-11-06 12:11:32 +01:00
}
if err != nil {
log.Fatal(err)
2015-09-07 15:29:24 +02:00
}
2015-08-11 14:14:07 +02:00
}