Skip to content

Implement endpoint initialization on server start #4146

Open

Description

@coderabbitai

Description

This issue tracks the need to implement endpoint initialization on server start to catch configuration errors early.

PR #4139 moved the endpoint initialization call from the Serverpod constructor to the server start sequence. This issue extends that work to ensure endpoints are initialized at the appropriate time based on server mode.

The main goal is to throw errors if the server is started with missing configuration for an endpoint. This is achieved by declaring required dependencies as final fields in the endpoint class, which will be evaluated when the endpoint is initialized.

Adding required configuration to an endpoint is done by declaring it as final such as:

class MyEndpoint extends Endpoint {
  final MyService service = MyServiceLocator.instance.getService;
}

This would make a call to fetch the service as soon as the endpoint is initialized, causing the server to fail fast if the service is not properly configured.

Requirements

Monolith Mode

When the server is started in monolith mode, ALL endpoints should be eagerly initialized on server start. This ensures that any configuration issues are detected immediately when the server starts, rather than when the first request arrives.

Serverless Mode

When the server is started in serverless mode, all endpoints should be lazily initialized when accessed. This allows for faster cold starts and only requires configuration for endpoints that are actually invoked.

Test Framework

As a side requirement, the test framework should also support lazy endpoint initialization so that tests only need configuration for the specific endpoints being tested.

Context

This feature is not critical for the current release but should be addressed in a future update.

Tasks

  • Implement eager endpoint initialization for monolith mode (initialize all endpoints on server start)
  • Implement lazy endpoint initialization for serverless mode (initialize endpoints when accessed)
  • Implement lazy endpoint initialization for test framework
  • Ensure proper error handling and reporting for configuration failures
  • Add tests for both initialization modes

Activity

  1. added
    area: runtime
    enhancement
    ux
    on Nov 3, 2025
  2. removed their assignment
    on Nov 3, 2025
  3. added this to the Post 3.0 release milestone on Nov 3, 2025
  4. changed the title [-]Implement lazy endpoint initialization in test framework[/-] [+]Implement lazy endpoint initialization[/+] on Nov 6, 2025
  5. SandPod commented on Nov 6, 2025

    @SandPod
    Contributor

    @coderabbitai can you update this issue so that it reflect implementing endpoint initialization on server start in general? We want to use this to throw errors if the server is started with missing configuration for an endpoint (final field as explained in the example of the issue).

    Adding lazy endpoint initialization in the test framework is just a side requirement of the real issue.

    When the server is started in serverless mode, all endpoints should be lazy initialized, when the server is started in monolith mode, ALL endpoints should be initialized on server start.

  6. changed the title [-]Implement lazy endpoint initialization[/-] [+]Implement endpoint initialization on server start[/+] on Nov 6, 2025
  7. moved this from Inbox 📫 to Backlog 🗂️ in Serverpod Frameworkson Nov 6, 2025
  8. coderabbitai commented on Nov 6, 2025

    @coderabbitai
    ContributorAuthor

    Tip

    For best results, initiate chat on the files or code changes.

    🧩 Analysis chain

    🏁 Script executed:

    #!/bin/bash
    # Read the current issue to see its full content
    gh issue view 4146 --repo serverpod/serverpod --json title,body
    

    Length of output: 1827


    🏁 Script executed:

    #!/bin/bash
    # Update the issue with corrected title and body
    
    gh issue edit 4146 --repo serverpod/serverpod \
      --title "Implement endpoint initialization on server start" \
      --body "## Description
    
    This issue tracks the need to implement endpoint initialization on server start to catch configuration errors early.
    
    PR #4139 moved the endpoint initialization call from the Serverpod constructor to the server start sequence. This issue extends that work to ensure endpoints are initialized at the appropriate time based on server mode.
    
    The main goal is to **throw errors if the server is started with missing configuration** for an endpoint. This is achieved by declaring required dependencies as \`final\` fields in the endpoint class, which will be evaluated when the endpoint is initialized.
    
    Adding required configuration to an endpoint is done by declaring it as \`final\` such as:
    
    \`\`\`dart
    class MyEndpoint extends Endpoint {
      final MyService service = MyServiceLocator.instance.getService;
    }
    \`\`\`
    
    This would make a call to fetch the service as soon as the endpoint is initialized, causing the server to fail fast if the service is not properly configured.
    
    ## Requirements
    
    ### Monolith Mode
    When the server is started in **monolith mode**, ALL endpoints should be **eagerly initialized on server start**. This ensures that any configuration issues are detected immediately when the server starts, rather than when the first request arrives.
    
    ### Serverless Mode
    When the server is started in **serverless mode**, all endpoints should be **lazily initialized** when accessed. This allows for faster cold starts and only requires configuration for endpoints that are actually invoked.
    
    ### Test Framework
    As a side requirement, the test framework should also support **lazy endpoint initialization** so that tests only need configuration for the specific endpoints being tested.
    
    ## Context
    
    - **Related PR:** https://github.com/serverpod/serverpod/pull/4139
    - **Comment:** https://github.com/serverpod/serverpod/pull/4139#issuecomment
    - **Requested by:** @SandPod
    
    This feature is not critical for the current release but should be addressed in a future update.
    
    ## Tasks
    
    - [ ] Implement eager endpoint initialization for monolith mode (initialize all endpoints on server start)
    - [ ] Implement lazy endpoint initialization for serverless mode (initialize endpoints when accessed)
    - [ ] Implement lazy endpoint initialization for test framework
    - [ ] Ensure proper error handling and reporting for configuration failures
    - [ ] Add tests for both initialization modes"
    

    Length of output: 50


    @SandPod, I've updated issue #4146 to reflect the correct priorities. Here's what changed:

    Title: "Implement endpoint initialization on server start"

    Key updates:

    • Main focus: Implementing endpoint initialization on server start to throw errors if the server starts with missing configuration (using final fields as shown in the example)
    • Monolith mode: ALL endpoints should be eagerly initialized on server start to detect configuration issues immediately
    • Serverless mode: All endpoints should be lazily initialized when accessed for faster cold starts
    • Test framework: Lazy initialization is now listed as a side requirement

    Tasks updated to:

    • Implement eager endpoint initialization for monolith mode
    • Implement lazy endpoint initialization for serverless mode
    • Implement lazy endpoint initialization for test framework
    • Ensure proper error handling and reporting for configuration failures
    • Add tests for both initialization modes

    The issue now correctly reflects that the primary goal is fail-fast configuration validation in monolith mode, with lazy initialization in serverless/test modes as supporting requirements.


    🧠 Learnings used
    Learnt from: SandPod
    Repo: serverpod/serverpod PR: 4127
    File: modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/lib/src/providers/passkey/endpoints/passkey_account_base_endpoint.dart:11-16
    Timestamp: 2025-10-30T10:11:34.781Z
    Learning: In Serverpod authentication endpoints, SandPod prefers eager initialization of AuthConfig-dependent fields (e.g., `final TokenManager tokenManager = AuthConfig.instance.tokenManager;`) rather than lazy getters, to detect misconfiguration at server startup rather than deferring errors until the first request to the endpoint.
    

    Learnt from: SandPod
    Repo: serverpod/serverpod PR: 4144
    File: modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/lib/src/providers/passkey/endpoints/passkey_idp_base_endpoint.dart:0-0
    Timestamp: 2025-11-03T15:40:41.827Z
    Learning: In Serverpod authentication IDP endpoint files (modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server), endpoint base classes should import `serverpod_auth_idp_server/core.dart` to access the Session type, rather than importing `package:serverpod_auth_core_server/session.dart` directly. This pattern is consistently used in Apple, Email, and Google endpoint files.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: runtimeenhancementux

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions