1. Introduction

Aqueduct is an HTTP Web server framework for building REST applications written in Dart.

2. Core concepts

Resources (Resources).

A resource is something that an application exposes through its HTTP API. It can be anything, such as scores, posts, user relationships, and so on, and when the resource is retrieved, it is typically stored in the response body as JSON.

Routing (Routing)

Path identification of HTTP request resources, for example: http://www.baidu.com, when we visit Baidu, Baidu’s server returns us resources in the form of text/ HTML, which is parsed by the browser and presented to us. When we request the route to the public server, we can display the content we want. If we cannot request the route to the public server, The server returns a 404 page

Controller (the Controller)

The object that handles the request. For example, a controller can fetch data from a database and send it to a client in response. Another controller may verify the authorization header request user and password is valid, therefore, respond to the request may be composed of multiple controller for the processing of the request, in general, multiple controllers linked together the request processing for the pipeline, in the form of these links can also be together controller called channel, the current controller of the validation is not through, Then the subsequent controllers will not process it

ApplicationChannel

An object that contains all the controllers of the application. It is also responsible for initializing the application’s services, reading configuration files, and other boot-related tasks

Services

Encapsulate complex task or algorithms, the purpose is to provide more detailed behavior simple methods, such as: the main users of the database link service controller, through the service passed as a parameter to the constructor of the controller, the service into the controller, the controller to keep to the service of reference, in order to process the request to use it

Isolation (Isolates)

Memory separate threads, on a separate objects created cannot be another isolation references, the application starts, generates contains a copy of the application code of one or more of the isolation zone, can effective load balancing, each area has its own set of services, such as a database connection, which can eliminate the problem of database connection pool

Binding (Bindings)

Annotations (similar to autowrite), requests may contain request headers, query parameters that need to be parsed, validated, and used in controller code, and bindings are annotations that are added to variables that automatically perform this parsing and validation

Quertes and Data Models

ORM object relational mapping, which provides static type queries that are easy to write and test, provides command line tools to generate database migration files for detecting changes to the data model that can be applied to the real-time version of the database, which can also be represented as JSON objects

Authorization (Authorization)

OAuth2.0 authorization framework, which can be integrated directly into your application or as a stand-alone federated service to provide authorization server, default implementation using Aqueduct ORM stored in PostgreSQL

Documentation

OpenAPI3.0 is a standardized document format for the HTTP API that supports automatic documentation

3. Start

Before you start, you need to configure your environment and download the relevant SDK in advance to use the pub command. The following uses MAC as an example

  • Download the download address: https://storage.googleapis.com/dart-archive/channels/stable/release/2.4.0/sdk/dartsdk-macos-x64-release.zip if it is other flat Stable -> dev(Channel branch)2.4.0 -> version number macOS -> Window, Linux -> i32

  • Open the console, type vim ~/.bash_profile and press A to go into edit mode. Add the following: DART_HOME is your SDK path

1export DART_HOME=/Users/rhyme/env/flutter/bin/cache/dart-sdk2export PATH=$PATH:$DART_HOME/binCopy the code

After the configuration is complete, press ESC to exit the editing mode, then press Shift +: to enter the wq key to exit and save the configuration file, and run source ~/. Bash_profile to make the configuration file effective. If you run the dart command and see the following output, it is considered successful

image.png
  • Activating the Aqueduct package can compare pub to NPM, which is also run as a script, by typing the following on the command line

1pub global activate aqueductCopy the code
image.png

Aqueduct
Warning
pub
aqueduct
export PATH=$PATH:$HOME/.pub-cache/bin
Configure the environment
aqueduct

image.png
  • At this point, we are ready to use Aqueduct to create our own DART service project.

1Aqueduct create your project nameCopy the code

Generate a project template in the current directory

image.png
  • Dart bin/main.dart or Aqueduct serve to start

    image.png
    image.png

The project will start half of the threads based on the number of cores in your computer. Currently, my computer has 4 cores, so I started 2. The default port number of 8888 is 8888, and the project adds an interface /example for us by default, which can be accessed directly from the browser

image.png

Ok, that’s all for today. Try to make it easy for everyone to learn about Dart. It’s not easy for the author to write.