preface

origin

Recently, WHEN I was writing some personal projects, I always needed some background interfaces. Before, I used to get an interface with basic functions through BMob or leanCloud, and I was thinking about learning a background. I tried The Flask of Python at first, and the experience was not bad. As a Developer of Flutter, DART is the language I write most often, so I tried to use DART to write the corresponding background interface of the client. The whole study currently takes two days.

perception

Most people are still skeptical about what’s behind Dart, and probably most of the focus is on,

  • By default, only GET, PUT, PUT, and delete are supported
  • The default database is Postgres

My comments in Zhihu and other places are that they are immature and not suitable for large projects, but not for small projects.

Selects the framework

  • aqueduct

Using the DART environment

Dart SDK version: 2.10.4 (stable) (Wed Nov 11 13:35:58 2020 +0100) on "macos_x64"
Copy the code

Implementation requirements

  • Account registration and login
  • Standard OAuth2.0 token authentication.
  • Swagger document generation.

This article will not cover actual combat

Tip: THE author will not elaborate on the principle of each part, only from the author’s ideas to share hands-on development experience

Begin to use

Installation environment

Execute at terminal

pub global activate aqueduct
Copy the code

Usually there is no problem, but there is a warning.

This is usually solved by adding the following environment variables.

export PATH="$PATH":"$HOME/.pub-cache/bin"
Copy the code

Create a project

aqueduct create example
Copy the code

The project structure

. ├ ─ ─ the README. Md ├ ─ ─ analysis_options. Yaml ├ ─ ─ bin │ └ ─ ─. Main dart ├ ─ ─ config. The SRC. The yaml ├ ─ ─ config. The yaml ├ ─ ─ lib │ ├ ─ ─ ├─ ├─ trash ├─ trash ├─ trash ├─ trash ├─ trash ├─ trash ├─ trash ├─ trash ├─ trash ├─ trash ├─ trashCopy the code

Bin /main.dart is the natural entry point for the entire project, with the main code in lib/channel.dart

(Optional) Changing the Configuration file

config.yaml

port: 8000
Copy the code

Port indicates the port number on which the listener is running.

Start the service

Run dart bin/main.dart or Aqueduct serve

There is a bug here, even if we change the port number in YAML, running Aqueduct Serve will still show that the current port is 8888, but the actual effect is our specific setting.

Switch ports in the program

Future main() async {
  finalapp = Application<ServerChannel>() .. options.configurationFilePath ="config.yaml"
    ..options.port = 8000;
}
Copy the code

The test interface

Use a browser or Postmanhttp://127.0.0.1:$port/example

Installing the Database

Open the official website www.postgresql.org/

Download the database corresponding to the platform, the author is MAC, long after downloading so

Double-clicking on one of the databases opens the terminal, which has that postgres by default. Double-clicking on it and typing \password can change the password.

Or install via BREW

brew install postgresql
Copy the code

Apt to install

apt install postgresql
Copy the code

Starting the Database Service

MAC command version

brew services start postgresql
Copy the code

MAC graphics edition

Click start in the graphical interface and then in the background.

ubuntu

/etc/init.d/postgresql start
Copy the code

or

pg_ctl start  
Copy the code

useNavicat PremiumConnecting to a Database

Click the connect button in the upper left corner and select PostgresQL. The one below is the default.

The use of Navicat won’t go into detail

Create a new entity class

Common entity class

class Server extends ManagedObject<_Server> implements _Server {
  Server() {}
}
class _Server {
  @Column(
    primaryKey: true.)int userId;
  @Column(
    databaseType: ManagedPropertyType.string,
  )
  String serversBase64;
}

Copy the code

An entity class that supports OAuth2.0 authentication


class User extends ManagedObject<_User>
    implements _User.ManagedAuthResourceOwner<_User> { User() { mobile ?? =' '; qq ?? =' '; role ?? =1; salt ?? =' '; }}class _User extends ResourceOwnerTableDefinition {
  @override
  @primaryKey // as primary key == @
  int id;
  String password;
  String mobile;
  String qq;
  @Column(unique: true, nullable: true) // Add index
  String email;
  int role;

  @Column(indexed: true) // Add index
  DateTime createDate;
}
Copy the code

The two entity classes will correspond to two tables, one is a user table with the primaryKey id, which is specified as an increment by the annotation code @primarykey, and the other is a custom table related to the user whose primaryKey is the id of the user data in the user table.

Nullable: true specifies that the value is unique in the entire table. Nullable: true specifies that the value is nullable.

Initialize the migration file

I don’t know if that’s a bad idea

Dart first imports the above entity class

Modify the configuration file database.yaml

username: postgres
password: * * *
host: localhost
port: 5432
databaseName: postgres
Copy the code

Run aqueduct db generate

After the migrations/00000001_initial.migration.dart file is generated.

The generated pit

There was a major change after dart2.8 that caused this code generator exception.

This can be resolved by adding the following code to YAML, which is officially recommended over demoting the DART version.

dependency_overrides:
  postgres: 2.11.
Copy the code

Mapping to a database

Run the pub run aqueduct db upgrade command. Note that aqueduct db upgrade will fail after dart version 2.8.

End of executionNavicatIf you look, you can see that the structure of the table already exists.The new database is created inNavicatRight-click the connected database in the upper left corner

All right, that’s the first one.