pneuma 0.3.0

  • README.md
  • CHANGELOG
  • Installing
  • Versions
  • 42

Pneuma

The vital spirit, soul, or creative force of a person.<br> "In Stoic philosophy, the pneuma penetrates all things, holding things together."<br> Wikipedia

Basically, yet another server framework, that provides middleware architecture and possibility to write MVC backend applications. Fast and very simple. Kickoff in a minute. If you used expressjs before then this framework will not be hard for you to understand.

Installation & setup

Put dart_pneuma package into your pubspec.yaml dependencies section and run pub get. Import the dependency inside your main file

import 'package:pneuma/pneuma.dart';

void main() {
  Pneuma app = new Pneuma();

  app.start();
}

This will start basic http server on default host and port 127.0.0.1:8080 You can either provide host and port named parametes into the constructor or use environment variables: IP and PORT

Usage

Pneuma could be used in three ways:

  1. Writing routed handlers
  2. Using middlewares
  3. Writing controllers with route maps

Writing routed handlers

Idea is the same as in nodejs's expressjs library. You can just map paths to a specific handler which will process the request or pass it forward by calling next callback.

import 'package:pneuma/pneuma.dart';

final RegExp allRoutes = new RegExp('.*');

void main() {
  Pneuma app = new Pneuma()
    ..get('/user', (req, res, next) {
      res.send('Hello user');
    })
    .post('/user', (req, res, next) async {
      dynamic body = await req.body;
      
      print(body);
      res.send('User has been updated');
    })
    .match(allRoutes, (req, res, next) {
      res.send('Not found');
    });

  app.start();
}

Using middlewares

Middleware abstract class should be extended to create your middleware. You will need to override run method, which will receive Request and Response instances as an arguments and should return Future of null or next Middleware usually accessible with this.next property, as Middleware is LinkedListEntity

import 'package:pneuma/pneuma.dart';

class LogMiddleware extends Middleware {
  @override
  Future<Middleware> run(Request req, Response res) {
    DateTime start = new DateTime.now();
    
    res.done.then((_res) {
      DateTime sent = new DateTime.now();
      String stamp = sent.toIso8601String();
      double timeTaken = (sent.millisecondsSinceEpoch - start.millisecondsSinceEpoch) / 1000;

      print('[$stamp]: ${req.method.name} ${_res.statusCode} ${req.uri.toString()} took ${timeTaken} sec.');
    });

    return new Future.value(this.next);
  }
}

void main() {
  Pneuma app = new Pneuma()
    ..use(new LogMiddleware())
    ..get('/user', (req, res, next) {
      res.send('Hello user');
    });

  app.start();
}

Writing controllers with route map

As once popular and still widely used, MVC architectural pattern is very usefull to build large scale applications. Extended from Middleware, Controller class can also be extended to let you map application routes to specific action methods, which should process the request as an endpoints of the app.

class UserController extends Controller {
  TestController() {
    routeMap = {
      new RegExp(r'^\/user'): indexAction,
    };
  }

  void indexAction(Request req, Response res) {
    res.send('Index Page');
  }
}

void main() {
  Pneuma app = new Pneuma()
    ..use(new UserController());

  app.start();
}

routeMap is a Map<RegExp, Invocation> which, in oreder for actions to be mapped to specified paths, should be defined.

Next step for the controllers will be defining annotations to marks actions for a specific route in a more friendlier manner.

class UserController extends Controller {
  @Route(r'^\/user')
  void indexAction(Request req, Response res) {
    res.send('Index Page');
  }
}
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.3.0] - 2018-08-17
### Changed
- Pneuma server get/post/etc shortcut methods accepts dynamic as handler. Could be middleware
- Controller now passes matched params as an arguments aside of req, and res

## [0.2.5] - 2018-08-15
### Added
- Response timeout added

### Fixed
- Adding default headers before app start
- Controller routes fixed

## [0.2.4] - 2018-08-15
### Added
- Added ServerStatus to Pneuma
- On start and server error handling
- ServerStatus stream

### Changed
- Updated HttpHeaders to dart 2 for Response

## [0.2.2] - 2018-08-14
### Added
- request headers

## [0.2.1] - 2018-08-14
### Added
- `stop` for Pneuma instance to stop HttpServer
- methods to controll HttpServer#defaultHeaders

## [0.2.0] - 2018-08-14
### Changed
- Updated to dart version 2

## [0.1.0] - 2018-08-06
### Added
- Middleware as an interface
- Controller to work in MVC kind of manner
- example dir with example

### Changed
- Future based middleware chain

### Removed
- Custom events class
- waterfall recurcive middleware handling
- Log to be a separate middleware

## [0.0.3] - 2015-08-16
### Added
- Initial structure
- Event based controllers concept
- MiddlewareHandler through a waterfall recurcive stack

Use this package as a library

1. Depend on it

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


dependencies:
  pneuma: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:pneuma/pneuma.dart';
  
Version Uploaded Documentation Archive
0.3.0 Aug 17, 2018 Go to the documentation of pneuma 0.3.0 Download pneuma 0.3.0 archive
0.2.5 Aug 15, 2018 Go to the documentation of pneuma 0.2.5 Download pneuma 0.2.5 archive
0.2.4 Aug 15, 2018 Go to the documentation of pneuma 0.2.4 Download pneuma 0.2.4 archive
0.2.3 Aug 14, 2018 Go to the documentation of pneuma 0.2.3 Download pneuma 0.2.3 archive
0.2.2 Aug 14, 2018 Go to the documentation of pneuma 0.2.2 Download pneuma 0.2.2 archive
0.2.1 Aug 14, 2018 Go to the documentation of pneuma 0.2.1 Download pneuma 0.2.1 archive
0.2.0 Aug 13, 2018 Go to the documentation of pneuma 0.2.0 Download pneuma 0.2.0 archive
0.1.4 Aug 8, 2018 Go to the documentation of pneuma 0.1.4 Download pneuma 0.1.4 archive
0.1.3 Aug 6, 2018 Go to the documentation of pneuma 0.1.3 Download pneuma 0.1.3 archive
0.1.2 Aug 6, 2018 Go to the documentation of pneuma 0.1.2 Download pneuma 0.1.2 archive

All 15 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
88
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
42
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, other

Primary library: package:pneuma/pneuma.dart with components: io.

Health issues and suggestions

Document public APIs (-9.81 points)

103 out of 105 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Fix lib/src/errors.dart. (-1.49 points)

Analysis of lib/src/errors.dart reported 3 hints:

line 11 col 49: 'INTERNAL_SERVER_ERROR' is deprecated and shouldn't be used.

line 24 col 45: 'NOT_FOUND' is deprecated and shouldn't be used.

line 28 col 55: 'METHOD_NOT_ALLOWED' is deprecated and shouldn't be used.

Fix lib/src/response.dart. (-1 points)

Analysis of lib/src/response.dart reported 2 hints:

line 35 col 11: Always override hashCode if overriding ==.

line 40 col 62: 'MOVED_TEMPORARILY' is deprecated and shouldn't be used.

Format lib/src/app.dart.

Run dartfmt to format lib/src/app.dart.

Fix additional 6 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/body.dart (Run dartfmt to format lib/src/body.dart.)
  • lib/src/controller.dart (Run dartfmt to format lib/src/controller.dart.)
  • lib/src/middleware.dart (Run dartfmt to format lib/src/middleware.dart.)
  • lib/src/middleware/conditional_middleware.dart (Run dartfmt to format lib/src/middleware/conditional_middleware.dart.)
  • lib/src/request.dart (Run dartfmt to format lib/src/request.dart.)
  • lib/src/types.dart (Run dartfmt to format lib/src/types.dart.)

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use pneuma.dart. Packages with multiple examples should use example/readme.md.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
http_server >=0.9.8 <1.0.0 0.9.8+1
Transitive dependencies
mime 0.9.6+2
path 1.6.2