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.