-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!(api-finance): versioning and auth guard
- Loading branch information
1 parent
da58735
commit e7d20b7
Showing
11 changed files
with
186 additions
and
51 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
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,20 +1,55 @@ | ||
use axum::{ | ||
body::Body, | ||
extract::Request, | ||
http::Response, | ||
middleware::{from_fn, Next}, | ||
routing::{get, post}, | ||
Extension, Router, | ||
}; | ||
use finance::api::*; | ||
use worker::{event, Context, Env, Request, Response, Result, Router}; | ||
use tower_service::Service; | ||
use worker::{event, Context, Env, HttpRequest, Result}; | ||
|
||
macro_rules! handler ( | ||
($name:path) => { | ||
|Extension(env): Extension<Env>, req: Request| async { | ||
let resp = $name(req.try_into().expect("convert request"), env).await.expect("handler result"); | ||
Into::<Response<Body>>::into(resp) | ||
} | ||
} | ||
); | ||
|
||
macro_rules! layer { | ||
($name:path) => { | ||
|Extension(env): Extension<Env>, req: Request, next: Next| async { | ||
$name(req, env, next).await.expect("handler result") | ||
} | ||
}; | ||
} | ||
|
||
fn api_v1_services() -> Router { | ||
Router::new() | ||
.route("/transactions", get(handler!(list_transactions))) | ||
.route("/transactions/:id", get(handler!(get_transaction))) | ||
.route("/transactions", post(handler!(create_transaction))) | ||
.route("/tags", get(handler!(list_tags))) | ||
.route("/tags/:id", get(handler!(get_tag))) | ||
.route("/tags", post(handler!(create_tag))) | ||
.route("/categories", get(handler!(list_categories))) | ||
.route("/categories/:id", get(handler!(get_category))) | ||
.route("/categories", post(handler!(create_category))) | ||
} | ||
|
||
#[event(fetch)] | ||
async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> { | ||
let router = Router::new(); | ||
async fn main(req: HttpRequest, env: Env, _ctx: Context) -> Result<Response<Body>> { | ||
console_error_panic_hook::set_once(); | ||
|
||
let router = Router::new() | ||
.nest("/v1", api_v1_services()) | ||
.layer(from_fn(layer!(authentication_guard))) | ||
.layer(Extension(env)) | ||
.call(req) | ||
.await?; | ||
|
||
router | ||
.get_async("/transactions", list_transactions) | ||
.get_async("/transactions/:id", retrieve_transaction) | ||
.post_async("/transactions", create_transaction) | ||
.get_async("/tags", list_tags) | ||
.get_async("/tags/:id", retrieve_tag) | ||
.post_async("/tags", create_tag) | ||
.get_async("/categories", list_categories) | ||
.get_async("/categories/:id", retrieve_category) | ||
.post_async("/categories", create_category) | ||
.run(req, env) | ||
.await | ||
Ok(router) | ||
} |
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,49 @@ | ||
use axum::{ | ||
body::Body, | ||
extract::Request, | ||
http::{Response, StatusCode}, | ||
middleware::Next, | ||
}; | ||
use worker::{Env, Result}; | ||
|
||
#[worker::send] | ||
pub async fn authentication_guard(req: Request, env: Env, next: Next) -> Result<Response<Body>> { | ||
let auth_key_env = env.var("AUTH_KEY"); | ||
|
||
let auth_key: String; | ||
|
||
match auth_key_env { | ||
Ok(key) => auth_key = key.to_string(), | ||
_ => { | ||
return Ok( | ||
Response::builder() | ||
.status(StatusCode::INTERNAL_SERVER_ERROR) | ||
.body(Body::empty()) | ||
.unwrap(), | ||
); | ||
} | ||
}; | ||
|
||
let auth_header_key_param = req.headers().get("x-auth-key"); | ||
|
||
match auth_header_key_param { | ||
Some(key) => { | ||
if auth_key.ne(key) { | ||
return Ok( | ||
Response::builder() | ||
.status(StatusCode::UNAUTHORIZED) | ||
.body(Body::empty()) | ||
.unwrap(), | ||
); | ||
} | ||
|
||
return Ok(next.run(req).await); | ||
} | ||
None => Ok( | ||
Response::builder() | ||
.status(StatusCode::FORBIDDEN) | ||
.body(Body::empty()) | ||
.unwrap(), | ||
), | ||
} | ||
} |
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,7 +1,9 @@ | ||
mod authentication; | ||
mod category; | ||
mod tag; | ||
mod transaction; | ||
|
||
pub use category::{create_category, list_categories, retrieve_category}; | ||
pub use tag::{create_tag, list_tags, retrieve_tag}; | ||
pub use transaction::{create_transaction, list_transactions, retrieve_transaction}; | ||
pub use authentication::authentication_guard; | ||
pub use category::{create_category, get_category, list_categories}; | ||
pub use tag::{create_tag, get_tag, list_tags}; | ||
pub use transaction::{create_transaction, get_transaction, list_transactions}; |
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.