-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapp.dart
150 lines (118 loc) · 3.7 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import 'dart:io';
import 'package:json/json.dart' as JSON;
import 'package:dirty/dirty.dart';
import 'package:uuid/uuid_server.dart';
import 'package:ansicolor/ansicolor.dart';
main() {
var port = Platform.environment['PORT'] == null ?
8000 : int.parse(Platform.environment['PORT']);
HttpServer.bind('127.0.0.1', port).then((app) {
//app.listen(Public.matcher, Public.handler);
app.listen((req) {
log(req);
if (req.method == 'GET' && req.uri.path == '/comics') {
return Comics.index(req);
}
if (req.method == 'POST' && req.uri.path == '/comics') {
return Comics.post(req);
}
if (req.method == 'DELETE' &&
new RegExp(r"^/comics/[-\w\d]+$").hasMatch(req.uri.path)) {
return Comics.delete(req);
}
if (Public.matcher(req)) {
return Public.handler(req);
}
HttpResponse res = req.response;
res.statusCode = HttpStatus.NOT_FOUND;
res.write("Not found.");
res.close();
});
print('Server started on port: ${port}');
});
}
class Comics {
static Uuid uuid = new Uuid();
static Dirty db = new Dirty('dart_comics.db');
static index(HttpRequest req) {
// print(db.values.toList());
HttpResponse res = req.response;
res.headers.contentType
= new ContentType("application", "json", charset: "utf-8");
res.write(JSON.stringify(db.values.toList()));
res.close();
}
static post(HttpRequest req) {
HttpResponse res = req.response;
req.toList().then((list) {
var post_data = new String.fromCharCodes(list[0]);
// print(post_data);
var graphic_novel = JSON.parse(post_data);
graphic_novel['id'] = uuid.v1();
db[graphic_novel['id']] = graphic_novel;
res.statusCode = 201;
res.headers.contentType
= new ContentType("application", "json", charset: "utf-8");
res.write(JSON.stringify(graphic_novel));
res.close();
});
}
static delete(HttpRequest req) {
HttpResponse res = req.response;
var r = new RegExp(r"^/comics/([-\w\d]+)");
var id = r.firstMatch(req.uri.path)[1];
db.remove(id);
res.write('{}');
res.close();
}
}
class Public {
static matcher(HttpRequest req) {
if (req.method != 'GET') return false;
String path = publicPath(req.uri.path);
if (path == null) return false;
req.session['path'] = path;
return true;
}
static handler(req) {
HttpResponse res = req.response;
String ext = req.uri.path.split('.').last;
switch (ext) {
case 'css':
res.headers.contentType =
new ContentType("text", "css", charset: "utf-8");
break;
case 'dart':
res.headers.contentType =
new ContentType("application", "dart", charset: "utf-8");
break;
case 'js':
res.headers.contentType =
new ContentType("application", "javascript", charset: "utf-8");
break;
default:
res.headers.contentType = ContentType.HTML;
}
var file = new File(req.session['path']);
var stream = file.openRead();
stream.pipe(req.response);
}
static String publicPath(String path) {
if (pathExists("web$path")) return "web$path";
if (pathExists("web$path/index.html")) return "web$path/index.html";
}
static bool pathExists(String path) => new File(path).existsSync();
}
log(req) {
req.response.done.then((res){
var now = new DateTime.now();
print('[${now}] "${req.method} ${req.uri.path}" ${logStatusCode(res)}');
});
}
final AnsiPen red = new AnsiPen()..red(bold: true);
final AnsiPen green = new AnsiPen()..green(bold: true);
logStatusCode(HttpResponse res) {
var code = res.statusCode;
if (code > 399) return red(code);
return green(code);
}