serral 0.1.14

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

serral #

中文文档

Fast backEnd server framework provided by Dart, like Koa. Easy add middleware at before request and after request. Easy extend context Type and values.

API #

API Documentation

Getting Started #

Create dart project #

Create dir and create pubspec.yaml

$ mkdir your_project && cd your_project
$ touch pubspec.yaml

pubspec.yaml:

name: your_project
version: 0.0.1
environment:
  sdk: '>=2.3.0 <3.0.0'

dependencies:
  serral: any

Install dependencies:

$ pub get

Coding your first Serral server #

$ mkdir lib
$ touch lib/main.dart

Edit lib/main.dart:

import 'package:serral/serral.dart';

void main() {
  final app = Serral();

  // open cros
  app.before(app.addCorsHeaders);

  app.before((SerralCtx ctx) {
    print(ctx.request.uri.toString());
    ctx.context['dog'] = 100;
  });

  app.after((SerralCtx ctx) {
    print('end');
  });

  app.GET('/', getHome);
  app.POST('/dog', postDog);

  app.serve(port: 5100);
}

void getHome(SerralCtx ctx) async {
  // read ctx.context, check app.before;
  print(ctx.context['dog']);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}

void postDog(SerralCtx ctx) async {
  print(ctx.body);
  // use Futrue, check app.after;
  await Future.delayed(Duration(milliseconds: 300));
  ctx.send(200, 'order');
}

Start server #

$ dart lib/main.dart

Ok, server is running:s

serral runing: http://127.0.0.1:5100

Use mongodb or other driver #

Install mongo_dart:

dev_dependencies:
  mongo_dart: any

Case 1, save in context: #

import 'package:mongo_dart/mongo_dart.dart';

import 'package:serral/serral.dart';

void main() async {
  Db db = new Db("mongodb://127.0.0.1:27017/test");
  await db.open();

  final app = Serral();

  app.before((SerralCtx ctx) {
    // add mongodb in context
    ctx.context['db'] = db;
  });

  app.GET('/', getHome);

  app.serve(port: 5100);
}

void getHome(SerralCtx ctx) async {
  // use mongodb in some router:
  Db db = ctx.context['db'];
  print(db);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}

Case 2, mixin SerralCtx #

import 'package:mongo_dart/mongo_dart.dart';

import 'package:serral/serral.dart';

class MongoCtx with SerralCtx {
  Db db;
}

void main() async {
  Db db = new Db("mongodb://127.0.0.1:27017/test");
  await db.open();

  // Use MongoCtx repeat SerralCtx
  final app = Serral(()=> MongoCtx());

  app.before((MongoCtx ctx) {
    // save db at MongodbCtx.db
    ctx.db = db;
  });

  app.GET('/', getHome);

  app.serve(port: 5100);
}

void getHome(MongoCtx ctx) async {
  // use mongodb in some router:
  print(ctx.db);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}

AOT build and AOT runtime #

AOT build:

dart2aot lib/main.dart lib/main.aot

use dartaotruntime run it:

dartaotruntime lib/main.aot

[0.1.14] - Change route param name #

void ANY(String route, Function fn) -> Change to: void ANY(String url, Function fn)

[0.1.13] - Change file name #

Change: lib/main.dart -> lib/serral.dart

[0.1.12] - Update Readme #

[0.1.11] - Fix Serral initCtx #

[0.1.10] - Add Chinese README.md #

[0.1.9] - Change description #

[0.1.7] - Add mixin SerralCtx #

  • Look README.md

[0.1.6] - Add queryPaser #

Map<String, dynamic> data = ctx.queryPaser('name=dog&age=10');
print(data);

[0.1.5] - Add API: addCorsHeaders #

open cros:

app.before(app.addCorsHeaders);

[0.1.4] - Add api app.sendJson #

[0.1.3] - Fix parse body data #

  • fix parse body data
  • change API: serral.use -> serral.ANY

[0.1.2] - Update README #

[0.1.1] - Update README #

[0.1.0] - Update Github URL #

[0.0.7] - Use Likt Koa API #

[0.0.5] - Add middleware #

[0.0.1] - TODO: Add release date. #

  • TODO: Describe initial release.

example/example.dart

import 'package:serral/serral.dart';

void main() {
  final app = Serral();
  app.serve(port: 5100);

  // open cros
  app.before(app.addCorsHeaders);

  app.before((SerralCtx ctx) {
    print(ctx.request.uri.toString());
    ctx.context['dog'] = 100;
  });

  app.after((SerralCtx ctx) {
    print('end');
  });

  app.GET('/', getHome);
  app.POST('/dog', postDog);
}

void getHome(SerralCtx ctx) async {
  // read ctx.context, check app.before;
  print(ctx.context['dog']);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}

void postDog(SerralCtx ctx) async {
  print(ctx.body);
  // use Futrue, check app.after;
  await Future.delayed(Duration(milliseconds: 300));
  ctx.send(200, 'order');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  serral: ^0.1.14

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:serral/serral.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
79
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Health issues and suggestions

Fix lib/serral.dart. (-25.38 points)

Analysis of lib/serral.dart failed with 1 error, 1 hint:

line 141 col 52: The argument type 'Utf8Decoder' can't be assigned to the parameter type 'StreamTransformer<Uint8List, dynamic>'.

line 160 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Package is getting outdated. (-1.37 points)

The package was last published 52 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
Dev dependencies
mongo_dart any