Class CustomHandlerServer

java.lang.Object
com.fortemate.dicechess.runtime.CustomHandlerServer

public final class CustomHandlerServer extends Object
A minimal HTTP server for the Azure Functions custom-handler model: one path, one WebhookHandler, no framework.

Azure starts the custom-handler process and tells it which port to listen on via the FUNCTIONS_CUSTOMHANDLER_PORT environment variable; startFromEnvironment(WebhookHandler) reads it. Nothing here is Azure-specific beyond that one variable — the same server runs identically under func start locally, or under any other host that can point HTTP traffic at a port.

  • Field Details

    • ENV_BOT_TOKEN

      public static final String ENV_BOT_TOKEN
      Environment variable name holding the bot bearer token.
      See Also:
    • ENV_PLAY_API_BASE_URL

      public static final String ENV_PLAY_API_BASE_URL
      Environment variable name holding play-api's base URL.
      See Also:
    • ENV_DRAIN_DEADLINE_SECONDS

      public static final String ENV_DRAIN_DEADLINE_SECONDS
      Environment variable name holding the drain deadline in seconds.
      See Also:
  • Method Details

    • startFromEnvironment

      public static HttpServer startFromEnvironment(WebhookHandler handler) throws IOException
      Starts the server on the port named by FUNCTIONS_CUSTOMHANDLER_PORT (default 8080), serving handler at /api/webhook, and attaches a SIGTERM shutdown hook using environment variables.
      Parameters:
      handler - the webhook logic to serve
      Returns:
      the running server; call HttpServer.stop(int) to shut it down
      Throws:
      IOException - if the port cannot be bound
    • start

      public static HttpServer start(int port, String path, WebhookHandler handler) throws IOException
      Starts the server on an explicit port and path.
      Parameters:
      port - the port to listen on
      path - the path handler answers on
      handler - the webhook logic to serve
      Returns:
      the running server; call HttpServer.stop(int) to shut it down
      Throws:
      IOException - if the port cannot be bound
    • start

      public static HttpServer start(int port, String path, WebhookHandler handler, AtomicBoolean isDraining) throws IOException
      Starts the server on an explicit port and path with a shared drain flag.
      Parameters:
      port - the port to listen on
      path - the path handler answers on
      handler - the webhook logic to serve
      isDraining - the flag indicating whether drain mode is active
      Returns:
      the running server; call HttpServer.stop(int) to shut it down
      Throws:
      IOException - if the port cannot be bound
    • executeShutdown

      public static CustomHandlerServer.ShutdownOutcome executeShutdown(HttpServer server, AtomicBoolean isDraining, String token, String baseUrl, long drainDeadlineSeconds)
      Executes the shutdown procedure and reports which path was taken.

      With a token, POST /bot/games/resign-all concedes every game the bot is seated in and pauses its seating, which is the only mechanism that reaches a game whose turn is not currently the bot's. A refused or unreachable call falls through to drain mode rather than exiting as if the games had been conceded: an expired token answers 401, and silently treating that as success leaves the bot's games running until they flag.

      Drain mode answers every further delivery with a resignation until drainDeadlineSeconds elapses. It is best-effort by nature: a game only receives a delivery when it is the bot's turn.

      This method never calls System.exit. It is invoked from a shutdown hook, and Runtime.exit called while the shutdown sequence is already running blocks that thread indefinitely, so the JVM would hang until the orchestrator's SIGKILL instead of stopping cleanly.

      Parameters:
      server - the server to stop
      isDraining - the flag controlling drain mode
      token - the bot token, or null/blank if unconfigured
      baseUrl - play-api base URL
      drainDeadlineSeconds - drain mode timeout in seconds
      Returns:
      which path the shutdown actually took
    • attachShutdownHook

      public static Thread attachShutdownHook(HttpServer server, AtomicBoolean isDraining, String token, String baseUrl, long drainDeadlineSeconds)
      Attaches a JVM shutdown hook that runs executeShutdown(HttpServer, AtomicBoolean, String, String, long) on SIGTERM.
      Parameters:
      server - the server to stop
      isDraining - the flag controlling drain mode
      token - the bot token, or null/blank if unconfigured
      baseUrl - play-api base URL
      drainDeadlineSeconds - drain mode timeout in seconds
      Returns:
      the attached shutdown hook thread