summaryrefslogtreecommitdiff
path: root/system/api
diff options
context:
space:
mode:
authorSteve <nilslice@gmail.com>2017-01-24 10:35:18 -0800
committerGitHub <noreply@github.com>2017-01-24 10:35:18 -0800
commit3a897e4db97cc6f5e47f395662499402eb4c2bda (patch)
tree61350d459fb5cbf044e878042c0239acb060c7da /system/api
parent0cf0d36f7613bbb2e13c0c8406689de3be9ee8d5 (diff)
[core] System backups (uploads, system.db, analytics.db) (#42)
Diffstat (limited to 'system/api')
-rw-r--r--system/api/analytics/backup.go26
-rw-r--r--system/api/analytics/init.go4
2 files changed, 29 insertions, 1 deletions
diff --git a/system/api/analytics/backup.go b/system/api/analytics/backup.go
new file mode 100644
index 0000000..07b1a46
--- /dev/null
+++ b/system/api/analytics/backup.go
@@ -0,0 +1,26 @@
+package analytics
+
+import (
+ "fmt"
+ "net/http"
+ "time"
+
+ "github.com/boltdb/bolt"
+)
+
+// Backup writes a snapshot of the analytics.db database to an HTTP response
+func Backup(res http.ResponseWriter) error {
+ err := store.View(func(tx *bolt.Tx) error {
+ ts := time.Now().Unix()
+ disposition := `attachment; filename="analytics-%d.db.bak"`
+
+ res.Header().Set("Content-Type", "application/octet-stream")
+ res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, ts))
+ res.Header().Set("Content-Length", fmt.Sprintf("%d", int(tx.Size())))
+
+ _, err := tx.WriteTo(res)
+ return err
+ })
+
+ return err
+}
diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go
index 3eccc13..f24425b 100644
--- a/system/api/analytics/init.go
+++ b/system/api/analytics/init.go
@@ -43,13 +43,15 @@ const RANGE = 14
func Record(req *http.Request) {
external := strings.Contains(req.URL.Path, "/external/")
+ ts := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)
+
r := apiRequest{
URL: req.URL.String(),
Method: req.Method,
Origin: req.Header.Get("Origin"),
Proto: req.Proto,
RemoteAddr: req.RemoteAddr,
- Timestamp: time.Now().Unix() * 1000,
+ Timestamp: ts,
External: external,
}