Skip to content

Commit

Permalink
Activate venv in django example.
Browse files Browse the repository at this point in the history
Remove print() in todo/urls.py.
Default routes to rank 0. Set wildcard route to rank -20 (as documented).
Hide template errors in production (release profile) and avoid blocking IO.
  • Loading branch information
levkk committed Oct 29, 2024
1 parent 8e26aec commit aa462f2
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions examples/django/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ edition = "2021"
[dependencies]
rwf = { path = "../../rwf", features = ["wsgi"] }
tokio = { version = "1", features = ["full"] }
pyo3 = "0.22"
tracing = "0.1"
24 changes: 24 additions & 0 deletions examples/django/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Django + Rwf

This application demonstrates how to run Django (and any other WSGI application) with Rwf.

## Quick start

Create a virtual environment:

```bash
python3 -m venv venv
```

Activate the venv and install dependencies:

```bash
source venv/bin/activate
pip install -r requirements.txt
```

Run the app:

```
cargo run
```
24 changes: 24 additions & 0 deletions examples/django/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use rwf::controller::WsgiController;
use rwf::http::Server;
use rwf::prelude::*;

use pyo3::prelude::*;
use std::path::Path;

#[derive(Default)]
struct RustIndex;

Expand All @@ -17,10 +20,31 @@ async fn main() {
Logger::init();

// Set PYTHONPATH to where the Django app is.
tracing::info!("Adding \"todo\" to PYTHONPATH");

let cwd = std::env::current_dir().unwrap();
let pythonpath = cwd.join("todo");
std::env::set_var("PYTHONPATH", pythonpath.display().to_string());

// Make virtualenv work
for py in ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] {
let venv = Path::new("venv/lib/")
.join(&format!("python{}", py))
.join("site-packages");

if venv.exists() {
tracing::info!("Importing packages from \"{}\" (venv)", venv.display());

Python::with_gil(|py| {
let sys = py.import_bound("sys").unwrap();
let path = sys.getattr("path").unwrap();
path.call_method1("append", (venv.display().to_string(),))
.unwrap();
});
break;
}
}

Server::new(vec![
// Serve /rust with Rwf.
route!("/rust" => RustIndex),
Expand Down
1 change: 0 additions & 1 deletion examples/django/todo/todo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def index(request):
</html>
""")
else:
print(request.POST)
name = request.POST.get("name", "none")
return HttpResponse("Name: " + name)

Expand Down
9 changes: 7 additions & 2 deletions rwf/src/http/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Handler {
path: Path::parse(path).unwrap().with_regex(path_type).unwrap(),
controller: Box::new(controller),
name: None,
rank: -20,
rank: 0,
}
}

Expand All @@ -33,7 +33,7 @@ impl Handler {
}

pub fn wildcard(path: &str, controller: impl Controller + 'static) -> Self {
Self::new(path, controller, PathType::Wildcard)
Self::new(path, controller, PathType::Wildcard).with_rank(-20)
}

pub fn route(path: &str, controller: impl Controller + 'static) -> Self {
Expand All @@ -53,6 +53,11 @@ impl Handler {
self.path.deref()
}

pub fn with_rank(mut self, rank: i64) -> Self {
self.rank = rank;
self
}

pub fn controller_name(&self) -> &'static str {
self.deref().controller_name()
}
Expand Down
5 changes: 4 additions & 1 deletion rwf/src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ impl Response {
let err = format!("{:?}", err);

#[cfg(not(debug_assertions))]
let err = "";
let err = {
let _ = err;
""
};

Self::new()
.html(format!(
Expand Down
19 changes: 14 additions & 5 deletions rwf/src/view/template/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,21 @@ impl Error {
}

pub fn pretty_from_path(self, path: impl AsRef<Path> + Copy) -> Self {
let src = match std::fs::read_to_string(path) {
Ok(src) => src,
Err(_) => return self,
};
#[cfg(debug_assertions)]
{
let src = match std::fs::read_to_string(path) {
Ok(src) => src,
Err(_) => return self,
};

self.pretty(&src, Some(path))
}

self.pretty(&src, Some(path))
#[cfg(not(debug_assertions))]
{
let _ = path;
self
}
}
}

Expand Down

0 comments on commit aa462f2

Please sign in to comment.