본문으로 건너뛰기

Introduction to Aqueduct

Introduction to Aqueduct

Aqueduct is a powerful and flexible server-side framework for building web applications in Dart programming language. It follows the Model-View-Controller (MVC) architectural pattern, making it easy to develop scalable and maintainable applications.

History​

Aqueduct was developed by the Dart team at Google as an open-source project. It was initially released in 2017 and has since gained popularity among developers due to its simplicity and performance. The framework is regularly updated and maintained by a dedicated community of developers.

Features​

1. ORM (Object-Relational Mapping)​

Aqueduct provides a built-in ORM that allows you to interact with your database using Dart classes. This greatly simplifies database operations and provides a higher level of abstraction. Here's an example of defining a database model using Aqueduct's ORM:

import 'package:aqueduct/aqueduct.dart';

class User extends ManagedObject<_User> implements _User {}

class _User {

int id;

(unique: true)
String username;

()
String password;
}

In the above code snippet, we define a User class that extends ManagedObject and represents a database table. The @primaryKey and @Column annotations define the primary key and column mappings, respectively.

2. Routing and Controllers​

Aqueduct allows you to define routes and map them to controllers. Controllers handle incoming requests and generate responses. Here's an example of defining a route and a corresponding controller:

import 'package:aqueduct/aqueduct.dart';

class UsersController extends Controller {
.get()
Future<Response> getUsers() async {
final users = await fetchUsersFromDatabase();
return Response.ok(users);
}
}

In the above code snippet, we define a UsersController class that extends Controller and handles the GET request to retrieve users from the database. The @Operation.get() annotation maps the method to the GET HTTP verb.

3. Authentication and Authorization​

Aqueduct provides built-in support for authentication and authorization. You can easily secure your routes and authenticate users using various authentication schemes like JWT (JSON Web Tokens). Here's an example of protecting a route with JWT authentication:

import 'package:aqueduct/aqueduct.dart';

class UsersController extends Controller {
.get()
.requireToken()
Future<Response> getUsers() async {
final users = await fetchUsersFromDatabase();
return Response.ok(users);
}
}

In the above code snippet, the @Authorize.requireToken() annotation ensures that only authenticated requests with a valid JWT token can access the getUsers() method.

4. WebSocket Support​

Aqueduct provides support for WebSocket communication, allowing real-time bidirectional communication between clients and the server. Here's an example of setting up a WebSocket endpoint:

import 'package:aqueduct/aqueduct.dart';

class WebSocketController extends Controller {

Future<RequestOrResponse> handle(Request request) async {
if (request.isWebSocket) {
final channel = WebSocketChannel(request);
channel.sink.add("Connected to WebSocket!");
// Handle WebSocket communication here
return null;
}
return Response.notFound();
}
}

In the above code snippet, we override the handle() method to check if the request is a WebSocket request. If it is, we establish a WebSocket channel and send an initial message to the client.

Examples​

  • Aqueduct Todo List: A simple todo list application built with Aqueduct.

  • Aqueduct Chat: A real-time chat application using Aqueduct's WebSocket support.

  • Aqueduct Blog: A blog application with authentication and authorization features.

For more information and detailed documentation, you can visit the official Aqueduct website: https://aqueduct.io.

Aqueduct provides a comprehensive set of features for building web applications in Dart. Its ORM, routing and controller system, authentication and authorization support, and WebSocket capabilities make it a powerful choice for developing scalable and efficient server-side applications.