Skip to content

iamdevmarcos/rust-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Http Server

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.

Features

  • 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.

Modules

main.rs

The entry point of the application:

  • Loads the server configuration.
  • Initializes and starts the server with the specified host and port.

config.rs

Defines server configuration:

  • Sets default values for the HOST and PORT.
  • Provides a load function to initialize configuration.

server.rs

Server setup:

  • Imports the home route and binds it to the server.
  • Parses the IP address and port to run the Warp server.

routes/home.rs

Defines a route for the home path (/):

  • Uses the hello_handler to respond with JSON data.

handlers/hello.rs

Handler for the home route:

  • Returns a JSON response with a "ping" message.

models/response.rs

Defines the data model for the JSON response:

  • Contains a single field, message, that will be serialized into JSON.

Running the Server

To start the server, run the following command:

cargo run

By default, the server will be available at http://127.0.0.1:8080.

API Endpoint

  • GET / - Returns a JSON message confirming the route is reachable.

Example Response

{
  "message": "ping to hello_handler"
}

Dependencies

  • Warp - For creating a lightweight, asynchronous web server.

  • Serde - For serializing the JSON response.

  • Preview

Screenshot 2024-10-08 at 21 34 30

About

Rust http server using Warp. Modular structure for easy scalability and configuration.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages