Relic 是一个轻量级且灵活的 Dart Web 服务器,受 Shelf 启发,专为构建 API 和后端服务而设计。它提供了一种简单、模块化的方式来处理 HTTP 请求和响应,非常适合希望在多个平台上实现高性能和易用性的开发者。支持 Android、iOS、macOS、Windows 和 Linux,Relic 可在 Flutter 应用程序或独立服务中实现无缝后端开发。其极小的占用空间和简洁的 API 使得快速原型设计和可扩展部署成为可能。
适合场景
构建 RESTful API
为Flutter应用创建后端服务
开发跨平台的服务器端逻辑
原型化微服务
处理 HTTP 请求路由
主要特点
轻量且快速
模块化中间件支持
跨平台兼容性
简单直观的 API
内置的请求/响应处理
适合对象
需要后端功能的Flutter开发人员
构建微服务的团队
独立开发者原型设计API
需要在多个平台上进行服务器端逻辑的项目
具有低延迟要求的应用程序
使用建议
在没有额外扩展的情况下,不适用于高并发生产负载
与完整框架相比,内置认证功能有限
需要手动设置以集成数据库
最适合中小型应用
核心包中没有内置的 WebSocket 支持
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Relic web server
Relic is a modern, high-performance web server framework inspired by Shelf and built by the Serverpod team to meet the demanding requirements of modern web applications. Relic provides a robust, type-safe, and efficient foundation for building scalable web services.
While Shelf has been a solid foundation for Dart web applications, several areas for improvement were identified. Rather than work around these limitations, Relic was created as a next-generation framework that maintains Shelf's familiar structure while delivering significant enhancements. These are some of the improvements:
Strongly typed APIs with validated HTTP headers.
Simplified interface for setting up routing and middleware.
Unified encoding model for HTTP bodies.
Built-in trie-based router, which improves performance.
Static file handling with support for cache busting and etags.
Built-in support for WebSockets.
Improvements in hot reload and running with multiple isolates.
Quick start
Install Dart 3.7 or newer and verify with dart --version.
Add Relic: dart pub add relic
Create a new console app and drop in the hello world server below.
Place this file in bin/hello_world.dart (runnable example is also under example/example.dart):
import'package:relic/io_adapter.dart';
import'package:relic/relic.dart';
/// A simple 'Hello World' server demonstrating basic Relic usage.Future<void> main() async {
// Setup the app.final app =
RelicApp()
// Route with parameters (:name & :age).
..get('/user/:name/age/:age', helloHandler)
// Middleware on all paths below '/'.
..use('/', logRequests())
// Custom fallback - optional (default is 404 Not Found).
..fallback = respondWith(
(_) => Response.notFound(
body: Body.fromString("Sorry, that doesn't compute.\n"),
),
);
// Start the server (defaults to using port 8080).await app.serve();
}
const _ageParam = PathParam<int>(#age, int.parse);
/// Handles requests to the hello endpoint with path parameters.ResponsehelloHandler(finalRequest req) {
final name = req.pathParameters.raw[#name];
final age = req.pathParameters.get(_ageParam);
returnResponse.ok(
body: Body.fromString('Hello, $name! To think you are $age years old.\n'),
);
}
Run it with dart run bin/hello_world.dart and visit http://localhost:8080/user/Nova/age/27.
Additional examples
Relic comes with an extensive set of examples. To run them, clone the Relic repository from GitHub and navigate to the example directory.
git clone https://github.com/serverpod/relic.git
cd relic/example
dart example.dart
Migrating from Shelf
Are you on Shelf? Moving over is straightforward. Relic's APIs are very similar while giving you typed requests, built-in routing, and WebSockets out of the box. Follow the step-by-step guide at docs.dartrelic.dev/getting-started/shelf-migration to upgrade your project in minutes.