mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
Create directories before key setup, not on startup
This commit is contained in:
parent
75c1eae7f2
commit
061d31829d
2 changed files with 32 additions and 28 deletions
32
handlers.go
32
handlers.go
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -21,6 +22,31 @@ func isHashed(dir string) bool {
|
|||
return dir == "data"
|
||||
}
|
||||
|
||||
func createDirectories(c *Context) {
|
||||
log.Println("Creating repository directories")
|
||||
|
||||
dirs := []string{
|
||||
"data",
|
||||
"index",
|
||||
"keys",
|
||||
"locks",
|
||||
"snapshots",
|
||||
"tmp",
|
||||
}
|
||||
|
||||
for _, d := range dirs {
|
||||
if err := os.MkdirAll(filepath.Join(c.path, d), 0700); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 256; i++ {
|
||||
if err := os.MkdirAll(filepath.Join(c.path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
@ -172,6 +198,12 @@ func SaveBlob(c *Context) http.HandlerFunc {
|
|||
dir := vars[1]
|
||||
name := vars[2]
|
||||
|
||||
if dir == "keys" {
|
||||
if _, err := os.Stat("keys"); err != nil && os.IsNotExist(err) {
|
||||
createDirectories(c)
|
||||
}
|
||||
}
|
||||
|
||||
tmp := filepath.Join(c.path, "tmp", name)
|
||||
|
||||
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
|
|
28
main.go
28
main.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -10,31 +9,6 @@ import (
|
|||
"runtime/pprof"
|
||||
)
|
||||
|
||||
func createDirectories(path string) {
|
||||
log.Println("Creating repository directories")
|
||||
|
||||
dirs := []string{
|
||||
"data",
|
||||
"index",
|
||||
"keys",
|
||||
"locks",
|
||||
"snapshots",
|
||||
"tmp",
|
||||
}
|
||||
|
||||
for _, d := range dirs {
|
||||
if err := os.MkdirAll(filepath.Join(path, d), 0700); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 256; i++ {
|
||||
if err := os.MkdirAll(filepath.Join(path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setupRoutes(path string) *Router {
|
||||
context := &Context{path}
|
||||
|
||||
|
@ -72,8 +46,6 @@ func main() {
|
|||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
createDirectories(*path)
|
||||
|
||||
router := setupRoutes(*path)
|
||||
|
||||
var handler http.Handler
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue