-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
142 lines (119 loc) · 3.27 KB
/
app.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*jshint laxcomma: true, node: true*/
'use strict'
var hapi = require("hapi")
, fs = require('fs')
, path = require('path')
, Api = require('../lib/api')
, _Resource = require('../lib/resource')
, Cache = require('../lib/cache')
, fields = require('../lib/fields')
, server
, port
, v1
;
port = process.env.PORT || 3000
server = new hapi.Server()
server.connection({
port:port,
host:'0.0.0.0',
labels:['main']
})
var Base = _Resource.extend({
options:{
pk:'guid'
,cache: new Cache({engine:'catbox-memory'})
,allowed:{
upload:{
get:true,
post:true
}
}
}
,fields:{
age : { type:'int' }
, fake : new fields.ApiField({readonly:true})
, name : {type:'char'}
, fullname : {type:'char'}
, city : {type:'char', attribute:'company.address.city'}
, date : {type:'datetime', attribute:'registered'}
, location : {type:'field', readonly: true}
, file : new fields.FileField({create:true, root:path.resolve(__dirname), exclude: true})
}
, constructor: function( opts ){
this.parent('constructor', opts )
}
, dehydrate: function( obj ){
return obj;
}
, dehydrate_fullname: function( obj ){
return obj.gender + " " + obj.company.address.state;
}
, dehydrate_location: ( obj /*, bundle */ ) => {
return [ obj.longitude, obj.latitude ];
}
, dehydrate_fake: ( obj, bundle ) => {
return bundle.req.method + " " + obj.index;
}
, dispatch_upload: function(req, reply ) {
return this.dispatch('upload', this.bundle( req, reply ) );
}
, get_objects: (bundle, callback) => {
fs.readFile( path.join(__dirname, 'data.json') , callback);
}
, get_object: function( bundle, callback ) {
this.get_objects(bundle,function(e, objects){
var obj = JSON.parse( objects ).filter(function( obj ){
return obj.guid === bundle.req.params.pk;
})[0];
callback( null, obj );
})
}
, post_list: function( bundle ){
var format = this.format( bundle );
const that = this;
this.deserialize(bundle.req.payload, format, (err, data) => {
bundle.obj = Object.create(that.options.template.prototype)
bundle.data = data
that.full_dehydrate(data, bundle, (err, b) => {
console.log(b)
return bundle.res("done").code(201);
})
})
}
// Results should be sent using multipart/form-data
, post_upload: function( bundle ){
var format = this.format( bundle );
this.deserialize( bundle.req.payload, format, ( err, data ) => {
bundle.data = data;
bundle.object = {company:{address:{}}};
this.fields.file.hydrate( bundle, ( err, value ) => {
// quick and dirty respnose
bundle.data = {file: value}
this.respond( bundle )
});
});
}
, prepend_urls: function() {
return [{
path: '/api/v1/data/upload'
, handler: this.dispatch_upload.bind( this )
, name:'upload'
, config:{
payload:{
output:'stream'
,maxBytes: 3 + Math.pow(1024, 2)
,parse: true
}
}
}]
}
});
// Api setup
v1 = new Api('api/v1');
v1.use('data', new Base )
server.register( [v1, require('vision'), require('inert'), require('tv')], function(){
server.start(function(){
console.log(`server running at http://${server.info.host}:${server.info.port}` );
})
});
module.exports = Base;