43 lines
1.4 KiB
Dart
43 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
import 'package:protevus_framework/http.dart';
|
|
import 'package:protevus_seo/protevus_seo.dart';
|
|
import 'package:protevus_static/protevus_static.dart';
|
|
import 'package:file/local.dart';
|
|
import 'package:http_parser/http_parser.dart';
|
|
|
|
void main() async {
|
|
var app = Protevus();
|
|
var fs = const LocalFileSystem();
|
|
var http = ProtevusHttp(app);
|
|
|
|
// You can wrap a [VirtualDirectory]
|
|
var vDir = inlineAssetsFromVirtualDirectory(
|
|
VirtualDirectory(
|
|
app,
|
|
fs,
|
|
source: fs.directory('web'),
|
|
),
|
|
);
|
|
|
|
app.fallback(vDir.handleRequest);
|
|
|
|
// 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')));
|
|
|
|
app.get('/using_response_buffer', (req, res) async {
|
|
var indexHtml = fs.directory('web').childFile('index.html');
|
|
var contents = await indexHtml.readAsString();
|
|
res
|
|
..contentType = MediaType('text', 'html', {'charset': 'utf-8'})
|
|
..buffer!.add(utf8.encode(contents));
|
|
});
|
|
|
|
app.fallback((req, res) => throw ProtevusHttpException.notFound());
|
|
|
|
var server = await http.startServer('127.0.0.1', 3000);
|
|
print('Listening at http://${server.address.address}:${server.port}');
|
|
}
|