Skip to content

Commit

Permalink
create Context Struct and update Cargo.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Bonill authored and Sam Bonill committed Oct 10, 2022
1 parent 6e5090c commit c70fabd
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 17 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ name = "graphul"
version = "0.1.0"
edition = "2021"
license = "MIT"
categories = ["asynchronous", "network-programming", "web-programming::http-server"]
keywords = ["http", "web", "framework"]
description = "Optimize, speed, scale your microservices and save money 💵"
homepage = "https://github.com/rustful-rs/rustful"
documentation = "https://github.com/rustful-rs/rustful/blob/main/README.md"
repository = "https://github.com/rustful-rs/rustful"
homepage = "https://github.com/graphul-rs/graphul"
documentation = "https://github.com/graphul-rs/graphul/blob/main/README.md"
repository = "https://github.com/graphul-rs/graphul"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ async fn main() {
}
```

## Context

```rust
use graphul::{Graphul, Context, http::Methods };


#[tokio::main]
async fn main() {
let mut app = Graphul::new();

// /samuel?country=Colombia
app.get("/:name", |c: Context| async move {

let name = c.params("name");
let country = c.query("country");

format!("My name is {}, I'm from {}", name, country)
});

app.run("127.0.0.1:8000").await;

}
```

## JSON

```rust
Expand Down
1 change: 1 addition & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ use crate::Body;
pub type Request = axum::http::Request<Body>;

pub type StatusCode = hyper::StatusCode;

75 changes: 61 additions & 14 deletions src/http/context.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
use super::response::{Response};
use std::{collections::HashMap};

use async_trait::async_trait;
use axum::extract::{FromRequest, rejection::{QueryRejection, PathRejection}, RequestParts, Query, Path};

use crate::Body;



type HashMapRequest = HashMap<String, String>;

#[derive(Debug)]
pub struct Context {
params_map: HashMapRequest,
query_map: HashMapRequest,
}

// get context from request, in progressss......
impl Context {
pub fn new() -> Self {
Self {
pub fn params(&self, key: &'static str) -> String {
match self.params_map.get(key) {
Some(value) => value.clone(),
None => String::new()
}
}
// part of request
pub fn args(&self, _name: &str) -> Option<String> {
None
pub fn all_params(&self) -> HashMapRequest {
self.params_map.clone()
}
pub fn params(&self, _name: &str) -> Option<&str> {
None
pub fn query(&self, key: &'static str) -> String {
match self.query_map.get(key) {
Some(value) => value.clone(),
None => String::new()
}
}
// part of response
pub fn send(&self, _body: &'static str) -> Response {
todo!()
pub fn all_query(&self) -> HashMapRequest {
self.query_map.clone()
}
pub fn json(&self, _body: serde_json::Value) -> Response {
todo!()
}

#[async_trait]
impl FromRequest<Body> for Context {
type Rejection = QueryRejection;

async fn from_request(req: &mut RequestParts<Body>) -> Result<Self, Self::Rejection> {
let mut query_map: HashMapRequest = HashMap::new();
let mut params_map: HashMapRequest = HashMap::new();
// get query
let result_query : Result<Query<HashMapRequest>, QueryRejection> = Query::from_request(req).await;
match result_query {
Ok(query) => {
match query {
Query(parse_query) => {
query_map = parse_query;
}
}
},
Err(_) => {}
}
// get params
let result_params : Result<Path<HashMapRequest>, PathRejection> = Path::from_request(req).await;
match result_params {
Ok(params) => {
match params {
Path(parse_params) => {
params_map = parse_params;
}
}
},
Err(_) => {}
}

Ok(Context { params_map: params_map, query_map: query_map })
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use axum::routing::{delete, get, head, options, patch, post, put, trace};
use axum::Router;
use http::resource::Resource;

pub use http::context::Context;

pub type Body = axum::body::Body;

pub type Request = axum::http::Request<Body>;
Expand Down

0 comments on commit c70fabd

Please sign in to comment.