Serverpod

Dart Framework

Serverpod Documentation

Overview

Serverpod is a full-stack framework for building scalable server-side applications in Dart. Alkimist provides complete support for Serverpod backends with automatic database setup and service configuration.

Project Structure

A typical Serverpod application structure:

text
server/
  lib/
    server.dart
  config/
    development.yaml

Health Check Endpoint

Serverpod applications must implement a /up endpoint. Here's how:

Basic Health Check Endpoint

dart
// server/lib/src/endpoints/health_endpoint.dart
import 'package:serverpod/serverpod.dart';

class HealthEndpoint extends Endpoint {
  @override
  Future<String> healthCheck(Session session) async {
    return 'ok';
  }
}

Register the endpoint in your server:

dart
// server/lib/server.dart
import 'package:serverpod/serverpod.dart';
import 'package:your_app_server/src/endpoints/health_endpoint.dart';

void run(List<String> args) async {
  final pod = Serverpod(
    args,
    Protocol(),
    Endpoints(),
  );

  // Register health check endpoint
  pod.router.get('/up', (request) async {
    return Response.ok('ok');
  });

  await pod.start();
}

Using Serverpod's Built-in Health Check

Serverpod provides a built-in health check mechanism:

dart
// server/lib/server.dart
import 'package:serverpod/serverpod.dart';

void run(List<String> args) async {
  final pod = Serverpod(
    args,
    Protocol(),
    Endpoints(),
  );

  // Add health check route
  pod.router.get('/up', (request) async {
    try {
      // Optional: Check database connection
      // await pod.database.connection.query('SELECT 1');
      return Response.ok('ok');
    } catch (e) {
      return Response.internalServerError(body: 'error');
    }
  });

  await pod.start();
}

Configuration

Your config/development.yaml should include:

yaml
server:
  publicHost: 0.0.0.0
  publicPort: 8080

Database

Alkimist automatically detects and configures your database from Serverpod's configuration files.

Environment Variables

Common Serverpod environment variables:

  • DATABASE_URL (PostgreSQL connection string)
  • REDIS_URL (if using Redis)
  • SERVERPOD_ENVIRONMENT=production

Example Project

Minimal Serverpod server with health check:

dart
// server/lib/server.dart
import 'package:serverpod/serverpod.dart';

void run(List<String> args) async {
  final pod = Serverpod(
    args,
    Protocol(),
    Endpoints(),
  );

  pod.router.get('/up', (request) async {
    return Response.ok('ok');
  });

  await pod.start();
}

The health check will be available at /up and return "ok" when the server is running.