Skip to content

Commit

Permalink
Release v0.1.11 (#15)
Browse files Browse the repository at this point in the history
* Multi Threading Implementation (#14)

* Add dyn-clone

* Update RouteHandler, Middleware, Endpoint and Router to implement Clone trait

* Upadate server.rs to separate Server into Server and Listener, Add multi-threading

* Update middlewares to implement Clone trait, Update doctests

* Update server.rs to bring back listen function for Server, Fix doctests

* Update readme and project version

* Upadte rustfmt to remove unstable features, Format whole codebase
  • Loading branch information
emrecancorapci authored Sep 4, 2024
1 parent 3686d94 commit 0bcdbd7
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 98 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "krustie"
version = "0.1.10"
version = "0.1.11"
description = "Krustie is a backend library written in Rust. Currently, it is a work in progress and not yet ready for production use."
categories = ["network-programming", "web-programming::http-server"]
keywords = ["http", "web", "framework"]
Expand All @@ -11,6 +11,7 @@ license = "MIT"
edition = "2021"

[dependencies]
dyn-clone = "1.0.17"
flate2 = "1.0.33"
regex = "1.10.6"
serde_json = "1.0.127"
Expand All @@ -30,4 +31,4 @@ debug = false
panic = "unwind"
lto = true
codegen-units = 1
strip = true
strip = true
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-krustie-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/krustie)
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/emrecancorapci/krustie/rust.yml?branch=main&style=for-the-badge" height="20">](https://github.com/emrecancorapci/krustie/actions?query=branch%3main)

Krustie is a backend library written in Rust. It is currently a work in progress and not yet ready for production use. This project serves as a personal learning experience, and contributions or feedback are welcome.
Krustie is a backend library written in Rust. It is currently a work in progress and not yet ready for production use. This project serves as a personal learning experience, and contributions or feedbacks are welcome.

## Features

- Stackable Router with parameter and query support
- Middleware support
- Middleware support for routers and endpoints
- Multi-threaded server
- JSON parsing ([serde_json](https://crates.io/crates/serde_json))

### Builtin Middlewares
Expand All @@ -22,7 +23,7 @@ Krustie is a backend library written in Rust. It is currently a work in progress
## Start your server

```rust
use krustie::{ Router, Server, StatusCode };
use krustie::{ Router, Server, Listener, StatusCode };

fn main() {
let mut server = Server::create();
Expand All @@ -34,10 +35,10 @@ fn main() {

server.use_handler(router);

server.listen((127, 0, 0, 1), 8080);
server.listen(8080);
}
```

## Contributing

As an inexperienced developer contributions will be welcomed. Please open an issue or a pull request.
All contributions are welcomed. Please open an issue or a pull request to report a bug or request a feature.
54 changes: 2 additions & 52 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
indent_style = "Block"
use_small_heuristics = "Default"
fn_call_width = 60
attr_fn_like_width = 70
Expand All @@ -12,66 +11,17 @@ array_width = 60
chain_width = 60
single_line_if_else_max_width = 50
single_line_let_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
skip_macro_invocations = []
hex_literal_case = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
imports_granularity = "Preserve"
group_imports = "Preserve"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_params_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2015"
version = "One"
inline_attribute_width = 0
format_generated_files = true
edition = "2021"
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
unstable_features = false
disable_all_formatting = false
skip_children = false
show_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
ignore = []
emit_mode = "Files"
make_backup = false

15 changes: 12 additions & 3 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//!
//! It takes itself as `&self`, a `Request` and a `Response` as arguments and returns a `HandlerResult`.
use dyn_clone::DynClone;
use std::fmt::Debug;

use crate::{
Expand All @@ -31,7 +32,7 @@ pub use self::{gzip::GzipEncoder, rate_limiter::RateLimiter, statics::ServeStati
/// ```rust
/// use krustie::{ Request, Response, Middleware, server::route_handler::HandlerResult };
///
/// #[derive(Debug)]
/// #[derive(Clone)]
/// struct AddKrustieHeader;
///
/// impl AddKrustieHeader {
Expand All @@ -53,7 +54,7 @@ pub use self::{gzip::GzipEncoder, rate_limiter::RateLimiter, statics::ServeStati
/// ```rust
/// use krustie::{ Request, Response, Middleware, server::route_handler::HandlerResult };
///
/// #[derive(Debug)]
/// #[derive(Clone)]
/// struct AddHeader {
/// server: String,
/// value: String,
Expand All @@ -73,7 +74,7 @@ pub use self::{gzip::GzipEncoder, rate_limiter::RateLimiter, statics::ServeStati
/// }
/// ```
///
pub trait Middleware: Debug {
pub trait Middleware: DynClone + Send {
/// Middleware function to be implemented for the middleware.
///
/// For the middleware to be executed and continue the execution, it should return `HandlerResult::Next`.
Expand All @@ -90,3 +91,11 @@ where
T::middleware(self, request, response)
}
}

impl Debug for dyn Middleware {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Middleware",)
}
}

dyn_clone::clone_trait_object!(Middleware);
2 changes: 1 addition & 1 deletion src/middleware/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{request::Request, response::Response, server::route_handler::Handler
///
/// server.use_handler(GzipEncoder);
///
#[derive(Debug)]
#[derive(Clone)]
pub struct GzipEncoder;

impl GzipEncoder {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{server::route_handler::HandlerResult, Middleware, StatusCode};
/// A rate limiter middleware
///
/// Limits the number of requests from an IP address based on the token number and token refill time.
#[derive(Debug)]
#[derive(Clone)]
pub struct RateLimiter {
token_number: u16,
token_refill_duration: Duration,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
///
/// server.use_handler(statics);
/// ```
#[derive(Debug)]
#[derive(Clone)]
pub struct ServeStatic {
folder_path: String,
}
Expand Down
19 changes: 19 additions & 0 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ impl RouteHandler for Router {
}
}

impl Clone for Router {
fn clone(&self) -> Self {
let endpoints: Vec<Endpoint> = self.endpoints.clone();
let middlewares: Vec<Box<dyn Middleware>> = self.middlewares.clone();
let subdirs: HashMap<String, Box<Router>> = self.subdirs.clone();
let param_dir: Option<(String, Box<Router>)> = match &self.param_dir {
Some((key, router)) => Some((key.clone(), router.clone())),
None => None,
};

Self {
endpoints,
middlewares,
subdirs,
param_dir,
}
}
}

#[derive(Eq, Hash, PartialEq, Debug)]
enum PathType {
Subdirectory(String),
Expand Down
12 changes: 11 additions & 1 deletion src/router/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::Controller;
///
/// server.use_handler(router);
///
/// // server.listen((127, 0, 0, 1), 8080);
/// // server.listen(8080);
/// ```
#[derive(Debug)]
pub struct Endpoint {
Expand Down Expand Up @@ -81,3 +81,13 @@ impl Endpoint {
&mut self.middlewares
}
}

impl Clone for Endpoint {
fn clone(&self) -> Self {
Self {
method: self.method.clone(),
controller: self.controller.clone(),
middlewares: self.middlewares.clone(),
}
}
}
Loading

0 comments on commit 0bcdbd7

Please sign in to comment.