synw / isohttpd

A lightweight http server that runs in an isolate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Isohttpd

pub package

A lightweight http server that runs in an isolate. Powered by Iso

Example

import 'dart:io';
import 'dart:async';
import 'package:isohttpd/isohttpd.dart';

Future<HttpResponse> handler(HttpRequest request, IsoLogger log) async {
  return jsonResponse(request, {"response": "ok"});
}

void main() async {
  // set routes
  final defaultRoute = IsoRoute(path: "*", handler: handler);
  final routes = <IsoRoute>[defaultRoute];
  final router = IsoRouter(routes);

  // run
  final iso = IsoHttpdRunner(host: "localhost", router: router);
  await iso.run(verbose: true);

  // listen to logs
  iso.logs.listen((String data) => print("$data"));
iso.requestLogs.listen((ServerRequestLog data) => print("=> $data"));
  // idle
  final waiter = Completer<Null>();
  await waiter.future;
}

Commands

Start the server:

await iso.run(startServer: false);
iso.start();

Stop the server:

iso.stop();

Server status:

iso.status();

Utils

Send a json response from a handler:

jsonResponse(request, {"response": "ok"});

Decode multipart/form-data:

final body = await decodeMultipartRequest(request);
final dynamic data = body.data;

List a directory's content:

final data = await directoryListing(Directory(somePath));

About

A lightweight http server that runs in an isolate

License:MIT License


Languages

Language:Dart 100.0%