mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
Refactor handlers: make Config not global
This commit is contained in:
parent
7dd5483ea3
commit
b98c171644
5 changed files with 212 additions and 196 deletions
|
@ -25,73 +25,88 @@ var cmdRoot = &cobra.Command{
|
||||||
//Version: fmt.Sprintf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH),
|
//Version: fmt.Sprintf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
repoPath = "/tmp/restic"
|
||||||
|
listen = ":8000"
|
||||||
|
logFile string
|
||||||
|
cpuProfile string
|
||||||
|
tlsKey string
|
||||||
|
tlsCert string
|
||||||
|
useTLS bool
|
||||||
|
noAuth bool
|
||||||
|
appendOnly bool
|
||||||
|
privateRepos bool
|
||||||
|
prometheus bool
|
||||||
|
debug bool
|
||||||
|
showVersion bool
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flags := cmdRoot.Flags()
|
flags := cmdRoot.Flags()
|
||||||
flags.StringVar(&restserver.Config.CPUProfile, "cpu-profile", restserver.Config.CPUProfile, "write CPU profile to file")
|
flags.StringVar(&cpuProfile, "cpu-profile", cpuProfile, "write CPU profile to file")
|
||||||
flags.BoolVar(&restserver.Config.Debug, "debug", restserver.Config.Debug, "output debug messages")
|
flags.BoolVar(&debug, "debug", debug, "output debug messages")
|
||||||
flags.StringVar(&restserver.Config.Listen, "listen", restserver.Config.Listen, "listen address")
|
flags.StringVar(&listen, "listen", listen, "listen address")
|
||||||
flags.StringVar(&restserver.Config.Log, "log", restserver.Config.Log, "log HTTP requests in the combined log format")
|
flags.StringVar(&logFile, "log", logFile, "log HTTP requests in the combined log format")
|
||||||
flags.StringVar(&restserver.Config.Path, "path", restserver.Config.Path, "data directory")
|
flags.StringVar(&repoPath, "path", repoPath, "data directory")
|
||||||
flags.BoolVar(&restserver.Config.TLS, "tls", restserver.Config.TLS, "turn on TLS support")
|
flags.BoolVar(&useTLS, "tls", useTLS, "turn on TLS support")
|
||||||
flags.StringVar(&restserver.Config.TLSCert, "tls-cert", restserver.Config.TLSCert, "TLS certificate path")
|
flags.StringVar(&tlsCert, "tls-cert", tlsCert, "TLS certificate path")
|
||||||
flags.StringVar(&restserver.Config.TLSKey, "tls-key", restserver.Config.TLSKey, "TLS key path")
|
flags.StringVar(&tlsKey, "tls-key", tlsKey, "TLS key path")
|
||||||
flags.BoolVar(&restserver.Config.NoAuth, "no-auth", restserver.Config.NoAuth, "disable .htpasswd authentication")
|
flags.BoolVar(&noAuth, "no-auth", noAuth, "disable .htpasswd authentication")
|
||||||
flags.BoolVar(&restserver.Config.AppendOnly, "append-only", restserver.Config.AppendOnly, "enable append only mode")
|
flags.BoolVar(&appendOnly, "append-only", appendOnly, "enable append only mode")
|
||||||
flags.BoolVar(&restserver.Config.PrivateRepos, "private-repos", restserver.Config.PrivateRepos, "users can only access their private repo")
|
flags.BoolVar(&privateRepos, "private-repos", privateRepos, "users can only access their private repo")
|
||||||
flags.BoolVar(&restserver.Config.Prometheus, "prometheus", restserver.Config.Prometheus, "enable Prometheus metrics")
|
flags.BoolVar(&prometheus, "prometheus", prometheus, "enable Prometheus metrics")
|
||||||
flags.BoolVarP(&restserver.Config.Version, "version", "V", restserver.Config.Version, "output version and exit")
|
flags.BoolVarP(&showVersion, "version", "V", showVersion, "output version and exit")
|
||||||
}
|
}
|
||||||
|
|
||||||
var version = "manually"
|
var version = "manually"
|
||||||
|
|
||||||
func tlsSettings() (bool, string, string, error) {
|
func tlsSettings() (bool, string, string, error) {
|
||||||
var key, cert string
|
var key, cert string
|
||||||
enabledTLS := restserver.Config.TLS
|
if !useTLS && (tlsKey != "" || tlsCert != "") {
|
||||||
if !enabledTLS && (restserver.Config.TLSKey != "" || restserver.Config.TLSCert != "") {
|
|
||||||
return false, "", "", errors.New("requires enabled TLS")
|
return false, "", "", errors.New("requires enabled TLS")
|
||||||
} else if !enabledTLS {
|
} else if !useTLS {
|
||||||
return false, "", "", nil
|
return false, "", "", nil
|
||||||
}
|
}
|
||||||
if restserver.Config.TLSKey != "" {
|
if tlsKey != "" {
|
||||||
key = restserver.Config.TLSKey
|
key = tlsKey
|
||||||
} else {
|
} else {
|
||||||
key = filepath.Join(restserver.Config.Path, "private_key")
|
key = filepath.Join(repoPath, "private_key")
|
||||||
}
|
}
|
||||||
if restserver.Config.TLSCert != "" {
|
if tlsCert != "" {
|
||||||
cert = restserver.Config.TLSCert
|
cert = tlsCert
|
||||||
} else {
|
} else {
|
||||||
cert = filepath.Join(restserver.Config.Path, "public_key")
|
cert = filepath.Join(repoPath, "public_key")
|
||||||
}
|
}
|
||||||
return enabledTLS, key, cert, nil
|
return useTLS, key, cert, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHandler() (http.Handler, error) {
|
func getHandler(config restserver.Config) (http.Handler, error) {
|
||||||
mux := restserver.NewMux()
|
mux := restserver.NewHandler(config)
|
||||||
if restserver.Config.NoAuth {
|
if config.NoAuth {
|
||||||
log.Println("Authentication disabled")
|
log.Println("Authentication disabled")
|
||||||
return mux, nil
|
return mux, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Authentication enabled")
|
log.Println("Authentication enabled")
|
||||||
htpasswdFile, err := restserver.NewHtpasswdFromFile(filepath.Join(restserver.Config.Path, ".htpasswd"))
|
htpasswdFile, err := restserver.NewHtpasswdFromFile(filepath.Join(config.Path, ".htpasswd"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot load .htpasswd (use --no-auth to disable): %v", err)
|
return nil, fmt.Errorf("cannot load .htpasswd (use --no-auth to disable): %v", err)
|
||||||
}
|
}
|
||||||
return restserver.AuthHandler(htpasswdFile, mux), nil
|
return config.AuthHandler(htpasswdFile, mux), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRoot(cmd *cobra.Command, args []string) error {
|
func runRoot(cmd *cobra.Command, args []string) error {
|
||||||
if restserver.Config.Version {
|
if showVersion {
|
||||||
fmt.Printf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
fmt.Printf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
log.Printf("Data directory: %s", restserver.Config.Path)
|
log.Printf("Data directory: %s", repoPath)
|
||||||
|
|
||||||
if restserver.Config.CPUProfile != "" {
|
if cpuProfile != "" {
|
||||||
f, err := os.Create(restserver.Config.CPUProfile)
|
f, err := os.Create(cpuProfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -102,12 +117,27 @@ func runRoot(cmd *cobra.Command, args []string) error {
|
||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
handler, err := getHandler()
|
config := restserver.Config{
|
||||||
|
Path: repoPath,
|
||||||
|
Listen: listen,
|
||||||
|
Log: logFile,
|
||||||
|
CPUProfile: cpuProfile,
|
||||||
|
TLSKey: tlsKey,
|
||||||
|
TLSCert: tlsCert,
|
||||||
|
TLS: useTLS,
|
||||||
|
NoAuth: noAuth,
|
||||||
|
AppendOnly: appendOnly,
|
||||||
|
PrivateRepos: privateRepos,
|
||||||
|
Prometheus: prometheus,
|
||||||
|
Debug: debug,
|
||||||
|
}
|
||||||
|
|
||||||
|
handler, err := getHandler(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: %v", err)
|
log.Fatalf("error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if restserver.Config.PrivateRepos {
|
if privateRepos {
|
||||||
log.Println("Private repositories enabled")
|
log.Println("Private repositories enabled")
|
||||||
} else {
|
} else {
|
||||||
log.Println("Private repositories disabled")
|
log.Println("Private repositories disabled")
|
||||||
|
@ -118,15 +148,15 @@ func runRoot(cmd *cobra.Command, args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !enabledTLS {
|
if !enabledTLS {
|
||||||
log.Printf("Starting server on %s\n", restserver.Config.Listen)
|
log.Printf("Starting server on %s\n", listen)
|
||||||
err = http.ListenAndServe(restserver.Config.Listen, handler)
|
err = http.ListenAndServe(listen, handler)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
log.Println("TLS enabled")
|
log.Println("TLS enabled")
|
||||||
log.Printf("Private key: %s", privateKey)
|
log.Printf("Private key: %s", privateKey)
|
||||||
log.Printf("Public key(certificate): %s", publicKey)
|
log.Printf("Public key(certificate): %s", publicKey)
|
||||||
log.Printf("Starting server on %s\n", restserver.Config.Listen)
|
log.Printf("Starting server on %s\n", listen)
|
||||||
err = http.ListenAndServeTLS(restserver.Config.Listen, publicKey, privateKey, handler)
|
err = http.ListenAndServeTLS(listen, publicKey, privateKey, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -35,17 +35,16 @@ func TestTLSSettings(t *testing.T) {
|
||||||
{passed{Path: "/tmp", TLS: false, TLSCert: "/etc/restic/cert"}, expected{"", "", true}},
|
{passed{Path: "/tmp", TLS: false, TLSCert: "/etc/restic/cert"}, expected{"", "", true}},
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultConfig := restserver.Config
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
||||||
t.Run("", func(t *testing.T) {
|
t.Run("", func(t *testing.T) {
|
||||||
defer func() { restserver.Config = defaultConfig }()
|
// defer func() { restserver.Config = defaultConfig }()
|
||||||
if test.passed.Path != "" {
|
if test.passed.Path != "" {
|
||||||
restserver.Config.Path = test.passed.Path
|
repoPath = test.passed.Path
|
||||||
}
|
}
|
||||||
restserver.Config.TLS = test.passed.TLS
|
useTLS = test.passed.TLS
|
||||||
restserver.Config.TLSKey = test.passed.TLSKey
|
tlsKey = test.passed.TLSKey
|
||||||
restserver.Config.TLSCert = test.passed.TLSCert
|
tlsCert = test.passed.TLSCert
|
||||||
|
|
||||||
gotTLS, gotKey, gotCert, err := tlsSettings()
|
gotTLS, gotKey, gotCert, err := tlsSettings()
|
||||||
if err != nil && !test.expected.Error {
|
if err != nil && !test.expected.Error {
|
||||||
|
@ -76,27 +75,20 @@ func TestTLSSettings(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetHandler(t *testing.T) {
|
func TestGetHandler(t *testing.T) {
|
||||||
// Save and restore config
|
|
||||||
defaultConfig := restserver.Config
|
|
||||||
defer func() { restserver.Config = defaultConfig }()
|
|
||||||
|
|
||||||
dir, err := ioutil.TempDir("", "rest-server-test")
|
dir, err := ioutil.TempDir("", "rest-server-test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(dir)
|
defer os.Remove(dir)
|
||||||
restserver.Config.Path = dir
|
|
||||||
|
|
||||||
// With NoAuth = false and no .htpasswd
|
// With NoAuth = false and no .htpasswd
|
||||||
restserver.Config.NoAuth = false // default
|
_, err = getHandler(restserver.Config{Path: dir})
|
||||||
_, err = getHandler()
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("NoAuth=false: expected error, got nil")
|
t.Errorf("NoAuth=false: expected error, got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
// With NoAuth = true and no .htpasswd
|
// With NoAuth = true and no .htpasswd
|
||||||
restserver.Config.NoAuth = true
|
_, err = getHandler(restserver.Config{NoAuth: true, Path: dir})
|
||||||
_, err = getHandler()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NoAuth=true: expected no error, got %v", err)
|
t.Errorf("NoAuth=true: expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
@ -110,8 +102,7 @@ func TestGetHandler(t *testing.T) {
|
||||||
defer os.Remove(htpasswd)
|
defer os.Remove(htpasswd)
|
||||||
|
|
||||||
// With NoAuth = false and with .htpasswd
|
// With NoAuth = false and with .htpasswd
|
||||||
restserver.Config.NoAuth = false // default
|
_, err = getHandler(restserver.Config{Path: dir})
|
||||||
_, err = getHandler()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NoAuth=false with .htpasswd: expected no error, got %v", err)
|
t.Errorf("NoAuth=false with .htpasswd: expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
184
handlers.go
184
handlers.go
|
@ -20,7 +20,23 @@ import (
|
||||||
"goji.io/pat"
|
"goji.io/pat"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isHashed(dir string) bool {
|
// Config determines how a Mux's handlers behave.
|
||||||
|
type Config struct {
|
||||||
|
Path string
|
||||||
|
Listen string
|
||||||
|
Log string
|
||||||
|
CPUProfile string
|
||||||
|
TLSKey string
|
||||||
|
TLSCert string
|
||||||
|
TLS bool
|
||||||
|
NoAuth bool
|
||||||
|
AppendOnly bool
|
||||||
|
PrivateRepos bool
|
||||||
|
Prometheus bool
|
||||||
|
Debug bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) isHashed(dir string) bool {
|
||||||
return dir == "data"
|
return dir == "data"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +55,7 @@ func valid(name string) bool {
|
||||||
|
|
||||||
var validTypes = []string{"data", "index", "keys", "locks", "snapshots", "config"}
|
var validTypes = []string{"data", "index", "keys", "locks", "snapshots", "config"}
|
||||||
|
|
||||||
func isValidType(name string) bool {
|
func (c Config) isValidType(name string) bool {
|
||||||
for _, tpe := range validTypes {
|
for _, tpe := range validTypes {
|
||||||
if name == tpe {
|
if name == tpe {
|
||||||
return true
|
return true
|
||||||
|
@ -69,8 +85,8 @@ func join(base string, names ...string) (string, error) {
|
||||||
return filepath.Join(clean...), nil
|
return filepath.Join(clean...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRepo returns the repository location, relative to Config.Path.
|
// getRepo returns the repository location, relative to c.Path.
|
||||||
func getRepo(r *http.Request) string {
|
func (c Config) getRepo(r *http.Request) string {
|
||||||
if strings.HasPrefix(fmt.Sprintf("%s", middleware.Pattern(r.Context())), "/:repo") {
|
if strings.HasPrefix(fmt.Sprintf("%s", middleware.Pattern(r.Context())), "/:repo") {
|
||||||
return pat.Param(r, "repo")
|
return pat.Param(r, "repo")
|
||||||
}
|
}
|
||||||
|
@ -79,32 +95,32 @@ func getRepo(r *http.Request) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPath returns the path for a file type in the repo.
|
// getPath returns the path for a file type in the repo.
|
||||||
func getPath(r *http.Request, fileType string) (string, error) {
|
func (c Config) getPath(r *http.Request, fileType string) (string, error) {
|
||||||
if !isValidType(fileType) {
|
if !c.isValidType(fileType) {
|
||||||
return "", errors.New("invalid file type")
|
return "", errors.New("invalid file type")
|
||||||
}
|
}
|
||||||
return join(Config.Path, getRepo(r), fileType)
|
return join(c.Path, c.getRepo(r), fileType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getFilePath returns the path for a file in the repo.
|
// getFilePath returns the path for a file in the repo.
|
||||||
func getFilePath(r *http.Request, fileType, name string) (string, error) {
|
func (c Config) getFilePath(r *http.Request, fileType, name string) (string, error) {
|
||||||
if !isValidType(fileType) {
|
if !c.isValidType(fileType) {
|
||||||
return "", errors.New("invalid file type")
|
return "", errors.New("invalid file type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if isHashed(fileType) {
|
if c.isHashed(fileType) {
|
||||||
if len(name) < 2 {
|
if len(name) < 2 {
|
||||||
return "", errors.New("file name is too short")
|
return "", errors.New("file name is too short")
|
||||||
}
|
}
|
||||||
|
|
||||||
return join(Config.Path, getRepo(r), fileType, name[:2], name)
|
return join(c.Path, c.getRepo(r), fileType, name[:2], name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return join(Config.Path, getRepo(r), fileType, name)
|
return join(c.Path, c.getRepo(r), fileType, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getUser returns the username from the request, or an empty string if none.
|
// getUser returns the username from the request, or an empty string if none.
|
||||||
func getUser(r *http.Request) string {
|
func (c Config) getUser(r *http.Request) string {
|
||||||
username, _, ok := r.BasicAuth()
|
username, _, ok := r.BasicAuth()
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return ""
|
||||||
|
@ -113,10 +129,10 @@ func getUser(r *http.Request) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMetricLabels returns the prometheus labels from the request.
|
// getMetricLabels returns the prometheus labels from the request.
|
||||||
func getMetricLabels(r *http.Request) prometheus.Labels {
|
func (c Config) getMetricLabels(r *http.Request) prometheus.Labels {
|
||||||
labels := prometheus.Labels{
|
labels := prometheus.Labels{
|
||||||
"user": getUser(r),
|
"user": c.getUser(r),
|
||||||
"repo": getRepo(r),
|
"repo": c.getRepo(r),
|
||||||
"type": pat.Param(r, "type"),
|
"type": pat.Param(r, "type"),
|
||||||
}
|
}
|
||||||
return labels
|
return labels
|
||||||
|
@ -134,14 +150,14 @@ func isUserPath(username, path string) bool {
|
||||||
|
|
||||||
// AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
|
// AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
|
||||||
// stored in f and returns the http.HandlerFunc.
|
// stored in f and returns the http.HandlerFunc.
|
||||||
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
func (c Config) AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
username, password, ok := r.BasicAuth()
|
username, password, ok := r.BasicAuth()
|
||||||
if !ok || !f.Validate(username, password) {
|
if !ok || !f.Validate(username, password) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if Config.PrivateRepos && !isUserPath(username, r.URL.Path) {
|
if c.PrivateRepos && !isUserPath(username, r.URL.Path) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -150,11 +166,11 @@ func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckConfig checks whether a configuration exists.
|
// CheckConfig checks whether a configuration exists.
|
||||||
func CheckConfig(w http.ResponseWriter, r *http.Request) {
|
func (c Config) CheckConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("CheckConfig()")
|
log.Println("CheckConfig()")
|
||||||
}
|
}
|
||||||
cfg, err := getPath(r, "config")
|
cfg, err := c.getPath(r, "config")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -162,7 +178,7 @@ func CheckConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
st, err := os.Stat(cfg)
|
st, err := os.Stat(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -173,11 +189,11 @@ func CheckConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfig allows for a config to be retrieved.
|
// GetConfig allows for a config to be retrieved.
|
||||||
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
func (c Config) GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("GetConfig()")
|
log.Println("GetConfig()")
|
||||||
}
|
}
|
||||||
cfg, err := getPath(r, "config")
|
cfg, err := c.getPath(r, "config")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -185,7 +201,7 @@ func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
bytes, err := ioutil.ReadFile(cfg)
|
bytes, err := ioutil.ReadFile(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -196,11 +212,11 @@ func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveConfig allows for a config to be saved.
|
// SaveConfig allows for a config to be saved.
|
||||||
func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
func (c Config) SaveConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("SaveConfig()")
|
log.Println("SaveConfig()")
|
||||||
}
|
}
|
||||||
cfg, err := getPath(r, "config")
|
cfg, err := c.getPath(r, "config")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -208,7 +224,7 @@ func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
f, err := os.OpenFile(cfg, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
|
f, err := os.OpenFile(cfg, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
|
||||||
if err != nil && os.IsExist(err) {
|
if err != nil && os.IsExist(err) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
||||||
|
@ -217,7 +233,7 @@ func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
_, err = io.Copy(f, r.Body)
|
_, err = io.Copy(f, r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -234,24 +250,24 @@ func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteConfig removes a config.
|
// DeleteConfig removes a config.
|
||||||
func DeleteConfig(w http.ResponseWriter, r *http.Request) {
|
func (c Config) DeleteConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("DeleteConfig()")
|
log.Println("DeleteConfig()")
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.AppendOnly {
|
if c.AppendOnly {
|
||||||
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := getPath(r, "config")
|
cfg, err := c.getPath(r, "config")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Remove(cfg); err != nil {
|
if err := os.Remove(cfg); err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
@ -269,26 +285,26 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ListBlobs lists all blobs of a given type in an arbitrary order.
|
// ListBlobs lists all blobs of a given type in an arbitrary order.
|
||||||
func ListBlobs(w http.ResponseWriter, r *http.Request) {
|
func (c Config) ListBlobs(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("ListBlobs()")
|
log.Println("ListBlobs()")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch r.Header.Get("Accept") {
|
switch r.Header.Get("Accept") {
|
||||||
case mimeTypeAPIV2:
|
case mimeTypeAPIV2:
|
||||||
ListBlobsV2(w, r)
|
c.ListBlobsV2(w, r)
|
||||||
default:
|
default:
|
||||||
ListBlobsV1(w, r)
|
c.ListBlobsV1(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListBlobsV1 lists all blobs of a given type in an arbitrary order.
|
// ListBlobsV1 lists all blobs of a given type in an arbitrary order.
|
||||||
func ListBlobsV1(w http.ResponseWriter, r *http.Request) {
|
func (c Config) ListBlobsV1(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("ListBlobsV1()")
|
log.Println("ListBlobsV1()")
|
||||||
}
|
}
|
||||||
fileType := pat.Param(r, "type")
|
fileType := pat.Param(r, "type")
|
||||||
path, err := getPath(r, fileType)
|
path, err := c.getPath(r, fileType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -296,7 +312,7 @@ func ListBlobsV1(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
items, err := ioutil.ReadDir(path)
|
items, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -305,12 +321,12 @@ func ListBlobsV1(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var names []string
|
var names []string
|
||||||
for _, i := range items {
|
for _, i := range items {
|
||||||
if isHashed(fileType) {
|
if c.isHashed(fileType) {
|
||||||
subpath := filepath.Join(path, i.Name())
|
subpath := filepath.Join(path, i.Name())
|
||||||
var subitems []os.FileInfo
|
var subitems []os.FileInfo
|
||||||
subitems, err = ioutil.ReadDir(subpath)
|
subitems, err = ioutil.ReadDir(subpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -326,7 +342,7 @@ func ListBlobsV1(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
data, err := json.Marshal(names)
|
data, err := json.Marshal(names)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -344,12 +360,12 @@ type Blob struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListBlobsV2 lists all blobs of a given type, together with their sizes, in an arbitrary order.
|
// ListBlobsV2 lists all blobs of a given type, together with their sizes, in an arbitrary order.
|
||||||
func ListBlobsV2(w http.ResponseWriter, r *http.Request) {
|
func (c Config) ListBlobsV2(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("ListBlobsV2()")
|
log.Println("ListBlobsV2()")
|
||||||
}
|
}
|
||||||
fileType := pat.Param(r, "type")
|
fileType := pat.Param(r, "type")
|
||||||
path, err := getPath(r, fileType)
|
path, err := c.getPath(r, fileType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -357,7 +373,7 @@ func ListBlobsV2(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
items, err := ioutil.ReadDir(path)
|
items, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -366,12 +382,12 @@ func ListBlobsV2(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var blobs []Blob
|
var blobs []Blob
|
||||||
for _, i := range items {
|
for _, i := range items {
|
||||||
if isHashed(fileType) {
|
if c.isHashed(fileType) {
|
||||||
subpath := filepath.Join(path, i.Name())
|
subpath := filepath.Join(path, i.Name())
|
||||||
var subitems []os.FileInfo
|
var subitems []os.FileInfo
|
||||||
subitems, err = ioutil.ReadDir(subpath)
|
subitems, err = ioutil.ReadDir(subpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -387,7 +403,7 @@ func ListBlobsV2(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
data, err := json.Marshal(blobs)
|
data, err := json.Marshal(blobs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -399,12 +415,12 @@ func ListBlobsV2(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckBlob tests whether a blob exists.
|
// CheckBlob tests whether a blob exists.
|
||||||
func CheckBlob(w http.ResponseWriter, r *http.Request) {
|
func (c Config) CheckBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("CheckBlob()")
|
log.Println("CheckBlob()")
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
path, err := c.getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -412,7 +428,7 @@ func CheckBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
st, err := os.Stat(path)
|
st, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -423,12 +439,12 @@ func CheckBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBlob retrieves a blob from the repository.
|
// GetBlob retrieves a blob from the repository.
|
||||||
func GetBlob(w http.ResponseWriter, r *http.Request) {
|
func (c Config) GetBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("GetBlob()")
|
log.Println("GetBlob()")
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
path, err := c.getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -436,7 +452,7 @@ func GetBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
@ -451,20 +467,20 @@ func GetBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.Prometheus {
|
if c.Prometheus {
|
||||||
labels := getMetricLabels(r)
|
labels := c.getMetricLabels(r)
|
||||||
metricBlobReadTotal.With(labels).Inc()
|
metricBlobReadTotal.With(labels).Inc()
|
||||||
metricBlobReadBytesTotal.With(labels).Add(float64(wc.Count()))
|
metricBlobReadBytesTotal.With(labels).Add(float64(wc.Count()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveBlob saves a blob to the repository.
|
// SaveBlob saves a blob to the repository.
|
||||||
func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
func (c Config) SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("SaveBlob()")
|
log.Println("SaveBlob()")
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
path, err := c.getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -488,7 +504,7 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -499,7 +515,7 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = tf.Close()
|
_ = tf.Close()
|
||||||
_ = os.Remove(path)
|
_ = os.Remove(path)
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
@ -509,7 +525,7 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := tf.Sync(); err != nil {
|
if err := tf.Sync(); err != nil {
|
||||||
_ = tf.Close()
|
_ = tf.Close()
|
||||||
_ = os.Remove(path)
|
_ = os.Remove(path)
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -518,39 +534,39 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if err := tf.Close(); err != nil {
|
if err := tf.Close(); err != nil {
|
||||||
_ = os.Remove(path)
|
_ = os.Remove(path)
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.Prometheus {
|
if c.Prometheus {
|
||||||
labels := getMetricLabels(r)
|
labels := c.getMetricLabels(r)
|
||||||
metricBlobWriteTotal.With(labels).Inc()
|
metricBlobWriteTotal.With(labels).Inc()
|
||||||
metricBlobWriteBytesTotal.With(labels).Add(float64(written))
|
metricBlobWriteBytesTotal.With(labels).Add(float64(written))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteBlob deletes a blob from the repository.
|
// DeleteBlob deletes a blob from the repository.
|
||||||
func DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
func (c Config) DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("DeleteBlob()")
|
log.Println("DeleteBlob()")
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.AppendOnly && pat.Param(r, "type") != "locks" {
|
if c.AppendOnly && pat.Param(r, "type") != "locks" {
|
||||||
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
path, err := c.getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var size int64
|
var size int64
|
||||||
if Config.Prometheus {
|
if c.Prometheus {
|
||||||
stat, err := os.Stat(path)
|
stat, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
size = stat.Size()
|
size = stat.Size()
|
||||||
|
@ -558,7 +574,7 @@ func DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
@ -569,20 +585,20 @@ func DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.Prometheus {
|
if c.Prometheus {
|
||||||
labels := getMetricLabels(r)
|
labels := c.getMetricLabels(r)
|
||||||
metricBlobDeleteTotal.With(labels).Inc()
|
metricBlobDeleteTotal.With(labels).Inc()
|
||||||
metricBlobDeleteBytesTotal.With(labels).Add(float64(size))
|
metricBlobDeleteBytesTotal.With(labels).Add(float64(size))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateRepo creates repository directories.
|
// CreateRepo creates repository directories.
|
||||||
func CreateRepo(w http.ResponseWriter, r *http.Request) {
|
func (c Config) CreateRepo(w http.ResponseWriter, r *http.Request) {
|
||||||
if Config.Debug {
|
if c.Debug {
|
||||||
log.Println("CreateRepo()")
|
log.Println("CreateRepo()")
|
||||||
}
|
}
|
||||||
|
|
||||||
repo, err := join(Config.Path, getRepo(r))
|
repo, err := join(c.Path, c.getRepo(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
|
@ -229,11 +229,11 @@ func TestResticHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// globally set append-only mode and configure path
|
// set append-only mode and configure path
|
||||||
Config.AppendOnly = true
|
mux := NewHandler(Config{
|
||||||
Config.Path = tempdir
|
AppendOnly: true,
|
||||||
|
Path: tempdir,
|
||||||
mux := NewMux()
|
})
|
||||||
|
|
||||||
// create the repo
|
// create the repo
|
||||||
checkRequest(t, mux.ServeHTTP,
|
checkRequest(t, mux.ServeHTTP,
|
||||||
|
|
83
mux.go
83
mux.go
|
@ -12,28 +12,7 @@ import (
|
||||||
"goji.io/pat"
|
"goji.io/pat"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config struct holds program configuration.
|
func (c Config) debugHandler(next http.Handler) http.Handler {
|
||||||
var Config = struct {
|
|
||||||
Path string
|
|
||||||
Listen string
|
|
||||||
Log string
|
|
||||||
CPUProfile string
|
|
||||||
TLSKey string
|
|
||||||
TLSCert string
|
|
||||||
TLS bool
|
|
||||||
NoAuth bool
|
|
||||||
AppendOnly bool
|
|
||||||
PrivateRepos bool
|
|
||||||
Prometheus bool
|
|
||||||
Debug bool
|
|
||||||
Version bool
|
|
||||||
}{
|
|
||||||
Path: "/tmp/restic",
|
|
||||||
Listen: ":8000",
|
|
||||||
AppendOnly: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
func debugHandler(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("%s %s", r.Method, r.URL)
|
log.Printf("%s %s", r.Method, r.URL)
|
||||||
|
@ -41,8 +20,8 @@ func debugHandler(next http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func logHandler(next http.Handler) http.Handler {
|
func (c Config) logHandler(next http.Handler) http.Handler {
|
||||||
accessLog, err := os.OpenFile(Config.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
accessLog, err := os.OpenFile(c.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: %v", err)
|
log.Fatalf("error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -50,43 +29,43 @@ func logHandler(next http.Handler) http.Handler {
|
||||||
return handlers.CombinedLoggingHandler(accessLog, next)
|
return handlers.CombinedLoggingHandler(accessLog, next)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMux is master HTTP multiplexer/router.
|
// NewHandler returns the master HTTP multiplexer/router.
|
||||||
func NewMux() *goji.Mux {
|
func NewHandler(config Config) *goji.Mux {
|
||||||
mux := goji.NewMux()
|
mux := goji.NewMux()
|
||||||
|
|
||||||
if Config.Debug {
|
if config.Debug {
|
||||||
mux.Use(debugHandler)
|
mux.Use(config.debugHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.Log != "" {
|
if config.Log != "" {
|
||||||
mux.Use(logHandler)
|
mux.Use(config.logHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.Prometheus {
|
if config.Prometheus {
|
||||||
mux.Handle(pat.Get("/metrics"), promhttp.Handler())
|
mux.Handle(pat.Get("/metrics"), promhttp.Handler())
|
||||||
}
|
}
|
||||||
|
|
||||||
mux.HandleFunc(pat.Head("/config"), CheckConfig)
|
mux.HandleFunc(pat.Head("/config"), config.CheckConfig)
|
||||||
mux.HandleFunc(pat.Head("/:repo/config"), CheckConfig)
|
mux.HandleFunc(pat.Head("/:repo/config"), config.CheckConfig)
|
||||||
mux.HandleFunc(pat.Get("/config"), GetConfig)
|
mux.HandleFunc(pat.Get("/config"), config.GetConfig)
|
||||||
mux.HandleFunc(pat.Get("/:repo/config"), GetConfig)
|
mux.HandleFunc(pat.Get("/:repo/config"), config.GetConfig)
|
||||||
mux.HandleFunc(pat.Post("/config"), SaveConfig)
|
mux.HandleFunc(pat.Post("/config"), config.SaveConfig)
|
||||||
mux.HandleFunc(pat.Post("/:repo/config"), SaveConfig)
|
mux.HandleFunc(pat.Post("/:repo/config"), config.SaveConfig)
|
||||||
mux.HandleFunc(pat.Delete("/config"), DeleteConfig)
|
mux.HandleFunc(pat.Delete("/config"), config.DeleteConfig)
|
||||||
mux.HandleFunc(pat.Delete("/:repo/config"), DeleteConfig)
|
mux.HandleFunc(pat.Delete("/:repo/config"), config.DeleteConfig)
|
||||||
mux.HandleFunc(pat.Get("/:type/"), ListBlobs)
|
mux.HandleFunc(pat.Get("/:type/"), config.ListBlobs)
|
||||||
mux.HandleFunc(pat.Get("/:repo/:type/"), ListBlobs)
|
mux.HandleFunc(pat.Get("/:repo/:type/"), config.ListBlobs)
|
||||||
mux.HandleFunc(pat.Head("/:type/:name"), CheckBlob)
|
mux.HandleFunc(pat.Head("/:type/:name"), config.CheckBlob)
|
||||||
mux.HandleFunc(pat.Head("/:repo/:type/:name"), CheckBlob)
|
mux.HandleFunc(pat.Head("/:repo/:type/:name"), config.CheckBlob)
|
||||||
mux.HandleFunc(pat.Get("/:type/:name"), GetBlob)
|
mux.HandleFunc(pat.Get("/:type/:name"), config.GetBlob)
|
||||||
mux.HandleFunc(pat.Get("/:repo/:type/:name"), GetBlob)
|
mux.HandleFunc(pat.Get("/:repo/:type/:name"), config.GetBlob)
|
||||||
mux.HandleFunc(pat.Post("/:type/:name"), SaveBlob)
|
mux.HandleFunc(pat.Post("/:type/:name"), config.SaveBlob)
|
||||||
mux.HandleFunc(pat.Post("/:repo/:type/:name"), SaveBlob)
|
mux.HandleFunc(pat.Post("/:repo/:type/:name"), config.SaveBlob)
|
||||||
mux.HandleFunc(pat.Delete("/:type/:name"), DeleteBlob)
|
mux.HandleFunc(pat.Delete("/:type/:name"), config.DeleteBlob)
|
||||||
mux.HandleFunc(pat.Delete("/:repo/:type/:name"), DeleteBlob)
|
mux.HandleFunc(pat.Delete("/:repo/:type/:name"), config.DeleteBlob)
|
||||||
mux.HandleFunc(pat.Post("/"), CreateRepo)
|
mux.HandleFunc(pat.Post("/"), config.CreateRepo)
|
||||||
mux.HandleFunc(pat.Post("/:repo"), CreateRepo)
|
mux.HandleFunc(pat.Post("/:repo"), config.CreateRepo)
|
||||||
mux.HandleFunc(pat.Post("/:repo/"), CreateRepo)
|
mux.HandleFunc(pat.Post("/:repo/"), config.CreateRepo)
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue