2017-06-21 15:23:33 -06:00
|
|
|
package restserver
|
2015-09-18 17:13:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-07-30 14:30:18 +02:00
|
|
|
"errors"
|
2016-11-05 17:18:42 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-18 17:13:38 +02:00
|
|
|
"io/ioutil"
|
2016-12-27 01:35:45 +01:00
|
|
|
"log"
|
2015-09-18 17:13:38 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-07-30 14:30:18 +02:00
|
|
|
"path"
|
2015-09-18 17:13:38 +02:00
|
|
|
"path/filepath"
|
2016-12-28 00:57:25 +01:00
|
|
|
"strings"
|
2015-09-18 17:13:38 +02:00
|
|
|
"time"
|
2017-10-25 12:32:25 +08:00
|
|
|
|
Add Prometheus metrics
Exposes a few metrics for Prometheus under /metrics if started with --prometheus.
Example:
# HELP rest_server_blob_read_bytes_total Total number of bytes read from blobs
# TYPE rest_server_blob_read_bytes_total counter
rest_server_blob_read_bytes_total{repo="test",type="data"} 2.13557024e+09
rest_server_blob_read_bytes_total{repo="test",type="index"} 1.198653e+06
rest_server_blob_read_bytes_total{repo="test",type="keys"} 5388
rest_server_blob_read_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_read_bytes_total{repo="test",type="snapshots"} 10018
# HELP rest_server_blob_read_total Total number of blobs read
# TYPE rest_server_blob_read_total counter
rest_server_blob_read_total{repo="test",type="data"} 3985
rest_server_blob_read_total{repo="test",type="index"} 21
rest_server_blob_read_total{repo="test",type="keys"} 12
rest_server_blob_read_total{repo="test",type="locks"} 12
rest_server_blob_read_total{repo="test",type="snapshots"} 32
# HELP rest_server_blob_write_bytes_total Total number of bytes written to blobs
# TYPE rest_server_blob_write_bytes_total counter
rest_server_blob_write_bytes_total{repo="test",type="data"} 1.063726179e+09
rest_server_blob_write_bytes_total{repo="test",type="index"} 395586
rest_server_blob_write_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_write_bytes_total{repo="test",type="snapshots"} 1933
# HELP rest_server_blob_write_total Total number of blobs written
# TYPE rest_server_blob_write_total counter
rest_server_blob_write_total{repo="test",type="data"} 226
rest_server_blob_write_total{repo="test",type="index"} 6
rest_server_blob_write_total{repo="test",type="locks"} 12
rest_server_blob_write_total{repo="test",type="snapshots"} 6
2017-10-24 23:03:50 +08:00
|
|
|
"github.com/miolini/datacounter"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-10-25 12:32:25 +08:00
|
|
|
"goji.io/middleware"
|
|
|
|
"goji.io/pat"
|
2015-09-18 17:13:38 +02:00
|
|
|
)
|
|
|
|
|
2016-11-11 01:29:55 +01:00
|
|
|
func isHashed(dir string) bool {
|
|
|
|
return dir == "data"
|
|
|
|
}
|
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
func valid(name string) bool {
|
|
|
|
// taken from net/http.Dir
|
|
|
|
if strings.Contains(name, "\x00") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var validTypes = []string{"data", "index", "keys", "locks", "snapshots", "config"}
|
|
|
|
|
|
|
|
func isValidType(name string) bool {
|
|
|
|
for _, tpe := range validTypes {
|
|
|
|
if name == tpe {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// join takes a number of path names, sanitizes them, and returns them joined
|
|
|
|
// with base for the current operating system to use (dirs separated by
|
|
|
|
// filepath.Separator). The returned path is always either equal to base or a
|
|
|
|
// subdir of base.
|
|
|
|
func join(base string, names ...string) (string, error) {
|
|
|
|
clean := make([]string, 0, len(names)+1)
|
|
|
|
clean = append(clean, base)
|
|
|
|
|
|
|
|
// taken from net/http.Dir
|
|
|
|
for _, name := range names {
|
|
|
|
if !valid(name) {
|
|
|
|
return "", errors.New("invalid character in path")
|
|
|
|
}
|
|
|
|
|
|
|
|
clean = append(clean, filepath.FromSlash(path.Clean("/"+name)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(clean...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRepo returns the repository location, relative to Config.Path.
|
2016-12-28 00:57:25 +01:00
|
|
|
func getRepo(r *http.Request) string {
|
2017-01-25 20:59:18 +01:00
|
|
|
if strings.HasPrefix(fmt.Sprintf("%s", middleware.Pattern(r.Context())), "/:repo") {
|
2017-07-30 14:30:18 +02:00
|
|
|
return pat.Param(r, "repo")
|
2016-12-28 00:57:25 +01:00
|
|
|
}
|
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
return "."
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPath returns the path for a file type in the repo.
|
|
|
|
func getPath(r *http.Request, fileType string) (string, error) {
|
|
|
|
if !isValidType(fileType) {
|
|
|
|
return "", errors.New("invalid file type")
|
|
|
|
}
|
|
|
|
return join(Config.Path, getRepo(r), fileType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFilePath returns the path for a file in the repo.
|
|
|
|
func getFilePath(r *http.Request, fileType, name string) (string, error) {
|
|
|
|
if !isValidType(fileType) {
|
|
|
|
return "", errors.New("invalid file type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if isHashed(fileType) {
|
|
|
|
if len(name) < 2 {
|
|
|
|
return "", errors.New("file name is too short")
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(Config.Path, getRepo(r), fileType, name[:2], name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(Config.Path, getRepo(r), fileType, name)
|
2016-12-28 00:57:25 +01:00
|
|
|
}
|
|
|
|
|
2016-11-06 11:15:33 +01:00
|
|
|
// AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
|
|
|
|
// stored in f and returns the http.HandlerFunc.
|
2015-09-19 14:52:50 +02:00
|
|
|
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2016-11-06 18:59:19 +01:00
|
|
|
if username, password, ok := r.BasicAuth(); !ok || !f.Validate(username, password) {
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
2015-09-19 14:52:50 +02:00
|
|
|
return
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2015-09-19 14:52:50 +02:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// CheckConfig checks whether a configuration exists.
|
|
|
|
func CheckConfig(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("CheckConfig()")
|
|
|
|
}
|
2017-07-30 14:30:18 +02:00
|
|
|
cfg, err := getPath(r, "config")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-16 23:00:46 +01:00
|
|
|
st, err := os.Stat(cfg)
|
2016-12-27 13:42:43 +01:00
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// GetConfig allows for a config to be retrieved.
|
|
|
|
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("GetConfig()")
|
|
|
|
}
|
2017-07-30 14:30:18 +02:00
|
|
|
cfg, err := getPath(r, "config")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-05-01 20:02:27 +02:00
|
|
|
|
2017-01-16 23:00:46 +01:00
|
|
|
bytes, err := ioutil.ReadFile(cfg)
|
2016-12-27 13:42:43 +01:00
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Write(bytes)
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// SaveConfig allows for a config to be saved.
|
|
|
|
func SaveConfig(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("SaveConfig()")
|
|
|
|
}
|
2017-07-30 14:30:18 +02:00
|
|
|
cfg, err := getPath(r, "config")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-05-01 20:02:27 +02:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
bytes, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2017-05-01 20:01:52 +02:00
|
|
|
|
2017-01-16 23:00:46 +01:00
|
|
|
if err := ioutil.WriteFile(cfg, bytes, 0600); err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
// DeleteConfig removes a config.
|
|
|
|
func DeleteConfig(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-03-18 13:11:29 +01:00
|
|
|
log.Println("DeleteConfig()")
|
|
|
|
}
|
2017-09-02 20:16:21 -05:00
|
|
|
|
|
|
|
if Config.AppendOnly {
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
cfg, err := getPath(r, "config")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-03-18 13:11:29 +01:00
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
if err := os.Remove(cfg); err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-05-01 20:01:52 +02:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2017-03-18 13:11:29 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// ListBlobs lists all blobs of a given type in an arbitrary order.
|
|
|
|
func ListBlobs(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("ListBlobs()")
|
|
|
|
}
|
2017-07-30 14:30:18 +02:00
|
|
|
fileType := pat.Param(r, "type")
|
|
|
|
path, err := getPath(r, fileType)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
|
|
|
|
items, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
var names []string
|
|
|
|
for _, i := range items {
|
2017-07-30 14:30:18 +02:00
|
|
|
if isHashed(fileType) {
|
2016-12-27 13:42:43 +01:00
|
|
|
subpath := filepath.Join(path, i.Name())
|
|
|
|
subitems, err := ioutil.ReadDir(subpath)
|
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2016-11-06 14:02:43 +01:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
for _, f := range subitems {
|
|
|
|
names = append(names, f.Name())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
names = append(names, i.Name())
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
data, err := json.Marshal(names)
|
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Write(data)
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-28 19:51:25 +01:00
|
|
|
// CheckBlob tests whether a blob exists.
|
2016-12-27 13:42:43 +01:00
|
|
|
func CheckBlob(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("CheckBlob()")
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
st, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// GetBlob retrieves a blob from the repository.
|
|
|
|
func GetBlob(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("GetBlob()")
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
|
Add Prometheus metrics
Exposes a few metrics for Prometheus under /metrics if started with --prometheus.
Example:
# HELP rest_server_blob_read_bytes_total Total number of bytes read from blobs
# TYPE rest_server_blob_read_bytes_total counter
rest_server_blob_read_bytes_total{repo="test",type="data"} 2.13557024e+09
rest_server_blob_read_bytes_total{repo="test",type="index"} 1.198653e+06
rest_server_blob_read_bytes_total{repo="test",type="keys"} 5388
rest_server_blob_read_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_read_bytes_total{repo="test",type="snapshots"} 10018
# HELP rest_server_blob_read_total Total number of blobs read
# TYPE rest_server_blob_read_total counter
rest_server_blob_read_total{repo="test",type="data"} 3985
rest_server_blob_read_total{repo="test",type="index"} 21
rest_server_blob_read_total{repo="test",type="keys"} 12
rest_server_blob_read_total{repo="test",type="locks"} 12
rest_server_blob_read_total{repo="test",type="snapshots"} 32
# HELP rest_server_blob_write_bytes_total Total number of bytes written to blobs
# TYPE rest_server_blob_write_bytes_total counter
rest_server_blob_write_bytes_total{repo="test",type="data"} 1.063726179e+09
rest_server_blob_write_bytes_total{repo="test",type="index"} 395586
rest_server_blob_write_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_write_bytes_total{repo="test",type="snapshots"} 1933
# HELP rest_server_blob_write_total Total number of blobs written
# TYPE rest_server_blob_write_total counter
rest_server_blob_write_total{repo="test",type="data"} 226
rest_server_blob_write_total{repo="test",type="index"} 6
rest_server_blob_write_total{repo="test",type="locks"} 12
rest_server_blob_write_total{repo="test",type="snapshots"} 6
2017-10-24 23:03:50 +08:00
|
|
|
wc := datacounter.NewResponseWriterCounter(w)
|
|
|
|
http.ServeContent(wc, r, "", time.Unix(0, 0), file)
|
2016-12-27 13:42:43 +01:00
|
|
|
file.Close()
|
Add Prometheus metrics
Exposes a few metrics for Prometheus under /metrics if started with --prometheus.
Example:
# HELP rest_server_blob_read_bytes_total Total number of bytes read from blobs
# TYPE rest_server_blob_read_bytes_total counter
rest_server_blob_read_bytes_total{repo="test",type="data"} 2.13557024e+09
rest_server_blob_read_bytes_total{repo="test",type="index"} 1.198653e+06
rest_server_blob_read_bytes_total{repo="test",type="keys"} 5388
rest_server_blob_read_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_read_bytes_total{repo="test",type="snapshots"} 10018
# HELP rest_server_blob_read_total Total number of blobs read
# TYPE rest_server_blob_read_total counter
rest_server_blob_read_total{repo="test",type="data"} 3985
rest_server_blob_read_total{repo="test",type="index"} 21
rest_server_blob_read_total{repo="test",type="keys"} 12
rest_server_blob_read_total{repo="test",type="locks"} 12
rest_server_blob_read_total{repo="test",type="snapshots"} 32
# HELP rest_server_blob_write_bytes_total Total number of bytes written to blobs
# TYPE rest_server_blob_write_bytes_total counter
rest_server_blob_write_bytes_total{repo="test",type="data"} 1.063726179e+09
rest_server_blob_write_bytes_total{repo="test",type="index"} 395586
rest_server_blob_write_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_write_bytes_total{repo="test",type="snapshots"} 1933
# HELP rest_server_blob_write_total Total number of blobs written
# TYPE rest_server_blob_write_total counter
rest_server_blob_write_total{repo="test",type="data"} 226
rest_server_blob_write_total{repo="test",type="index"} 6
rest_server_blob_write_total{repo="test",type="locks"} 12
rest_server_blob_write_total{repo="test",type="snapshots"} 6
2017-10-24 23:03:50 +08:00
|
|
|
|
|
|
|
if Config.Prometheus {
|
|
|
|
labels := prometheus.Labels{"repo": getRepo(r), "type": pat.Param(r, "type")}
|
|
|
|
metricBlobReadTotal.With(labels).Inc()
|
|
|
|
metricBlobReadBytesTotal.With(labels).Add(float64(wc.Count()))
|
|
|
|
}
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// SaveBlob saves a blob to the repository.
|
|
|
|
func SaveBlob(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("SaveBlob()")
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2017-03-18 13:11:29 +01:00
|
|
|
}
|
2016-11-06 15:46:11 +01:00
|
|
|
|
2017-03-18 13:11:29 +01:00
|
|
|
tf, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
|
2016-12-27 13:42:43 +01:00
|
|
|
if err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2016-11-06 15:46:11 +01:00
|
|
|
|
Add Prometheus metrics
Exposes a few metrics for Prometheus under /metrics if started with --prometheus.
Example:
# HELP rest_server_blob_read_bytes_total Total number of bytes read from blobs
# TYPE rest_server_blob_read_bytes_total counter
rest_server_blob_read_bytes_total{repo="test",type="data"} 2.13557024e+09
rest_server_blob_read_bytes_total{repo="test",type="index"} 1.198653e+06
rest_server_blob_read_bytes_total{repo="test",type="keys"} 5388
rest_server_blob_read_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_read_bytes_total{repo="test",type="snapshots"} 10018
# HELP rest_server_blob_read_total Total number of blobs read
# TYPE rest_server_blob_read_total counter
rest_server_blob_read_total{repo="test",type="data"} 3985
rest_server_blob_read_total{repo="test",type="index"} 21
rest_server_blob_read_total{repo="test",type="keys"} 12
rest_server_blob_read_total{repo="test",type="locks"} 12
rest_server_blob_read_total{repo="test",type="snapshots"} 32
# HELP rest_server_blob_write_bytes_total Total number of bytes written to blobs
# TYPE rest_server_blob_write_bytes_total counter
rest_server_blob_write_bytes_total{repo="test",type="data"} 1.063726179e+09
rest_server_blob_write_bytes_total{repo="test",type="index"} 395586
rest_server_blob_write_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_write_bytes_total{repo="test",type="snapshots"} 1933
# HELP rest_server_blob_write_total Total number of blobs written
# TYPE rest_server_blob_write_total counter
rest_server_blob_write_total{repo="test",type="data"} 226
rest_server_blob_write_total{repo="test",type="index"} 6
rest_server_blob_write_total{repo="test",type="locks"} 12
rest_server_blob_write_total{repo="test",type="snapshots"} 6
2017-10-24 23:03:50 +08:00
|
|
|
written, err := io.Copy(tf, r.Body)
|
|
|
|
if err != nil {
|
2016-12-27 13:42:43 +01:00
|
|
|
tf.Close()
|
2017-03-18 13:11:29 +01:00
|
|
|
os.Remove(path)
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2017-05-01 20:01:52 +02:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
if err := tf.Sync(); err != nil {
|
|
|
|
tf.Close()
|
2017-03-18 13:11:29 +01:00
|
|
|
os.Remove(path)
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
|
|
|
}
|
2017-05-01 20:01:52 +02:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
if err := tf.Close(); err != nil {
|
|
|
|
os.Remove(path)
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-16 23:39:56 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2016-12-28 19:51:25 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2016-12-27 13:42:43 +01:00
|
|
|
return
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
Add Prometheus metrics
Exposes a few metrics for Prometheus under /metrics if started with --prometheus.
Example:
# HELP rest_server_blob_read_bytes_total Total number of bytes read from blobs
# TYPE rest_server_blob_read_bytes_total counter
rest_server_blob_read_bytes_total{repo="test",type="data"} 2.13557024e+09
rest_server_blob_read_bytes_total{repo="test",type="index"} 1.198653e+06
rest_server_blob_read_bytes_total{repo="test",type="keys"} 5388
rest_server_blob_read_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_read_bytes_total{repo="test",type="snapshots"} 10018
# HELP rest_server_blob_read_total Total number of blobs read
# TYPE rest_server_blob_read_total counter
rest_server_blob_read_total{repo="test",type="data"} 3985
rest_server_blob_read_total{repo="test",type="index"} 21
rest_server_blob_read_total{repo="test",type="keys"} 12
rest_server_blob_read_total{repo="test",type="locks"} 12
rest_server_blob_read_total{repo="test",type="snapshots"} 32
# HELP rest_server_blob_write_bytes_total Total number of bytes written to blobs
# TYPE rest_server_blob_write_bytes_total counter
rest_server_blob_write_bytes_total{repo="test",type="data"} 1.063726179e+09
rest_server_blob_write_bytes_total{repo="test",type="index"} 395586
rest_server_blob_write_bytes_total{repo="test",type="locks"} 1975
rest_server_blob_write_bytes_total{repo="test",type="snapshots"} 1933
# HELP rest_server_blob_write_total Total number of blobs written
# TYPE rest_server_blob_write_total counter
rest_server_blob_write_total{repo="test",type="data"} 226
rest_server_blob_write_total{repo="test",type="index"} 6
rest_server_blob_write_total{repo="test",type="locks"} 12
rest_server_blob_write_total{repo="test",type="snapshots"} 6
2017-10-24 23:03:50 +08:00
|
|
|
|
|
|
|
if Config.Prometheus {
|
|
|
|
labels := prometheus.Labels{"repo": getRepo(r), "type": pat.Param(r, "type")}
|
|
|
|
metricBlobWriteTotal.With(labels).Inc()
|
|
|
|
metricBlobWriteBytesTotal.With(labels).Add(float64(written))
|
|
|
|
}
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2016-12-27 13:42:43 +01:00
|
|
|
// DeleteBlob deletes a blob from the repository.
|
|
|
|
func DeleteBlob(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2016-12-27 13:42:43 +01:00
|
|
|
log.Println("DeleteBlob()")
|
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-09-02 20:16:21 -05:00
|
|
|
if Config.AppendOnly && pat.Param(r, "type") != "locks" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
path, err := getFilePath(r, pat.Param(r, "type"), pat.Param(r, "name"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2016-12-27 13:42:43 +01:00
|
|
|
}
|
2016-11-06 14:02:43 +01:00
|
|
|
|
2017-05-01 20:01:52 +02:00
|
|
|
if err := os.Remove(path); err != nil {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-05-01 20:01:52 +02:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2017-03-18 13:11:29 +01:00
|
|
|
return
|
|
|
|
}
|
2015-09-18 17:13:38 +02:00
|
|
|
}
|
2017-01-25 20:59:18 +01:00
|
|
|
|
|
|
|
// CreateRepo creates repository directories.
|
|
|
|
func CreateRepo(w http.ResponseWriter, r *http.Request) {
|
2017-06-21 15:23:33 -06:00
|
|
|
if Config.Debug {
|
2017-01-25 20:59:18 +01:00
|
|
|
log.Println("CreateRepo()")
|
|
|
|
}
|
2017-07-30 14:30:18 +02:00
|
|
|
|
|
|
|
repo, err := join(Config.Path, getRepo(r))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-01-25 20:59:18 +01:00
|
|
|
|
|
|
|
if r.URL.Query().Get("create") != "true" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-01 20:10:46 +02:00
|
|
|
log.Printf("Creating repository directories in %s\n", repo)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(repo, 0700); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-30 14:30:18 +02:00
|
|
|
for _, d := range validTypes {
|
|
|
|
if d == "config" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-01 20:10:46 +02:00
|
|
|
if err := os.MkdirAll(filepath.Join(repo, d), 0700); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
if err := os.MkdirAll(filepath.Join(repo, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 20:59:18 +01:00
|
|
|
}
|