2024-09-23 01:44:59 +00:00
|
|
|
import 'package:platform_container/mirrors.dart';
|
2024-09-25 04:04:57 +00:00
|
|
|
import 'package:platform_core/core.dart';
|
|
|
|
import 'package:platform_core/http.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
void main() async {
|
|
|
|
// Logging set up/boilerplate
|
|
|
|
Logger.root.onRecord.listen(print);
|
|
|
|
|
|
|
|
// Create our server.
|
2024-09-28 23:14:48 +00:00
|
|
|
var app =
|
|
|
|
Application(logger: Logger('protevus'), reflector: MirrorsReflector());
|
|
|
|
var http = PlatformHttp(app);
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
await app.mountController<ArtistsController>();
|
|
|
|
|
|
|
|
// Simple fallback to throw a 404 on unknown paths.
|
|
|
|
app.fallback((req, res) {
|
2024-09-28 23:14:48 +00:00
|
|
|
throw PlatformHttpException.notFound(
|
2024-09-23 01:44:59 +00:00
|
|
|
message: 'Unknown path: "${req.uri!.path}"',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.errorHandler = (e, req, res) => e.toJson();
|
|
|
|
|
|
|
|
await http.startServer('127.0.0.1', 3000);
|
|
|
|
print('Listening at ${http.uri}');
|
|
|
|
app.dumpTree();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArtistsController extends Controller {
|
|
|
|
List index() {
|
|
|
|
return ['Elvis', 'Stevie', 'Van Gogh'];
|
|
|
|
}
|
|
|
|
|
|
|
|
String getById(int id, RequestContext req) {
|
|
|
|
return 'You fetched ID: $id from IP: ${req.ip}';
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose.post
|
|
|
|
Future<Artist> form(RequestContext req) async {
|
|
|
|
// Deserialize the body into an artist.
|
|
|
|
var artist = await req.deserializeBody((m) {
|
|
|
|
return Artist(name: m!['name'] as String? ?? '(unknown name)');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return it (it will be serialized to JSON).
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Artist {
|
|
|
|
final String? name;
|
|
|
|
|
|
|
|
Artist({this.name});
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return {'name': name};
|
|
|
|
}
|
|
|
|
}
|