6 releases

0.1.0 Feb 19, 2024
0.0.6 Mar 2, 2024
0.0.5 Feb 25, 2024

#1046 in HTTP server

MIT license

15KB
274 lines

Arkyo

Arkyo is a simple HTTP server written in Rust, designed to handle incoming requests and provide appropriate responses based on defined routes and static file serving capabilities.

Features

  • HTTP Server: Arkyo listens for incoming TCP connections, parses HTTP requests, and dispatches them to corresponding handlers.
  • Routing: Define routes with specific HTTP methods and corresponding handler functions. Supports both static and dynamic routes.
  • Static File Serving: Serve static files from a specified folder.
  • Error Handling: Handle errors gracefully with appropriate HTTP status codes.
  • Multi-threaded: Arkyo is built to handle multiple incoming connections concurrently using multi-threading.
  • Middleware Support: Apply middleware functions to intercept and modify incoming requests or outgoing responses.

Roadmap

  • Static Content
  • Dynamic Routes
  • Middleware Support
  • Multiform Support

Usage

  1. Define Routes: Add routes to the server instance using the add_route method.
    server.add_route("/example", Method::Get, handler_function);
  1. Define Middlewares: Add Middlewares to the server isntance using add_middleware method.
    sever.add_middleware("/path", middleware_function)
  1. Set Static Folder: Optionally, set a folder from which to serve static files using static_folder method.
    server.static_folder("public");
  1. Start Server: Begin listening for incoming connections using listen method.
    server.listen();

Example

    use arkyo::prelude::*;

    fn main() {
        let mut server = Server::new();
    
        // Define routes
        server.add_route("/", Method::Get, index_handler);
        server.add_route("/about", Method::Get, about_handler);
        server.add_route("/user/:id", Method::Get, user_handler);
    
        // Set static folder
        server.static_folder("/static");
    
        // Start server
        server.listen("127.0.0.1:8080");
    }
    
    fn index_handler(_: Request) -> Response {
        Response::new()
            .status(Status::Ok)
            .body("Welcome to Arkyo!")
    }
    
    fn about_handler(_: Request) -> Response {
        Response::new()
            .status(Status::Ok)
            .body("Arkyo - A simple HTTP server written in Rust.")
    }
    
    fn user_handler(req: Request) -> Response {
        let user_id = req.params.get("id").unwrap();
    
        Response::new()
            .status(Status::Ok)
            .body(format!("User profile page for user {}", user_id))
    }

Installation

Add arkyo to your Cargo.toml dependencies:

    [dependencies]
    arkyo = "0.0.6"

Contributing

  • Found a bug? Please open an issue.
  • Want to contribute? Fork the repository and submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Arkyo - © 2024 DhavyLTS

Dependencies

~1MB
~18K SLoC