-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add JSON request/response support
- Loading branch information
Showing
10 changed files
with
270 additions
and
8 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
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 = "example-json" | ||
version = "0.1.0" | ||
publish = false | ||
description = "JSON - Flareon example." | ||
edition = "2021" | ||
|
||
[dependencies] | ||
flareon = { path = "../../flareon" } | ||
serde = "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,50 @@ | ||
use flareon::request::{Request, RequestExt}; | ||
use flareon::response::{Response, ResponseExt}; | ||
use flareon::router::{Route, Router}; | ||
use flareon::{FlareonApp, FlareonProject, StatusCode}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
struct AddRequest { | ||
a: i32, | ||
b: i32, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize)] | ||
struct AddResponse { | ||
result: i32, | ||
} | ||
|
||
async fn add(mut request: Request) -> flareon::Result<Response> { | ||
let add_request: AddRequest = request.json().await?; | ||
let response = AddResponse { | ||
result: add_request.a + add_request.b, | ||
}; | ||
|
||
Response::new_json(StatusCode::OK, &response) | ||
} | ||
|
||
struct AddApp; | ||
|
||
impl FlareonApp for AddApp { | ||
fn name(&self) -> &'static str { | ||
env!("CARGO_PKG_NAME") | ||
} | ||
|
||
fn router(&self) -> Router { | ||
Router::with_urls([Route::with_handler("/", add)]) | ||
} | ||
} | ||
|
||
// Test with: | ||
// curl --header "Content-Type: application/json" --request POST --data '{"a": 123, "b": 456}' 'http://127.0.0.1:8080/' | ||
|
||
#[flareon::main] | ||
async fn main() -> flareon::Result<FlareonProject> { | ||
let flareon_project = FlareonProject::builder() | ||
.register_app_with_views(AddApp, "") | ||
.build() | ||
.await?; | ||
|
||
Ok(flareon_project) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub(crate) const HTML_CONTENT_TYPE: &str = "text/html; charset=utf-8"; | ||
pub(crate) const FORM_CONTENT_TYPE: &str = "application/x-www-form-urlencoded"; | ||
#[cfg(feature = "json")] | ||
pub(crate) const JSON_CONTENT_TYPE: &str = "application/json"; |
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.