Merge pull request #2 from drasko/master

Config and routes directory structure + Docker file
This commit is contained in:
Nikola 2015-07-27 12:50:45 +02:00
commit 8979bb96db
10 changed files with 183 additions and 106 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
.git
node_modules
test

42
Dockerfile Normal file
View File

@ -0,0 +1,42 @@
###
# Mainflux Dockerfile
###
# Set the base image to Node, onbuild variant: https://registry.hub.docker.com/_/node/
FROM node:0.10-onbuild
# Maintained by Mainflux team
MAINTAINER Mainflux <docker@mainflux.com>
# Log info
RUN echo "Starting Mainflux server..."
###
# Installations
###
# Add Gulp globally
RUN npm install -g gulp
# Gulp also demands to be saved locally
RUN npm install --save-dev gulp
# Finally, install all project Node modules
RUN npm install
###
# Setup the port
###
# Run Mainflux on port 80
ENV PORT 80
# Expose port on which we run Mainflux
EXPOSE $PORT
###
# Run main command from entrypoint and parameters in CMD[]
###
# Default port to execute the entrypoint (MongoDB)
CMD [""]
# Set default container command
ENTRYPOINT gulp

View File

@ -1,17 +1,28 @@
# Mainflux
Mainflux is an open source MIT licensed IoT cloud written in NodeJS
# Run
## Run
## Install Node Modules
### Install Node Modules
```bash
npm install
```
## Run Gulp Task
### Run Gulp Task
```bash
gulp
```
## Docker
### Build image
```bash
sudo docker build -t=mainflux .
```
## Run image
```bash
sudo docker run -i -t -d -p 8080:8080 --name=mainflux mainflux`
```
## License
MIT

15
app/routes/status.js Normal file
View File

@ -0,0 +1,15 @@
var express = require('express');
var router = express.Router(); // get an instance of the express Router
// on routes that end in /things
// ----------------------------------------------------
router.route('/')
// get the status (accessed at GET http://localhost:8080/status)
.get(function(req, res) {
var stat = {"status":"OK"}
res.send(stat);
});
// export router module
module.exports = router;

85
app/routes/things.js Normal file
View File

@ -0,0 +1,85 @@
var express = require('express');
var router = express.Router(); // get an instance of the express Router
var Thing = require('../models/thing');
// on routes that end in /things
// ----------------------------------------------------
router.route('/')
// create a things (accessed at POST http://localhost:8080/things)
.post(function(req, res) {
var thing = new Thing(); // create a new instance of the Bear model
thing.name = req.body.name; // set the thing's name (comes from the request)
// save the thing and check for errors
thing.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Thing created!' });
});
})
// get all the things (accessed at GET http://localhost:8080/things)
.get(function(req, res) {
Thing.find(function(err, things) {
if (err)
res.send(err);
res.json(things);
});
});
// on routes that end in /things/:thing_id
// ----------------------------------------------------
router.route('/:thing_id')
// get the thing with that id (accessed at GET http://localhost:8080/things/:thing_id)
.get(function(req, res) {
Thing.findById(req.params.thing_id, function(err, thing) {
if (err)
res.send(err);
res.json(thing);
});
})
// update the thing with this id (accessed at PUT http://localhost:8080/things/:thing_id)
.put(function(req, res) {
// use our thing model to find the thing we want
Thing.findById(req.params.thing_id, function(err, thing) {
if (err)
res.send(err);
thing.name = req.body.name; // update the things info
// save the thing
thing.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Thing updated!' });
});
})
})
// delete the thing with this id (accessed at DELETE http://localhost:8080/things/:thing_id)
.delete(function(req, res) {
Thing.remove({
_id: req.params.thing_id
}, function(err, thing) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// export router module
module.exports = router;

4
config/config.js Normal file
View File

@ -0,0 +1,4 @@
/*
* Following recipe here: http://dailyjs.com/2014/01/02/recipe-for-express-configuration/
*/
module.exports = require('./' + (process.env.NODE_ENV || 'development') + '.json');

9
config/development.json Normal file
View File

@ -0,0 +1,9 @@
{
"message" : "We are in development",
"db" : {
"path" : "mongodb://localhost",
"port" : "27017",
"name" : "test"
},
"port" : "8080"
}

0
config/production.json Normal file
View File

0
config/test.json Normal file
View File

114
server.js
View File

@ -1,5 +1,12 @@
// server.js
/**
* Extrenal configs are kept in the config.js file on the same level
*/
var config = require('./config/config');
console.log(config.message);
// BASE SETUP
// =============================================================================
@ -10,120 +17,21 @@ var bodyParser = require('body-parser');
// MongoDB
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mainflux'); // connect to our database
mongoose.connect(config.db.path + ':' + config.db.port + '/' + config.db.name); // connect to our database
var Thing = require('./app/models/thing');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
var port = process.env.PORT || config.port; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// on routes that end in /things
// ----------------------------------------------------
router.route('/things')
// create a things (accessed at POST http://localhost:8080/api/things)
.post(function(req, res) {
var thing = new Thing(); // create a new instance of the Bear model
thing.name = req.body.name; // set the thing's name (comes from the request)
// save the thing and check for errors
thing.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Thing created!' });
});
})
// get all the things (accessed at GET http://localhost:8080/api/things)
.get(function(req, res) {
Thing.find(function(err, things) {
if (err)
res.send(err);
res.json(things);
});
});
// on routes that end in /things/:thing_id
// ----------------------------------------------------
router.route('/things/:thing_id')
// get the thing with that id (accessed at GET http://localhost:8080/api/things/:thing_id)
.get(function(req, res) {
Thing.findById(req.params.thing_id, function(err, thing) {
if (err)
res.send(err);
res.json(thing);
});
})
// update the thing with this id (accessed at PUT http://localhost:8080/api/things/:thing_id)
.put(function(req, res) {
// use our thing model to find the thing we want
Thing.findById(req.params.thing_id, function(err, thing) {
if (err)
res.send(err);
thing.name = req.body.name; // update the things info
// save the thing
thing.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Thing updated!' });
});
})
})
// delete the thing with this id (accessed at DELETE http://localhost:8080/api/things/:thing_id)
.delete(function(req, res) {
Thing.remove({
_id: req.params.thing_id
}, function(err, thing) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
app.use('/status', require('./app/routes/status'));
app.use('/things', require('./app/routes/things'));
// START THE SERVER