2018-11-08 16:19:31 +00:00
|
|
|
import 'dart:convert';
|
2021-08-20 02:33:49 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_framework/http.dart';
|
|
|
|
import 'package:angel3_seo/angel3_seo.dart';
|
|
|
|
import 'package:angel3_static/angel3_static.dart';
|
2018-07-09 06:14:25 +00:00
|
|
|
import 'package:file/local.dart';
|
2018-11-08 16:19:31 +00:00
|
|
|
import 'package:http_parser/http_parser.dart';
|
2018-07-09 06:14:25 +00:00
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
void main() async {
|
|
|
|
var app = Angel();
|
2018-07-09 06:14:25 +00:00
|
|
|
var fs = const LocalFileSystem();
|
2021-06-20 12:37:20 +00:00
|
|
|
var http = AngelHttp(app);
|
2018-07-09 06:14:25 +00:00
|
|
|
|
2018-07-09 13:49:25 +00:00
|
|
|
// You can wrap a [VirtualDirectory]
|
|
|
|
var vDir = inlineAssetsFromVirtualDirectory(
|
2021-06-20 12:37:20 +00:00
|
|
|
VirtualDirectory(
|
2018-07-09 06:14:25 +00:00
|
|
|
app,
|
|
|
|
fs,
|
|
|
|
source: fs.directory('web'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2018-11-08 16:17:44 +00:00
|
|
|
app.fallback(vDir.handleRequest);
|
2018-07-09 06:14:25 +00:00
|
|
|
|
2018-07-09 13:49:25 +00:00
|
|
|
// OR, just add a finalizer. Note that [VirtualDirectory] *streams* its response,
|
|
|
|
// so a response finalizer does not touch its contents.
|
|
|
|
//
|
|
|
|
// You likely won't need to use both; it just depends on your use case.
|
|
|
|
app.responseFinalizers.add(inlineAssets(fs.directory('web')));
|
|
|
|
|
2018-11-08 16:17:44 +00:00
|
|
|
app.get('/using_response_buffer', (req, res) async {
|
2018-07-09 13:49:25 +00:00
|
|
|
var indexHtml = fs.directory('web').childFile('index.html');
|
|
|
|
var contents = await indexHtml.readAsString();
|
|
|
|
res
|
2021-06-20 12:37:20 +00:00
|
|
|
..contentType = MediaType('text', 'html', {'charset': 'utf-8'})
|
|
|
|
..buffer!.add(utf8.encode(contents));
|
2018-07-09 13:49:25 +00:00
|
|
|
});
|
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
app.fallback((req, res) => throw AngelHttpException.notFound());
|
2018-07-09 06:14:25 +00:00
|
|
|
|
|
|
|
var server = await http.startServer('127.0.0.1', 3000);
|
|
|
|
print('Listening at http://${server.address.address}:${server.port}');
|
|
|
|
}
|