-
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.
- Loading branch information
1 parent
4c447be
commit 7bce034
Showing
9 changed files
with
1,825 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,43 @@ | ||
use axum::{extract::State, http::StatusCode, response::Result, Json}; | ||
use std::sync::Arc; | ||
|
||
use super::{ | ||
server::AppState, | ||
types::{Product, ProductRequest}, | ||
}; | ||
|
||
pub async fn root() -> &'static str { | ||
"Hello, World!" | ||
} | ||
|
||
pub async fn health() -> &'static str { | ||
"ok" | ||
} | ||
|
||
#[axum::debug_handler] | ||
pub async fn get_all_products(State(_state): State<Arc<AppState>>) -> Result<Json<Vec<Product>>> { | ||
Ok(Json(vec![Product { | ||
id: 1, | ||
name: "Product 1".to_string(), | ||
description: "Product 1 description".to_string(), | ||
price: 100, | ||
sku_code: "SKU-1".to_string(), | ||
}])) | ||
} | ||
|
||
#[axum::debug_handler] | ||
pub async fn create_product( | ||
State(_state): State<Arc<AppState>>, | ||
Json(_payload): Json<ProductRequest>, | ||
) -> Result<()> { | ||
todo!() | ||
} | ||
|
||
/// Utility function for mapping any error into a `500 Internal Server Error` | ||
/// response. | ||
fn internal_error<E>(err: E) -> (StatusCode, String) | ||
where | ||
E: std::error::Error, | ||
{ | ||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) | ||
} |
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 @@ | ||
mod handlers; | ||
pub mod server; | ||
pub mod types; |
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,39 @@ | ||
use crate::{ | ||
api::handlers::{create_product, get_all_products, health, root}, | ||
config::Config, | ||
}; | ||
use axum::{ | ||
routing::{get, post}, | ||
Router, | ||
}; | ||
use std::{ | ||
net::{IpAddr, Ipv4Addr, SocketAddr}, | ||
sync::Arc, | ||
}; | ||
use tokio::net::TcpListener; | ||
use tower_http::trace::TraceLayer; | ||
|
||
pub struct AppState {} | ||
|
||
pub async fn create(config: Config) -> anyhow::Result<()> { | ||
let state = Arc::new(AppState {}); | ||
|
||
let app = Router::new() | ||
.route("/", get(root)) | ||
.route("/health", get(health)) | ||
.route("/api/product", post(create_product)) | ||
.route("/api/product", get(get_all_products)) | ||
.layer(TraceLayer::new_for_http()) | ||
.with_state(state); | ||
|
||
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), config.port); | ||
|
||
let listener = TcpListener::bind(&socket).await.unwrap(); | ||
|
||
tracing::info!("listening on {}", socket); | ||
axum::serve(listener, app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} |
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,19 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ProductRequest { | ||
pub is_in_stock: bool, | ||
pub sku_code: String, | ||
} | ||
|
||
#[derive(Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Product { | ||
#[serde(rename = "product_id")] | ||
pub id: i64, | ||
pub name: String, | ||
pub description: String, | ||
pub price: isize, | ||
pub sku_code: String, | ||
} |
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,19 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Config { | ||
#[serde(default = "defaults::port")] | ||
pub port: u16, | ||
} | ||
|
||
mod defaults { | ||
pub const fn port() -> u16 { | ||
8083 | ||
} | ||
} | ||
|
||
impl Config { | ||
pub fn new() -> Result<Self, envy::Error> { | ||
envy::from_env::<Config>() | ||
} | ||
} |
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,2 @@ | ||
pub mod api; | ||
pub mod config; |
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,6 +1,24 @@ | ||
use std::{thread, time::Duration}; | ||
use dotenv::dotenv; | ||
use product_service::{api::server, config::Config}; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
fn main() { | ||
println!("Product service started"); | ||
thread::sleep(Duration::from_secs(60 * 60 * 24)); | ||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { | ||
"product_service=info,tower_http=debug,axum::rejection=trace".into() | ||
}), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
dotenv().ok(); | ||
let config = Config::new().expect("Config couldn't be loaded"); | ||
|
||
tracing::info!("{:?}", config); | ||
|
||
server::create(config).await?; | ||
|
||
Ok(()) | ||
} |