-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
105 lines (89 loc) · 3.51 KB
/
server.js
File metadata and controls
105 lines (89 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var port = process.env.PORT || 8080; // set our port
// DB CONN
// =============================================================================
var Sequelize = require('sequelize');
var sequelize = new Sequelize(null, null, null, {
dialect: 'sqlite',
pool: {
max: 5,
min: 0,
idle: 10000
},
// SQLite only
storage: 'data/asm.db'
});
// Now define our model
var Instruction = sequelize.define('instruction', {
timestamps: false,
freezeTableName: true,
platform: Sequelize.TEXT,
mnem: Sequelize.TEXT,
description: Sequelize.TEXT,
tableName: 'instructions'
})
// Remove the chaff we don't want
Instruction.removeAttribute('id');
Instruction.removeAttribute('timestamps');
Instruction.removeAttribute('freezeTableName');
Instruction.removeAttribute('tableName');
Instruction.removeAttribute('createdAt');
Instruction.removeAttribute('updatedAt');
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// List our platforms
router.get('/platforms', function(req, res) {
sequelize.query("SELECT DISTINCT platform from `instructions` ORDER BY platform", { type: sequelize.QueryTypes.SELECT}).then(function(platforms) {
if (platforms) {
platforms = platforms.map(function(platform){ return platform.platform; });
res.json(platforms);
} else {
res.send(401).body("No platforms found");
}
});
});
// List instructions for a given platform
router.get('/platforms/:platform', function(req, res) {
Instruction.findAll({ where: { platform: req.params.platform }, attributes: ['mnem']}).then(function(instructions) {
if (instructions) {
instructions = instructions.map(function(instruction){ return instruction.mnem; });
res.json(instructions);
} else {
res.send(401).body("No instructions found in platform");
}
});
});
// Find a set of instructions by 'search' in a given platform
router.get('/platforms/:platform/search/:terms', function(req, res) {
Instruction.findAll({ where: { platform: req.params.platform, mnem: { $like: '%' + req.params.terms + '%' }}, attributes: ['mnem']}).then(function(instructions) {
if (instructions) {
instructions = instructions.map(function(instruction){ return instruction.mnem; });
res.json(instructions);
} else {
res.send(401).body("No instructions found in platform");
}
});
});
// List a given mnem for a given platform
router.get('/platforms/:platform/:mnem', function(req, res) {
Instruction.findOne({ where: { platform: req.params.platform, mnem: req.params.mnem }}).then(function(instruction) {
if (instruction) {
res.json(instruction);
} else {
res.send(401).body("Could not find instruction");
}
});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('The awesomeness is located on port ' + port);