http_server_tiny

Crates.iohttp_server_tiny
lib.rshttp_server_tiny
version0.2.1
sourcesrc
created_at2023-03-08 12:09:44.922081
updated_at2023-03-15 15:19:30.545435
descriptionHTTP SERVER
homepage
repositoryhttps://github.com/HakanVardarr/http_server_tiny
max_upload_size
id804497
size11,065
Hakan Vardar (HakanVardarr)

documentation

README

http_server_tiny

Tiny http server library using tiny http

Usage:

You need to provide a error.html file to use when server return 404 error.

Route Macro (Experimental)!!!

This macro is usefull for deploying static files.

Example

route!(get_html => server, "/", ".index.html");

This macro is equal to this

server.add_route(
  &Method::Get,
    "/",
    Box::new(|_| Res::File {
        name: "./index.html",
        ct: "text/html; charset=utf-8",
        sc: 200,
  }),
);

src/main.rs

use http_server_tiny::{route, HttpServer, Method, Res};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut server = HttpServer::new("0.0.0.0:8000", "./error.html");
    route!(get_html => server, "/", ".index.html");
    route!(get_js => server, "/index.js", ".index.js");

    server.handle_requests(Box::new(|req| {
        println!("INFO: {} {}", req.method, req.url);
    }))
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello, World</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
  <script src="index.js"></script>
</html>

index.js

console.log("Hello, world!");

error.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>404</title>
  </head>
  <body>
    404
  </body>
</html>
Commit count: 21

cargo fmt