Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions example/basic/body_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Future<void> main() async {
'/static/**',
StaticHandler.directory(
Directory('example/static_files'),
cacheControl: (final _, final __) => CacheControlHeader(maxAge: 3600),
cacheControl: (_, _) => CacheControlHeader(maxAge: 3600),
).asHandler,
);

Expand All @@ -59,7 +59,7 @@ Future<void> main() async {
'/logo',
StaticHandler.file(
File('example/static_files/logo.svg'),
cacheControl: (final _, final __) => CacheControlHeader(maxAge: 86400),
cacheControl: (_, _) => CacheControlHeader(maxAge: 86400),
).asHandler,
);

Expand All @@ -80,12 +80,12 @@ Future<void> main() async {
}

/// Basic text response handler
ResponseContext helloHandler(final NewContext ctx) {
ResponseContext helloHandler(final RequestContext ctx) {
return ctx.respond(Response.ok(body: Body.fromString('Hello, World!')));
}

/// JSON with automatic MIME detection handler
ResponseContext dataHandler(final NewContext ctx) {
ResponseContext dataHandler(final RequestContext ctx) {
return ctx.respond(
Response.ok(
body: Body.fromString('{"message": "Hello"}'),
Expand All @@ -95,7 +95,7 @@ ResponseContext dataHandler(final NewContext ctx) {
}

/// Small file handler - read entire file into memory
Future<ResponseContext> smallFileHandler(final NewContext ctx) async {
Future<ResponseContext> smallFileHandler(final RequestContext ctx) async {
final file = File('example.txt');

if (!await file.exists()) {
Expand All @@ -108,7 +108,7 @@ Future<ResponseContext> smallFileHandler(final NewContext ctx) async {
}

/// Large file handler - stream for memory efficiency
Future<ResponseContext> largeFileHandler(final NewContext ctx) async {
Future<ResponseContext> largeFileHandler(final RequestContext ctx) async {
final file = File('large-file.dat');

if (!await file.exists()) {
Expand All @@ -128,14 +128,14 @@ Future<ResponseContext> largeFileHandler(final NewContext ctx) async {
}

/// Reading request body as string handler
Future<ResponseContext> echoHandler(final NewContext ctx) async {
Future<ResponseContext> echoHandler(final RequestContext ctx) async {
final content = await ctx.request.readAsString();

return ctx.respond(Response.ok(body: Body.fromString('You sent: $content')));
}

/// JSON API handler
Future<ResponseContext> apiDataHandler(final NewContext ctx) async {
Future<ResponseContext> apiDataHandler(final RequestContext ctx) async {
final jsonData = await ctx.request.readAsString();
final data = jsonDecode(jsonData);

Expand All @@ -152,7 +152,7 @@ Future<ResponseContext> apiDataHandler(final NewContext ctx) async {
}

/// File upload handler with size validation
Future<ResponseContext> uploadHandler(final NewContext ctx) async {
Future<ResponseContext> uploadHandler(final RequestContext ctx) async {
const maxFileSize = 10 * 1024 * 1024; // 10MB
final contentLength = ctx.request.body.contentLength;

Expand All @@ -171,7 +171,7 @@ Future<ResponseContext> uploadHandler(final NewContext ctx) async {
}

/// Image response handler with automatic format detection
Future<ResponseContext> imageHandler(final NewContext ctx) async {
Future<ResponseContext> imageHandler(final RequestContext ctx) async {
final file = File('example/static_files/logo.svg');
final imageBytes = await file.readAsBytes();

Expand All @@ -186,7 +186,7 @@ Future<ResponseContext> imageHandler(final NewContext ctx) async {
}

/// Streaming response handler with chunked transfer encoding
Future<ResponseContext> streamHandler(final NewContext ctx) async {
Future<ResponseContext> streamHandler(final RequestContext ctx) async {
Stream<Uint8List> generateLargeDataset() async* {
for (var i = 0; i < 100; i++) {
await Future<void>.delayed(const Duration(milliseconds: 50));
Expand Down
22 changes: 11 additions & 11 deletions example/context/context_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:relic/relic.dart';
import 'package:web_socket/web_socket.dart';

/// Demonstrates the four main context types in Relic using proper routing:
/// - NewContext: Starting point for all requests
/// - RequestContext: Starting point for all requests
/// - ResponseContext: HTTP response handling
/// - ConnectContext: WebSocket connections
/// - HijackContext: Raw connection control
/// - ConnectionContext: WebSocket connections
/// - HijackedContext: Raw connection control

/// Simple HTML page for demonstration
String _htmlHomePage() {
Expand All @@ -24,16 +24,16 @@ String _htmlHomePage() {
<ul>
<li><a href="/api">API Example (ResponseContext)</a></li>
<li><a href="/api/users/123">User API with Parameters (ResponseContext)</a></li>
<li><a href="/ws">WebSocket Example (ConnectContext)</a></li>
<li><a href="/custom">Custom Protocol (HijackContext)</a></li>
<li><a href="/ws">WebSocket Example (ConnectionContext)</a></li>
<li><a href="/custom">Custom Protocol (HijackedContext)</a></li>
</ul>
<p>This demonstrates the different context types in Relic using proper routing.</p>
</body>
</html>
''';
}

Future<ResponseContext> homeHandler(final NewContext ctx) async {
Future<ResponseContext> homeHandler(final RequestContext ctx) async {
return ctx.respond(
Response.ok(
body: Body.fromString(
Expand All @@ -45,7 +45,7 @@ Future<ResponseContext> homeHandler(final NewContext ctx) async {
);
}

Future<ResponseContext> apiHandler(final NewContext ctx) async {
Future<ResponseContext> apiHandler(final RequestContext ctx) async {
final data = {
'message': 'Hello from Relic API!',
'timestamp': DateTime.now().toIso8601String(),
Expand All @@ -59,7 +59,7 @@ Future<ResponseContext> apiHandler(final NewContext ctx) async {
);
}

Future<ResponseContext> userHandler(final NewContext ctx) async {
Future<ResponseContext> userHandler(final RequestContext ctx) async {
final userId = ctx.pathParameters[#id];
final data = {
'userId': userId,
Expand All @@ -74,7 +74,7 @@ Future<ResponseContext> userHandler(final NewContext ctx) async {
);
}

ConnectContext webSocketHandler(final NewContext ctx) {
ConnectionContext webSocketHandler(final RequestContext ctx) {
return ctx.connect((final webSocket) async {
log('WebSocket connection established');

Expand All @@ -98,7 +98,7 @@ ConnectContext webSocketHandler(final NewContext ctx) {
});
}

HijackContext customProtocolHandler(final NewContext ctx) {
HijackedContext customProtocolHandler(final RequestContext ctx) {
return ctx.hijack((final channel) {
log('Connection hijacked for custom protocol');

Expand All @@ -115,7 +115,7 @@ HijackContext customProtocolHandler(final NewContext ctx) {
});
}

Future<ResponseContext> dataHandler(final NewContext ctx) async {
Future<ResponseContext> dataHandler(final RequestContext ctx) async {
final request = ctx.request;

// Access basic HTTP information
Expand Down
2 changes: 1 addition & 1 deletion example/context/context_property_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Handler requestIdMiddleware(final Handler next) {
}

// Handler that uses the stored request ID
Future<ResponseContext> handler(final NewContext ctx) async {
Future<ResponseContext> handler(final RequestContext ctx) async {
// Retrieve the request ID that was set by middleware
final requestId = _requestIdProperty[ctx];

Expand Down
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Future<void> main() async {
await app.serve();
}

ResponseContext hello(final NewContext ctx) {
ResponseContext hello(final RequestContext ctx) {
final name = ctx.pathParameters[#name];
final age = int.parse(ctx.pathParameters[#age]!);

Expand Down
6 changes: 3 additions & 3 deletions example/middleware/auth_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:relic/relic.dart';
/// Simple authentication middleware
Middleware authMiddleware() {
return (final Handler innerHandler) {
return (final NewContext ctx) async {
return (final RequestContext ctx) async {
// Check for API key in header
final apiKey = ctx.request.headers['X-API-Key']?.first;

Expand All @@ -22,12 +22,12 @@ Middleware authMiddleware() {
}

/// Public handler (no auth needed)
Future<ResponseContext> publicHandler(final NewContext ctx) async {
Future<ResponseContext> publicHandler(final RequestContext ctx) async {
return ctx.respond(Response.ok(body: Body.fromString('This is public!')));
}

/// Protected handler (needs auth)
Future<ResponseContext> protectedHandler(final NewContext ctx) async {
Future<ResponseContext> protectedHandler(final RequestContext ctx) async {
return ctx.respond(Response.ok(body: Body.fromString('This is protected!')));
}

Expand Down
4 changes: 2 additions & 2 deletions example/middleware/cors_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:relic/relic.dart';
/// Simple CORS middleware
Middleware corsMiddleware() {
return (final Handler innerHandler) {
return (final NewContext ctx) async {
return (final RequestContext ctx) async {
// Handle preflight requests
if (ctx.request.method == Method.options) {
return ctx.respond(
Expand Down Expand Up @@ -40,7 +40,7 @@ Middleware corsMiddleware() {
}

/// API handler
Future<ResponseContext> apiHandler(final NewContext ctx) async {
Future<ResponseContext> apiHandler(final RequestContext ctx) async {
final data = {'message': 'Hello from CORS API!'};

return ctx.respond(Response.ok(body: Body.fromString(jsonEncode(data))));
Expand Down
2 changes: 1 addition & 1 deletion example/middleware/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Future<void> main() async {
typedef User = int; // just an example
final _auth = ContextProperty<User>('auth');

extension on RequestContext {
extension on Context {
User get user => _auth[this];
}

Expand Down
10 changes: 5 additions & 5 deletions example/middleware/middleware_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:relic/relic.dart';
/// Middleware that adds a custom header
Middleware addHeaderMiddleware() {
return (final Handler innerHandler) {
return (final NewContext ctx) async {
return (final RequestContext ctx) async {
final result = await innerHandler(ctx);

if (result is ResponseContext) {
Expand All @@ -27,7 +27,7 @@ Middleware addHeaderMiddleware() {
/// Timing middleware
Middleware timingMiddleware() {
return (final Handler innerHandler) {
return (final NewContext ctx) async {
return (final RequestContext ctx) async {
final stopwatch = Stopwatch()..start();

final result = await innerHandler(ctx);
Expand All @@ -43,7 +43,7 @@ Middleware timingMiddleware() {
/// Simple error handling middleware
Middleware errorHandlingMiddleware() {
return (final Handler innerHandler) {
return (final NewContext ctx) async {
return (final RequestContext ctx) async {
try {
return await innerHandler(ctx);
} catch (error) {
Expand All @@ -58,13 +58,13 @@ Middleware errorHandlingMiddleware() {
}

/// Simple handlers
Future<ResponseContext> homeHandler(final NewContext ctx) async {
Future<ResponseContext> homeHandler(final RequestContext ctx) async {
return ctx.respond(
Response.ok(body: Body.fromString('Hello from home page!')),
);
}

Future<ResponseContext> apiHandler(final NewContext ctx) async {
Future<ResponseContext> apiHandler(final RequestContext ctx) async {
final data = {'message': 'Hello from API!'};

return ctx.respond(Response.ok(body: Body.fromString(jsonEncode(data))));
Expand Down
6 changes: 3 additions & 3 deletions example/middleware/pipeline_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:relic/relic.dart';
/// Simple middleware that adds a header
Middleware addServerHeader() {
return (final Handler innerHandler) {
return (final NewContext ctx) async {
return (final RequestContext ctx) async {
final result = await innerHandler(ctx);

if (result is ResponseContext) {
Expand All @@ -23,7 +23,7 @@ Middleware addServerHeader() {
}

/// Simple handler
Future<ResponseContext> simpleHandler(final NewContext ctx) async {
Future<ResponseContext> simpleHandler(final RequestContext ctx) async {
return ctx.respond(
Response.ok(body: Body.fromString('Hello from Pipeline!')),
);
Expand All @@ -41,7 +41,7 @@ void main() async {
RelicApp()
..use('/', logRequests())
..use('/', addServerHeader())
..get('/router', (final NewContext ctx) async {
..get('/router', (final RequestContext ctx) async {
return ctx.respond(
Response.ok(body: Body.fromString('Hello from Router!')),
);
Expand Down
Loading