This is a basic example of a server built with Rust using the Warp web framework. This server has a single route (/
) that responds with a JSON message.
- Uses the Warp framework for handling HTTP requests.
- Provides an endpoint (
/
) that returns a JSON response. - Structured using modules for configuration, routes, handlers, and models.
The entry point of the application:
- Loads the server configuration.
- Initializes and starts the server with the specified host and port.
Defines server configuration:
- Sets default values for the
HOST
andPORT
. - Provides a
load
function to initialize configuration.
Server setup:
- Imports the home route and binds it to the server.
- Parses the IP address and port to run the Warp server.
Defines a route for the home path (/
):
- Uses the
hello_handler
to respond with JSON data.
Handler for the home route:
- Returns a JSON response with a "ping" message.
Defines the data model for the JSON response:
- Contains a single field,
message
, that will be serialized into JSON.
To start the server, run the following command:
cargo run
By default, the server will be available at http://127.0.0.1:8080
.
- GET / - Returns a JSON message confirming the route is reachable.
{
"message": "ping to hello_handler"
}
-
Warp - For creating a lightweight, asynchronous web server.
-
Serde - For serializing the JSON response.
