Milstian - a Rust Internet Framework

Milstian Logo

In progress, primarily used for learning Rust programming.

This project is based on the programming exercise Building a multithreaded web server from the book The Rust Programming Language (no starch press 2018) and inspired by the Aomebo Web Framework for PHP.

Major goal

Goals

Development

Run local server

Parameters are: * TCP Hostname * TCP Port * Limit of workers * HTTP directory index file * HTTP web-server file-system root * HTTP file not found file * Maximum TCP request size

Example static TCP-HTTP application

``` rust extern crate milstian;

use milstian::{Application, Config};

fn main() { Application::tcphttpwithlegacyresponders(Config::from_env()); } ```

Example simple dynamic TCP-HTTP application

``` rust extern crate milstian;

use std::collections::HashMap; use std::net::SocketAddr;

extern crate milstianhttp; use milstianhttp::request; use milstian_http::response;

use milstian::response::tcp::http::ResponderInterface; use milstian::{Application, Config};

[derive(Clone)]

pub struct Responder { pub route: Option, }

impl Responder { pub fn new() -> Responder { Responder { route: None } } }

impl ResponderInterface for Responder { fn matches( &mut self, requestmessage: &request::Message, _config: &Config, _socket: &SocketAddr, ) -> bool { match requestmessage.requestline.queryarguments.get("test") { Some(value) => { self.route = Some(value.clone()); return true; } None => { return false; } } }

fn respond(
    &self,
    request_message: &request::Message,
    _config: &Config,
    _socket: &SocketAddr,
) -> Result<Vec<u8>, String> {
    if let Some(route) = &self.route {
        let protocol =
            request::Message::get_protocol_text(&request_message.request_line.protocol);
        let mut headers: HashMap<String, String> = HashMap::new();
        headers.insert("Content-Type".to_string(), "text/plain".to_string());
        return Ok(response::Message::new(
            protocol.to_string(),
            "200 OK".to_string(),
            headers,
            format!("Was here: {}", route).as_bytes().to_vec(),
        ).to_bytes());
    } else {
        Err("No result".to_string())
    }
}

}

fn main() { Application::tcphttpwithlegacyandcustomresponders( Config::from_env(), Box::new(Responder::new()), ); } ```

Docs

License

This project is under the GPLv3 license