Skip to content

Commit 9dc1732

Browse files
committed
models + container management
1 parent 85352d6 commit 9dc1732

File tree

15 files changed

+197
-7
lines changed

15 files changed

+197
-7
lines changed

app.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
const express = require('express');
2+
const config = require('./config');
3+
const mongoose = require('mongoose');
24
const fileUpload = require('express-fileupload');
35
const createError = require('http-errors');
4-
const logger = require('morgan');
56
const requestLogger = require('./middleware/requestLogger');
67
const errorHandler = require('./middleware/errorHandler');
7-
8-
const rootRouter = require('./routes/root');
9-
const dockerRouter = require('./routes/docker');
8+
const registerRoutes = require('./routes');
109

1110
const app = express();
1211

13-
// app.use(logger('dev'));
12+
mongoose.connect(config.mongo.url, {useNewUrlParser: true});
13+
1414
app.use(express.json());
1515
app.use(fileUpload());
1616
app.use(requestLogger);
1717

18-
app.use('/', rootRouter);
19-
app.use('/docker', dockerRouter);
18+
registerRoutes(app);
2019

2120
// catch 404 and forward to error handler
2221
app.use(function (req, res, next) {

config/default.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"docker": {
33
"socketPath": "/var/run/docker.sock"
4+
},
5+
"mongo": {
6+
"url": "mongodb://localhost/docker-composer"
47
}
58
}

controllers/ContainerController.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const ContainerService = require('../services/ContainerService');
2+
const handler = require('../helpers/handler');
3+
const ValidationError = require('../errors/ValidationError');
4+
5+
class ContainerController {
6+
static async create(req, res) {
7+
const result = await ContainerService.dao().create(req.body);
8+
return res.json(result);
9+
}
10+
11+
static async update(req, res) {
12+
const id = retrieveId(req);
13+
const result = await ContainerService.dao().update(id, req.body);
14+
return res.json(result);
15+
}
16+
17+
static async get(req, res) {
18+
const id = retrieveId(req);
19+
const result = await ContainerService.dao().findOne(id);
20+
return res.json(result);
21+
}
22+
23+
static async getAll(req, res) {
24+
const result = await ContainerService.dao().findAll(req.body);
25+
return res.json(result);
26+
}
27+
28+
static async delete(req, res) {
29+
const id = retrieveId(req);
30+
const result = await ContainerService.dao().delete(id);
31+
return res.json(result);
32+
}
33+
}
34+
35+
function retrieveId(req) {
36+
const id = req.params.id;
37+
if (!id) {
38+
throw new ValidationError('id must be provided')
39+
}
40+
return id;
41+
}
42+
43+
module.exports = handler(ContainerController);

dao/ContainerDao.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Container = require('../models/Container');
2+
3+
class ContainerDao {
4+
static model() {
5+
return Container;
6+
}
7+
8+
static create(data) {
9+
return Container.create(data);
10+
}
11+
12+
static update(id, data) {
13+
return Container.findByIdAndUpdate(id, data, {new: true}).exec();
14+
}
15+
16+
static findOne(id) {
17+
return Container.findById(id).exec()
18+
}
19+
20+
static findAll(query = {}) {
21+
return Container.find(query).exec()
22+
}
23+
24+
static delete(id) {
25+
return Container.findByIdAndDelete(id).exec()
26+
}
27+
}
28+
29+
module.exports = ContainerDao;

models/Container.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {BaseSchema, mongoose, referenceField} = require('./index');
2+
3+
const ContainerSchema = new BaseSchema({
4+
name: String,
5+
image: String,
6+
ports: [Number],
7+
volumes: [referenceField('Volume')],
8+
networks: [referenceField('Network')]
9+
});
10+
11+
module.exports = mongoose.model('Container', ContainerSchema);

models/Network.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const {BaseSchema, mongoose} = require('./index');
2+
3+
4+
const DriverOption = new BaseSchema({
5+
name: String,
6+
value: String
7+
});
8+
9+
const NetworkSchema = new BaseSchema({
10+
name: String,
11+
driver: String,
12+
driverOptions: [DriverOption],
13+
});
14+
15+
module.exports = mongoose.model('Network', NetworkSchema);

models/Volume.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {BaseSchema, mongoose} = require('./index');
2+
3+
const DriverOption = new BaseSchema({
4+
name: String,
5+
value: String
6+
});
7+
8+
const VolumeSchema = new BaseSchema({
9+
name: String,
10+
driver: String,
11+
driverOptions: [DriverOption],
12+
});
13+
14+
module.exports = mongoose.model('Volume', VolumeSchema);

models/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mongoose = require('mongoose');
2+
const Schema = mongoose.Schema;
3+
4+
class BaseSchema extends Schema {
5+
constructor(def, opts = {}) {
6+
super(...arguments);
7+
this.set('toJSON', {
8+
transform: function (doc, ret, options) {
9+
// remove the _id of every document before returning the result
10+
ret.id = ret._id;
11+
delete ret._id;
12+
delete ret.__v;
13+
},
14+
virtuals: true
15+
});
16+
}
17+
}
18+
19+
function referenceField(model) {
20+
return {type: Schema.Types.ObjectId, ref: model}
21+
}
22+
23+
module.exports.BaseSchema = BaseSchema;
24+
25+
module.exports.mongoose = mongoose;
26+
27+
module.exports.referenceField = referenceField;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"express-fileupload": "^1.0.0",
1717
"http-errors": "~1.6.2",
1818
"mocha": "^5.2.0",
19+
"mongoose": "^5.3.9",
1920
"morgan": "~1.9.0",
2021
"nconf": "^0.10.0",
2122
"parser-yaml": "^0.1.1",

routes/container_route.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const router = require('express').Router();
2+
const ContainerController = require('../controllers/ContainerController');
3+
4+
router.route('/')
5+
.get(ContainerController.getAll)
6+
.post(ContainerController.create);
7+
8+
router.route('/:id')
9+
.get(ContainerController.get)
10+
.put(ContainerController.update)
11+
.delete(ContainerController.delete);
12+
13+
module.exports = router;

0 commit comments

Comments
 (0)