Getting Started
This guide will help you get started with Ngyn, a modern, async-first web framework for Rust. We'll walk through setting up a new project, creating routes, and running your first Ngyn application.
Prerequisites
Before you begin, make sure you have the following installed:
Creating a New Project
Let's create a new Rust project:
cargo new my_ngyn_app
cd my_ngyn_app
Adding Ngyn Dependencies
Add Ngyn to your Cargo.toml file:
[dependencies]
ngyn = "0.5"
tokio = { version = "1", features = ["full"] }
Creating Your First Application
Replace the contents of src/main.rs with the following code:
use ngyn::prelude::*;
#[handler]
fn hello() -> &'static str {
"Hello, Ngyn!"
}
#[tokio::main]
async fn main() {
let mut app = HyperApplication::default();
// Register a route
app.get("/", hello);
println!("Server running at http://127.0.0.1:3000");
let _ = app.listen("127.0.0.1:3000").await;
}
Running Your Application
Run your application with:
cargo run
You should see the message "Server running at http://127.0.0.1:3000". Open your browser and navigate to http://127.0.0.1:3000 to see "Hello, Ngyn!".
Understanding the Code
Let's break down what's happening in our application:
-
Imports: We import everything we need from the Ngyn prelude.
-
Route Handler: We define a function
hellothat returns a static string. The#[handler]attribute marks this function as a route handler. -
Application Setup: We create a new Hyper-based application with
HyperApplication::default(). -
Route Registration: We register the
hellofunction to handle GET requests to the root path (/). -
Server Start: We start the server on localhost port 3000.
Next Steps
Now that you have a basic Ngyn application running, you can explore more features:
- Routing - Learn about different routing patterns
- Middleware - Add middleware to your application
- Forms Handling - Process form submissions
- Access Gates - Control access to your routes
Or check out the examples in the Ngyn repository for more complex applications.