-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.dart
125 lines (102 loc) · 3.3 KB
/
server.dart
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
import 'dart:io';
import 'package:redstone/server.dart' as app;
import 'package:redstone_mapper/plugin.dart';
import 'package:redstone_mapper_mongo/manager.dart';
import 'package:redstone_mapper_mongo/service.dart';
import 'package:mongo_dart/mongo_dart.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:redstone_angular_todo/client.dart';
// Setup application log
var logger = new Logger("todo");
@app.Group("/todos")
class Todo extends MongoDbService<Item>{
Todo() : super("items");
@app.Route('/list')
@Encode()
list() {
logger.info("List items");
return find().then((items) {
logger.info("Found ${items.length} item(s)");
return items;
});
}
@app.Route('/add', methods: const [app.POST])
@Encode()
add(@Decode() Item item) {
logger.info("Add new item");
var err = item.validate();
if (err != null) {
logger.info("Invalid item!");
throw new app.ErrorResponse(400, err);
}
// Add item to database
return insert(item).then((dbRes) {
logger.info("Mongodb: $dbRes");
return "ok";
}).catchError((e) {
logger.warning("Unable to insert new item: $e");
return "error";
});
}
@app.Route('/update', methods: const [app.PUT])
updateStatus(@Decode() Item item) {
// Parse item to make sure only objects of type "Item" is accepted
logger.info("Updating item ${item.id}");
// Update item in database
return update({"_id": ObjectId.parse(item.id)}, item).then((dbRes) {
logger.info("Mongodb: ${dbRes}");
return "ok";
}).catchError((e) {
logger.warning("Unable to update item: $e");
return "error";
});
}
@app.Route('/delete/:id', methods: const [app.DELETE])
delete(String id) {
logger.info("Deleting item $id");
// Remove item from database
return remove({"_id": ObjectId.parse(id)}).then((dbRes) {
logger.info("Mongodb: $dbRes");
return "ok";
}).catchError((e) {
logger.warning("Unable to update item: $e");
return "error";
});
}
}
// Support for CORS
@app.Interceptor(r'/.*')
handleResponseHeader() {
if (app.request.method == "OPTIONS") {
// Overwrite the current response and interrupt the chain.
app.response = new shelf.Response.ok(null, headers: _createCorsHeader());
app.chain.interrupt();
} else {
// Process the chain and wrap the response
app.chain.next(() => app.response.change(headers: _createCorsHeader()));
}
}
_createCorsHeader() => {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-type",
"Access-Control-Allow-Methods": "OPTIONS, HEAD, GET, POST, PUT, DELETE"
};
void main() {
// Server port assignment
var PORT = Platform.environment['PORT'];
var appPort = PORT != null ? int.parse(PORT) : 9090;
// Database endpoint assignment
var MONGODB_URI = Platform.environment['MONGODB_URI'];
var appDB = MONGODB_URI != null ? MONGODB_URI : "mongodb://localhost/todo";
// Set connection pool size
var poolSize = 3;
// Setup database connection manager
var dbManager = new MongoDbManager(appDB, poolSize: poolSize);
// Setup server log
app.setupConsoleLog();
// Install redstone_mapper
app.addPlugin(getMapperPlugin(dbManager));
// Start server
app.start(address: '127.0.0.1', port: appPort);
}