I made my own simple web framework in Rust

February 7th 2024 988 views • 4 comments
tej 4 months ago

I tried to create a simple embed HTTP server in Rust using Actix Web for the Desktop Tauri app. However, I found a lot of problems and unable to perform a simple file upload operation and have Websocket without extra dependencies. None of the stackoverflow solution worked for me.

May be I am a Django Guy and found difficult to use Actix Web which may be my skill issue. So I thought to create my own web framework in Rust for embed projects.

Here's github link: https://github.com/tejmagar/rusty-web/

It is same like a Django's functional approach.

use rusty_web::paths::{Path, Paths};
use rusty_web::request::Request;
use rusty_web::response::Response;
use rusty_web::server::run_server;

fn home(request: Request, mut response: Response) {
    response.html(200, "Home Page".to_string()).send();
}

fn about(request: Request, mut response: Response) {
    response.html(200, "About Us".to_string()).send();
}

fn main() {
    let paths: Paths = vec![
        Path::new("/".to_string(), home),
        Path::new("/about/".to_string(), about),
    ];

    run_server("0.0.0.0:8080", paths);
}

I will be using this for my personal projects. Leave your suggestions to make this project better.

codie 4 months ago

Awesome work brother.

codie 4 months ago

I tried this

fn home(request: Request, mut response: Response) {
    response.json(200, "{'hey': 2}".to_string()).send();
}

Curious, how to you do JSON ?

tej 4 months ago

Currently, JSON asString only supported. What do you suggest? to support serde crate for JSON by default instead of String?

codie 4 months ago

Use struct.

struct XYZStruct {
   var_1: String
   var_2: String
   var_3: AnotherStruct
}

struct AnotherStruct {}

and something like this:

fn about(request: Request, mut response: Response) {
    response.json(Status::Ok, XYZStruct::new(...)).send();
}