Volga
Home
API Docs
GitHub
  • English
  • Русский
Home
API Docs
GitHub
  • English
  • Русский
  • Home
  • Basics

    • Quick Start
    • Route Parameters
    • Query Parameters
    • Route Groups
    • Headers
  • Data Formats

    • Handling JSON
    • Handling Form Data
    • Working with Files
    • Server-Sent Events (SSE)
  • Protocols

    • HTTP/1 and HTTP/2
    • HTTPS
    • WebSockets & WebTransport
  • Advanced

    • Custom Middleware
    • Response Compression
    • Request Decompression
    • Global Error Handling
    • Dependency Injection
    • Tracing & Logging
    • Static Files
    • CORS (Cross-Origin Resource Sharing)
    • Cookies
    • Request cancellation
    • Custom Handling of HEAD, OPTIONS, and TRACE Methods

Quick Start

Build a basic "Hello, World" Web API using Volga.

Prerequisites

Install Rust

If you haven't installed Rust yet, it is recommended to use the rustup. Here is the official guide where you can find how to do it.

Volga currently has a minimum supported Rust version (MSRV) of 1.80. Running rustup update will ensure you have the latest Rust version available.

Create an app

Create a new binary-based app:

cargo new hello-world
cd hello-world

Add the following dependencies in your Cargo.toml:

[dependencies]
volga = "0.5.8"
tokio = { version = "1", features = ["full"] }

Setup

Create your main application in main.rs:

use volga::{App, ok};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    // Configure the server
    let mut app = App::new();

    // Example of simple GET request handler
    app.map_get("/hello", || async {
        ok!("Hello, World!")
    });
    
    // Run the server
    app.run().await
}

Detailed Walkthrough

When the App struct is instantiated, it represents your API and by default binds it to http://localhost:7878:

let mut app = App::new();

Or if you need to bind it to another socket, you can use the bind() method like this:

// Binds the server to http://localhost:5000
let mut app = App::new().bind("localhost:5000");

Next, map a specific handler to a route. For instance, mapping our handler to GET /hello:

app.map_get("/hello", || {
    ok!("Hello, World!")
});

Ensure routes are mapped before you start the server with:

app.run().await

Testing the API

You can test your API using the curl command:

> curl -v "http://localhost:7878/hello"

Response expected:

* Host localhost:7878 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:7878...
* Connected to localhost (::1) port 7878
> GET /hello HTTP/1.1
> Host: localhost:7878
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< date: Sun, 6 Oct 2024 08:22:17 +0000
< server: Volga
< content-length: 12
< content-type: text/plain
<
* Connection #0 to host localhost left intact
Hello, World!

You can also check out the full example here

Last Updated:
Next
Route Parameters