Hello world
Once you have Dart installed, it only takes a few lines of code to set up your Relic server. These are the steps you need to take to get a simple "Hello world" server up and running.
Create a Dart package
First, you need to create a new Dart package for your Relic server.
dart create -t console-full hello_world
Add the Relic dependency
Next, add the relic package as a dependency to your pubspec.yaml file.
cd hello_world
dart pub add relic
Edit the main file
Edit the bin/hello_world.dart:
import 'package:relic/io_adapter.dart';
import 'package:relic/relic.dart';
Future<void> main() async {
// Create a simple handler that responds to every request
final app = RelicApp()
..get(
'/**', (final ctx) => ctx.respond(
Response.ok(
body: Body.fromString('Hello world!'),
),
),
);
// Start the server on port 8080
await app.serve();
}
What this code does:
- Router:
RelicApp()is used to configure routing for the server. - Route:
.get('/', ...)handles GET requests to/and responds with "Hello, Relic!". - Server:
app.serve()binds the router (as a handler) to port 8080 on all network interfaces. - Logging: The server logs to the console when started.
The result is a server that responds with "Hello, Relic!" when you send a GET request to http://localhost:8080/.
Running locally
First, make sure you have Relic installed by following the installation guide.
Start your server with:
dart run bin/hello_world.dart
Then, open your browser and visit http://localhost:8080/, or use curl:
curl http://localhost:8080/
You should see:
Hello, Relic!
Congratulations! You just ran your first Relic server.