-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request tracking, middleware changes, migration folder bug fix (#12)
- Loading branch information
Showing
35 changed files
with
505 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ members = [ | |
"rwf-macros", | ||
"rwf-tests", | ||
"examples/django", | ||
"examples/request-tracking", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "request-tracking" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
rwf = { path = "../../rwf" } | ||
tokio = { version = "1", features = ["full"] } | ||
rand = "0.8" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[general] | ||
track_requests = true | ||
log_queries = true | ||
|
||
[database] | ||
name = "rwf_request_tracking" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
mod controllers; | ||
mod models; | ||
|
||
use rand::Rng; | ||
use rwf::{http, prelude::*, Server}; | ||
|
||
#[derive(Default)] | ||
struct Index; | ||
|
||
#[async_trait] | ||
impl Controller for Index { | ||
async fn handle(&self, _request: &Request) -> Result<Response, Error> { | ||
let ok = rand::thread_rng().gen::<bool>(); | ||
|
||
if ok { | ||
// This is tracked. | ||
Ok(Response::new().html(" | ||
<h2>All requests are tracked</h2> | ||
<p>To view requests, connect to the <code>rwf_request_tracking</code> database and run:</p> | ||
<code>SELECT * FROM rwf_requests ORDER BY id</code> | ||
")) | ||
} else { | ||
// This is tracked also. | ||
Err(Error::HttpError(Box::new(http::Error::MissingParameter))) | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), http::Error> { | ||
Logger::init(); | ||
Migrations::migrate().await?; | ||
|
||
Server::new(vec![route!("/" => Index)]) | ||
.launch("0.0.0.0:8000") | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ log_queries = true | |
cache_templates = true | ||
|
||
[database] | ||
name = "rwf_turbo" | ||
name = "rwf_turbo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod requests; | ||
|
||
pub use requests::Request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::net::IpAddr; | ||
|
||
use crate::model::{Error, FromRow, Model, ToValue, Value}; | ||
use time::OffsetDateTime; | ||
|
||
#[derive(Clone)] | ||
pub struct Request { | ||
id: Option<i64>, | ||
path: String, | ||
method: String, | ||
query: serde_json::Value, | ||
code: i32, | ||
client_ip: Option<IpAddr>, | ||
created_at: OffsetDateTime, | ||
duration: f32, | ||
} | ||
|
||
impl FromRow for Request { | ||
fn from_row(row: tokio_postgres::Row) -> Result<Self, Error> { | ||
Ok(Self { | ||
id: row.try_get("id")?, | ||
path: row.try_get("path")?, | ||
method: row.try_get("method")?, | ||
query: row.try_get("query")?, | ||
code: row.try_get("code")?, | ||
client_ip: row.try_get("client")?, | ||
created_at: row.try_get("created_at")?, | ||
duration: row.try_get("duration")?, | ||
}) | ||
} | ||
} | ||
|
||
impl Model for Request { | ||
fn id(&self) -> Value { | ||
self.id.to_value() | ||
} | ||
|
||
fn table_name() -> &'static str { | ||
"rwf_requests" | ||
} | ||
|
||
fn foreign_key() -> &'static str { | ||
"rwf_request_id" | ||
} | ||
|
||
fn column_names() -> &'static [&'static str] { | ||
&[ | ||
"path", | ||
"method", | ||
"query", | ||
"code", | ||
"client_ip", | ||
"created_at", | ||
"duration", | ||
] | ||
} | ||
|
||
fn values(&self) -> Vec<Value> { | ||
vec![ | ||
self.path.to_value(), | ||
self.method.to_value(), | ||
self.query.to_value(), | ||
self.code.to_value(), | ||
self.client_ip.to_value(), | ||
self.created_at.to_value(), | ||
self.duration.to_value(), | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.