𝗖𝗿𝗲𝗮𝘁𝗶𝗻𝗴 𝗮𝗻𝗱 𝗥𝘂𝗻𝗻𝗶𝗻𝗴 𝗮 𝗦𝗲𝗿𝘃𝗲𝗿

A server is the heart of your web application. Hyperlane makes it easy to build and manage them. You can choose between speed and control based on your needs.

Three ways to build a server:

• Use Server::default() for quick setup. This is best for testing or simple apps. It uses preset settings for addresses and limits. • Use ServerConfig for total control. This lets you change the bind address, TCP options, or TTL. • Use RequestConfig to manage request details. This is best for adjusting buffer sizes and timeouts.

Managing the lifecycle:

Once you start a server with the run() method, you get a ServerControlHook. This tool gives you two main options:

  • wait(): This keeps the server running until you stop it. Use this at the end of your main function to prevent the program from exiting.
  • shutdown(): This stops the server gracefully. It finishes current requests before closing. This prevents data loss in production.

Advanced options:

Hyperlane uses attribute macros to simplify code. You can inject server variables directly into your functions. This makes your setup cleaner and more declarative.

You can also run multiple servers at once. Use Tokio to run different apps on different ports. This works well for:

  • Running HTTP and HTTPS on separate ports.
  • Serving different API versions.
  • Hosting microservices in one process.
  • Separating main apps from health checks.

Always handle errors when starting a server. Common issues include occupied ports or permission errors. Check your results to ensure your application stays stable.

Project Code: https://github.com/hyperlane-dev/hyperlane Source: https://dev.to/tengxgfyrz67s/creating-and-running-a-server-5ama