#request-response #routes #base #basic #content #eqeqo

eqeqo_server_base

Library for creating HTTP server basic functionality

4 releases

0.1.3 Apr 19, 2024
0.1.2 Apr 17, 2024
0.1.1 Mar 30, 2024
0.1.0 Mar 28, 2024

#225 in HTTP server

Download history 334/week @ 2024-03-25 71/week @ 2024-04-01 2/week @ 2024-04-08 246/week @ 2024-04-15

653 downloads per month

MIT license

16KB
449 lines

eqeqo - server_base

Overview

Still under development. If you are interested, feel free to report issues or branch the repository.

Yet another HTTP server implementation. Not trying to reinvent the wheel, I just don´t get how to use other existing libs even though I tried, or they would add too many unwanted dependencies to my code.

The eqeqo server base library aims to provide a basic HTTP server implementation to make it easy to create minimal api servers avoiding external dependencies. It can be used to handle HTTP requests and static files. The server will handle requests according to the setup routes, and also static files even if the route is not defined.

  • Requests can be any HTTP request.
  • Routes are formed by the path, the HTTP request type, and the function to handle the request. The function must return a Response instance.
  • Responses type are identified by the content type and are returned as bytes to browsers.

Examples

Creating a simple server:

	use server_base::{ ServerBase, Rt, Request, Response, StatusCode };

	// Demo route handler
	fn demo_handle_test_get (_request: &Request) -> Response {
		return Response {
			status: StatusCode::Ok.to_string(),
			content_type: String::new(),
			content: "test-get".as_bytes().to_vec(),
		}
	}
	// Another demo route handler
	fn demo_handle_test_post (_request: &Request) -> Response {
		return Response {
			status: StatusCode::Ok.to_string(),
			content_type: String::new(),
			content: "test-post".as_bytes().to_vec(),
		}
	}

	fn main() {
		// Location to run, in this example is defined to local.
		let serving_url: &str  = "127.0.0.1:7878";
		// Define number of requests the server will be able to run at the same time.
		let threads_number: u8 = 10;
		// Create a new server
		let server = ServerBase::new(serving_url, threads_number, None).unwrap();
		// Define routes and http method allowed, and link them to request handlers defined previously.
		server.add_route("/test_route", Rt::GET, demo_handle_test_get);
		server.add_route("/test_route", Rt::POST, demo_handle_test_post);
		// Start serving
		server.run();
		// ...
		// Stop and kill the server
		server.stop();
	}

License

Copyright (c) 2024 fahedsl

MIT License

No runtime deps