-
-
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.
Merge pull request #3 from levkk/levkk-django
uWSGI server
- Loading branch information
Showing
25 changed files
with
488 additions
and
30 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 |
---|---|---|
|
@@ -14,5 +14,5 @@ members = [ | |
"rwf", | ||
"rwf-cli", | ||
"rwf-macros", | ||
"rwf-tests", | ||
"rwf-tests", "examples/django", | ||
] |
This file was deleted.
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 |
---|---|---|
@@ -1 +1 @@ | ||
venv | ||
venv |
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,10 @@ | ||
[package] | ||
name = "django" | ||
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", features = ["wsgi"] } | ||
tokio = { version = "1", features = ["full"] } |
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 @@ | ||
django |
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,33 @@ | ||
use rwf::controller::WsgiController; | ||
use rwf::http::Server; | ||
use rwf::prelude::*; | ||
|
||
#[derive(Default)] | ||
struct RustIndex; | ||
|
||
#[async_trait] | ||
impl Controller for RustIndex { | ||
async fn handle(&self, _request: &Request) -> Result<Response, Error> { | ||
Ok(Response::new().html("This is served by Rust.")) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
Logger::init(); | ||
|
||
// Set PYTHONPATH to where the Django app is. | ||
let cwd = std::env::current_dir().unwrap(); | ||
let pythonpath = cwd.join("todo"); | ||
std::env::set_var("PYTHONPATH", pythonpath.display().to_string()); | ||
|
||
Server::new(vec![ | ||
// Serve /rust with Rwf. | ||
route!("/rust" => RustIndex), | ||
// Serve every other path with Django. | ||
WsgiController::new("todo.wsgi").wildcard("/"), | ||
]) | ||
.launch("0.0.0.0:8002") | ||
.await | ||
.unwrap(); | ||
} |
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,45 @@ | ||
use super::{Controller, Error}; | ||
use crate::http::{wsgi::WsgiRequest, Request, Response}; | ||
|
||
use async_trait::async_trait; | ||
use pyo3::prelude::*; | ||
|
||
use tokio::time::{timeout, Duration}; | ||
|
||
pub struct WsgiController { | ||
path: &'static str, | ||
} | ||
|
||
impl WsgiController { | ||
pub fn new(path: &'static str) -> Self { | ||
WsgiController { path } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Controller for WsgiController { | ||
async fn handle(&self, request: &Request) -> Result<Response, Error> { | ||
let request = WsgiRequest::from_request(request)?; | ||
let path = self.path; | ||
|
||
let response = timeout( | ||
Duration::from_secs(5), | ||
tokio::task::spawn_blocking(move || { | ||
let response = Python::with_gil(|py| { | ||
// Import is cached. | ||
let module = PyModule::import_bound(py, path).unwrap(); | ||
let application: Py<PyAny> = module.getattr("application").unwrap().into(); | ||
request.send(&application) | ||
}) | ||
.unwrap(); | ||
|
||
response | ||
}), | ||
) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
Ok(response.to_response()?) | ||
} | ||
} |
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
Oops, something went wrong.