2 releases

new 0.2.1 May 9, 2025
0.2.0 May 4, 2025
0.1.2 May 30, 2024
0.1.1 May 30, 2024
0.1.0 May 30, 2024

#1001 in HTTP server

Download history 110/week @ 2025-04-30

110 downloads per month

MIT/Apache

24KB
478 lines

NOXP


🦀 NOXP is a simple web framework for rust inspired by golang's net/http package

NOXP uses only the standard library

🚧 What's next:

  • Use the requests for some useful purpose
  • Better routing system
  • All status codes
  • All http methods
  • Headers!
  • Query strings support
  • Dynamic routing
  • Add middleware
  • Fix middleware
  • Authentication middleware
  • Add documentation
  • Publish to crates.io

Usage

use noxp::Server;
use noxp::http::{ Response, Request, StatusCode };

use std::fmt;
use std::sync::Arc;

struct Person {
  name: String,
  age: i32,
}

impl fmt::Display for Person {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{{ \"name\": \"{}\", \"age\": {} }}", self.name, self.age)
  }
}

fn main() -> std::io::Result<()> {
  // create the server
  let mut server = Server::new();

  // pay attention for the tuple (Method, &str)
  server.handle_func(("GET", "/"), Arc::new(index));
  server.handle_func(("POST", "/"), Arc::new(post));

  // you can also send html (only in the views folder)
  server.handle_func(("GET", "/hello"), Arc::new(file));

  // and send json (only structs which implement Display)
  server.handle_func(("GET", "/person"), Arc::new(json));

  // listening at localhost:8080
  server.listen_and_serve("localhost:8080")
}

fn index(_req: Request, res: &mut Response) {
  res.set_status(StatusCode::OK);
  res.set_text("Hello, World!");
}

fn post(req: Request, res: &mut Response) {
  req.print_body();

  res.set_status(StatusCode::OK);
  res.set_json(req.get_body());
}

fn file(_req: Request, res: &mut Response) {
  res.set_status(StatusCode::OK);
  res.set_html("hello.html");
}

fn json(_req: Request, res: &mut Response) {
  let person = Person {
    name: String::from("Menezes"),
    age: 16,
  };

  res.set_status(StatusCode::OK);
  res.set_json(person);
}

No runtime deps