Browse Source

Merge branch 'development'

Kieran Gibb 2 years ago
parent
commit
e0426472d9
No known key found for this signature in database

+ 1
- 1
README.md View File

@@ -83,7 +83,7 @@ cobox help
83 83
 | DELETE | /groups/:id/sync | groups/sync.js#destroy | unsync a group |
84 84
 | GET  | /groups/:id/files | groups/files.js#readdir | list a group's files |
85 85
 | GET  | /groups/:id/files/:filename | groups/files.js#readFile **OR** groups/files.js#createReadStream | download / get a file |
86
-| GET  | /settings | settings.js#index | list settings |
86
+| GET  | /settings | settings.js#index | list settings. Currently includes the public key needed for invite codes|
87 87
 | PATCH | /settings | settings.js#update | change settings e.g. name, mountdir, etc |
88 88
 | GET  | /groups/:id/drive | groups/drive.js#index | get drive state / history |
89 89
 | GET  | /admin/devices | admin/devices.js#index | list all coboxes devices we hold an admin passphrase for (get from `config.yml`)  |

+ 1
- 1
app/controllers/groups/mounts/index.js View File

@@ -10,7 +10,7 @@ const router = express.Router()
10 10
 
11 11
 router.post('/', [
12 12
   sanitize('location').customSanitizer((location, { req }) => {
13
-    if (!location) location = req.config.options.get('mount')
13
+    if (!location) location = req.mount
14 14
     let group = req.groups.current
15 15
     return path.join(location, group.name)
16 16
   })

+ 1
- 0
app/controllers/index.js View File

@@ -4,5 +4,6 @@ const router = express.Router()
4 4
 
5 5
 router.use('/groups', require('./groups'))
6 6
 router.use('/replicators', require('./replicators'))
7
+router.use('/settings', require('./settings'))
7 8
 
8 9
 module.exports = router

+ 14
- 0
app/controllers/settings/index.js View File

@@ -0,0 +1,14 @@
1
+const express = require('express')
2
+const debug = require('debug')('cobox-server')
3
+
4
+const router = express.Router()
5
+
6
+router.get('/', function settings (req, res) {
7
+  return res
8
+    .status(200)
9
+    .json({
10
+      publicKey: req.config.identity.publicKey.toString('hex')
11
+    })
12
+})
13
+
14
+module.exports = router

+ 1
- 1
app/middleware/get-group.js View File

@@ -9,7 +9,7 @@ const getGroup = module.exports = () => {
9 9
 
10 10
     res.status(422).json({
11 11
       errors: [{
12
-        msg: 'group does not exist'
12
+        msg: 'group does not exist, provide an address'
13 13
       }]
14 14
     })
15 15
   }

+ 17
- 0
index.js View File

@@ -8,11 +8,18 @@ const Store = require('cobox-group-store')
8 8
 const swaggerDocs = require('./docs/swagger.json')
9 9
 const Group = require('cobox-group')
10 10
 const BlindReplicator = require('cobox-blind-replicator')
11
+const mkdirp = require('mkdirp')
12
+const rimraf = require('rimraf')
11 13
 const path = require('path')
12 14
 
13 15
 function Application (config, opts = {}) {
16
+  console.log("stacking it up, block by block by...")
17
+
14 18
   const host = opts.host || 'localhost'
15 19
   const port = opts.port || 3000
20
+  const mount = opts.mount || constants.mount
21
+
22
+  mkdirp.sync(mount)
16 23
 
17 24
   const app = express()
18 25
 
@@ -40,6 +47,7 @@ function Application (config, opts = {}) {
40 47
 
41 48
   app.use(function configLoader (req, res, next) {
42 49
     req.config = config
50
+    req.mount = mount
43 51
     next()
44 52
   })
45 53
 
@@ -56,8 +64,17 @@ function Application (config, opts = {}) {
56 64
   app.use('/', require('./app/controllers'))
57 65
   app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocs))
58 66
 
67
+  process.on('SIGINT', () => {
68
+    console.log('\n...block by block by block, blocks all the way down')
69
+    rimraf(mount, (err) => {
70
+      if (err) console.error(new Error('failed to exit gracefully'))
71
+      process.exit(1)
72
+    })
73
+  })
74
+
59 75
   return app
60 76
 }
61 77
 
62 78
 module.exports = Application
63 79
 module.exports.Server = require('./server')
80
+module.exports.Start = require('./start')

+ 2
- 12
main.js View File

@@ -1,13 +1,3 @@
1
-const Config = require('cobox-config')
2
-const constants = require('cobox-constants')
3
-const App = require('./')
4
-const Server = require('./server')
5 1
 const yargs = require('yargs')
6
-
7
-const argv = yargs.argv
8
-const storage = argv.storage || constants.storage
9
-const config = Config(storage, { mount: argv.location })
10
-const location = config.options.get('mount')
11
-
12
-const app = App(config, argv)
13
-Server(app, { location, ...argv })
2
+const start = require('./start')
3
+start(yargs.argv)

+ 3
- 2
package.json View File

@@ -29,8 +29,10 @@
29 29
     "kappa-drive": "^1.1.0",
30 30
     "kappa-drive-mount": "^1.0.1",
31 31
     "os": "^0.1.1",
32
+    "rimraf": "^3.0.0",
32 33
     "swagger-ui-express": "^4.1.2",
33
-    "thunky": "^1.1.0"
34
+    "thunky": "^1.1.0",
35
+    "yargs": "^15.0.2"
34 36
   },
35 37
   "devDependencies": {
36 38
     "maybe": "^0.0.2",
@@ -39,7 +41,6 @@
39 41
     "nodemon": "^1.19.4",
40 42
     "random-access-memory": "^3.1.1",
41 43
     "random-words": "^1.1.0",
42
-    "rimraf": "^3.0.0",
43 44
     "tap-spec": "^5.0.0",
44 45
     "tape": "^4.11.0",
45 46
     "tape-plus": "^1.0.0",

+ 4
- 2
server.js View File

@@ -1,12 +1,14 @@
1 1
 const debug = require('./lib/debug')
2 2
 const coboxBanner = require('./cobox-banner')
3
+const constants = require('cobox-constants')
3 4
 
4 5
 module.exports = async function Server (app, opts = {}) {
5 6
   const host = opts.host || 'localhost'
6 7
   const port = opts.port || 3000
7
-  const location = opts.location
8
+  const mount = opts.mount || constants.mount
8 9
 
9 10
   app.listen(port)
10 11
   console.log(coboxBanner)
11
-  debug(`Listening on http://${host}:${port}`)
12
+  debug(`listening on http://${host}:${port}`)
13
+  debug(`mounting at file://${mount}`)
12 14
 }

+ 17
- 0
start.js View File

@@ -0,0 +1,17 @@
1
+const Config = require('cobox-config')
2
+const constants = require('cobox-constants')
3
+const path = require('path')
4
+
5
+const App = require('./')
6
+const Server = require('./server')
7
+
8
+module.exports = (opts) => {
9
+  const storage = opts.storage || constants.storage
10
+  const config = Config(storage, opts)
11
+
12
+  var mount = opts.mount || constants.mount
13
+  opts.mount = path.resolve(mount)
14
+
15
+  const app = App(config, opts)
16
+  Server(app, opts)
17
+}

+ 1
- 1
test/controllers/groups/connections/create.test.js View File

@@ -68,7 +68,7 @@ describe('POST /groups/:id/connections', (context) => {
68 68
       var data = response._getJSONData()
69 69
       let errors = data.errors
70 70
       assert.ok(Array.isArray(errors), 'returns an error')
71
-      assert.same(errors[0].msg, 'group does not exist', 'returns an error message')
71
+      assert.same(errors[0].msg, 'group does not exist, provide an address', 'returns an error message')
72 72
       cleanup(storage, next)
73 73
     })
74 74
   })

+ 1
- 1
test/controllers/groups/connections/destroy.test.js View File

@@ -67,7 +67,7 @@ describe('DELETE /groups/:id/connections', (context) => {
67 67
       var data = response._getJSONData()
68 68
       let errors = data.errors
69 69
       assert.ok(Array.isArray(errors), 'returns an error')
70
-      assert.same(errors[0].msg, 'group does not exist', 'returns an error message')
70
+      assert.same(errors[0].msg, 'group does not exist, provide an address', 'returns an error message')
71 71
       cleanup(storage, next)
72 72
     })
73 73
   })

+ 1
- 1
test/controllers/groups/drive/all.test.js View File

@@ -64,7 +64,7 @@ describe('GET /groups/:id/drive', (context) => {
64 64
       var data = response._getJSONData()
65 65
       let errors = data.errors
66 66
       assert.ok(Array.isArray(errors), 'returns an error')
67
-      assert.same(errors[0].msg, 'group does not exist', 'returns an error message')
67
+      assert.same(errors[0].msg, 'group does not exist, provide an address', 'returns an error message')
68 68
       cleanup(storage, next)
69 69
     })
70 70
   })

+ 1
- 1
test/controllers/groups/find.test.js View File

@@ -66,7 +66,7 @@ describe('GET /groups/:id', (context) => {
66 66
       var data = response._getJSONData()
67 67
       let errors = data.errors
68 68
       assert.ok(Array.isArray(errors), 'returns an error')
69
-      assert.same(errors[0].msg, 'group does not exist', 'returns an error message')
69
+      assert.same(errors[0].msg, 'group does not exist, provide an address', 'returns an error message')
70 70
       cleanup(storage, next)
71 71
     })
72 72
   })

+ 4
- 1
test/controllers/groups/mounts/create.test.js View File

@@ -36,6 +36,7 @@ describe('POST /groups/:id/mounts', (context) => {
36 36
       url: `/groups/${address}/mounts`,
37 37
       body: { address },
38 38
       groups,
39
+      mount: '/cobox',
39 40
       config
40 41
     })
41 42
 
@@ -58,6 +59,7 @@ describe('POST /groups/:id/mounts', (context) => {
58 59
       url: `/groups/${name}/mounts`,
59 60
       body: { name },
60 61
       groups,
62
+      mount: '/cobox',
61 63
       config
62 64
     })
63 65
 
@@ -81,6 +83,7 @@ describe('POST /groups/:id/mounts', (context) => {
81 83
       url: `/groups/${id}/mounts`,
82 84
       body: { id },
83 85
       groups,
86
+      mount: '/cobox',
84 87
       config
85 88
     })
86 89
 
@@ -90,7 +93,7 @@ describe('POST /groups/:id/mounts', (context) => {
90 93
       var data = response._getJSONData()
91 94
       let errors = data.errors
92 95
       assert.ok(Array.isArray(errors), 'returns an error')
93
-      assert.same(errors[0].msg, 'group does not exist', 'returns an error message')
96
+      assert.same(errors[0].msg, 'group does not exist, provide an address', 'returns an error message')
94 97
       cleanup(storage, next)
95 98
     })
96 99
   })

+ 1
- 1
test/controllers/groups/mounts/destroy.test.js View File

@@ -67,7 +67,7 @@ describe('DELETE /groups/:id/mounts', (context) => {
67 67
       var data = response._getJSONData()
68 68
       let errors = data.errors
69 69
       assert.ok(Array.isArray(errors), 'returns an error')
70
-      assert.same(errors[0].msg, 'group does not exist', 'returns an error message')
70
+      assert.same(errors[0].msg, 'group does not exist, provide an address', 'returns an error message')
71 71
       cleanup(storage, next)
72 72
     })
73 73
   })

+ 34
- 0
test/controllers/settings/all.test.js View File

@@ -0,0 +1,34 @@
1
+const { describe } = require('tape-plus')
2
+const httpMocks = require('node-mocks-http')
3
+const Config = require('cobox-config')
4
+
5
+const routeHandler = require('../../../app/controllers')
6
+const { tmp, cleanup } = require('../../util')
7
+
8
+describe('GET /settings', (context) => {
9
+  let storage, config, count
10
+  let request, response
11
+
12
+  context.beforeEach((c) => {
13
+    storage = tmp()
14
+    config = Config(storage)
15
+
16
+    request = httpMocks.createRequest({
17
+      method: 'GET',
18
+      url: '/settings',
19
+      config
20
+    })
21
+
22
+    response = httpMocks.createResponse({
23
+      eventEmitter: require('events').EventEmitter
24
+    })
25
+  })
26
+
27
+  context('valid with correct parameters', (assert, next) => {
28
+    routeHandler(request, response)
29
+    var settings = response._getJSONData()
30
+    assert.ok(settings, 'gives a response')
31
+    assert.ok(settings.publicKey, 'returns a public key')
32
+    cleanup(storage, next)
33
+  })
34
+})