dox_core

The core of the Dox framework: HTTP server, router, requests, responses, DTO input, cache, storage and config.

pub package license: MIT

Part of Dox, a web backend framework for Dart, like Laravel. Source code: github.com/dartondox/dox.

Install

The easy way is the dox CLI. It makes a full starter project:

dart pub global activate dox 3.0.0-alpha.4
dox create my_app

To add the package to your own project:

dart pub add dox_core

Dox 3.x is in alpha (3.0.0-alpha.4). The API can still change before the stable release.

Use it

A full app with one route:

import 'package:dox_core/dox_core.dart';

class ApiRouter extends Router {
  @override
  String get prefix => 'api';

  @override
  void register() {
    Route.get('/ping', (DoxRequest req) => 'pong'); // GET /api/ping
  }
}

void main() async {
  Dox().initialize(AppConfig(
    appKey: Env.get('APP_KEY'), // read from .env
    serverPort: 3000,
    routers: <Router>[ApiRouter()],
  ));
  await Dox().startServer();
}

Run it with dart run bin/server.dart, then call curl localhost:3000/api/ping.

A controller can ask for a typed, checked input class (a DTO):

@Dto()
class CreateBlog {
  @Length(min: 3, max: 200)
  final String title;

  const CreateBlog({required this.title});
  static const DtoSchema<CreateBlog> schema = $CreateBlogDto;
}

class BlogController {
  Future<Blog> store(CreateBlog body) async { ... } // bad input gets 422
}

The DTO code is generated by dox_builder (dart run build_runner build).

Features

  • One isolate per CPU core. All isolates serve the same port.
  • A native HTTP server by default (dox_http_native). Set server: ServerBackend.dartIo to use dart:io.
  • Router with groups, middleware, typed path params and resource routes.
  • DTO input: Dox reads the body or query, checks it, and answers 422 with all errors.
  • Cache() (from dox_cache) and Storage() (from dox_storage).
  • OpenAPI docs at /docs (from dox_openapi). Off in production by default.
  • Optional Socket.IO on the same port (from dox_socketio).
  • Typed env class, CORS, cookies, hashing and encryption.

Documentation

License

MIT. See LICENSE.