Skip to content

Commit b9a5ea8

Browse files
authored
chore: update examples API usage (#17)
1 parent 74febbe commit b9a5ea8

File tree

9 files changed

+46
-54
lines changed

9 files changed

+46
-54
lines changed

examples/apps/http/src/main.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2727
.route("/one", get(handler_one))
2828
.route("/two", get(handler_two));
2929

30-
let svc = ServiceBuilder::default().http(api).build();
30+
let mut svc = ServiceBuilder::default()
31+
.http(api)
32+
.build()?;
3133

32-
match svc {
33-
Ok(mut svc) => svc.start().await,
34-
Err(e) => panic!("{}", e.to_string()),
35-
}
36-
37-
Ok(())
34+
Ok(svc.start().await?)
3835
}

examples/apps/http_with_lifecycle/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6868
.route("/two", get(handler_two));
6969

7070
let state = Arc::new(Mutex::new(AppState::default()));
71-
let svc = ServiceBuilder::default()
71+
let mut svc = ServiceBuilder::default()
7272
.http_with_lifecycle(api, state.clone())
73-
.build();
73+
.build()?;
7474

75-
match svc {
76-
Ok(mut svc) => svc.start().await,
77-
Err(e) => panic!("{}", e.to_string()),
78-
}
79-
80-
Ok(())
75+
Ok(svc.start().await?)
8176
}

examples/apps/http_with_lifecycle_and_state/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6868
.route("/two", get(handler_two));
6969

7070
let state = Arc::new(Mutex::new(AppState::default()));
71-
let svc = ServiceBuilder::default()
71+
let mut svc = ServiceBuilder::default()
7272
.http_with_lifecycle_and_state(api, state.clone(), state.clone())
73-
.build();
73+
.build()?;
7474

75-
match svc {
76-
Ok(mut svc) => svc.start().await,
77-
Err(e) => panic!("{}", e.to_string()),
78-
}
79-
80-
Ok(())
75+
Ok(svc.start().await?)
8176
}

examples/apps/http_with_protobuf/src/api/router.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ async fn create_card(
180180
// Adds mikros context inside the request for the wrapper to also access it.
181181
request.extensions_mut().insert(context.clone());
182182

183+
// Translates the wrapper response into the endpoint response.
183184
match router.wrapper.create_card(request).await {
184185
Ok(response) => Ok(Json(response.into_inner().into())),
185186
Err(error) => {
186-
// Translates the wrapper error into the endpoint response.
187187
let e: merrors::ServiceError = error.into();
188188
Err(e.into())
189189
}
@@ -211,8 +211,13 @@ async fn get_card(
211211
request.extensions_mut().insert(context.clone());
212212

213213
// Translates the wrapper response into the endpoint response.
214-
let res = router.wrapper.get_card(request).await?;
215-
Ok(Json(res.into_inner().into()))
214+
match router.wrapper.get_card(request).await {
215+
Ok(response) => Ok(Json(response.into_inner().into())),
216+
Err(error) => {
217+
let e: merrors::ServiceError = error.into();
218+
Err(e.into())
219+
}
220+
}
216221
}
217222

218223
#[derive(Deserialize)]
@@ -245,8 +250,13 @@ async fn update_card(
245250
request.extensions_mut().insert(context.clone());
246251

247252
// Translates the wrapper response into the endpoint response.
248-
let res = router.wrapper.update_card(request).await?;
249-
Ok(Json(res.into_inner().into()))
253+
match router.wrapper.update_card(request).await {
254+
Ok(response) => Ok(Json(response.into_inner().into())),
255+
Err(error) => {
256+
let e: merrors::ServiceError = error.into();
257+
Err(e.into())
258+
}
259+
}
250260
}
251261

252262
async fn delete_card(
@@ -270,8 +280,13 @@ async fn delete_card(
270280
request.extensions_mut().insert(context.clone());
271281

272282
// Translates the wrapper response into the endpoint response.
273-
let res = router.wrapper.delete_card(request).await?;
274-
Ok(Json(res.into_inner().into()))
283+
match router.wrapper.delete_card(request).await {
284+
Ok(response) => Ok(Json(response.into_inner().into())),
285+
Err(error) => {
286+
let e: merrors::ServiceError = error.into();
287+
Err(e.into())
288+
}
289+
}
275290
}
276291

277292
#[derive(Clone)]

examples/apps/http_with_state/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5050
.route("/two", get(handler_two));
5151

5252
let state = Arc::new(Mutex::new(AppState::default()));
53-
let svc = ServiceBuilder::default()
53+
let mut svc = ServiceBuilder::default()
5454
.http_with_state(api, state.clone())
55-
.build();
55+
.build()?;
5656

57-
match svc {
58-
Ok(mut svc) => svc.start().await,
59-
Err(e) => panic!("{}", e.to_string()),
60-
}
61-
62-
Ok(())
57+
Ok(svc.start().await?)
6358
}

examples/apps/native/src/main.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ use mikros::service::builder::ServiceBuilder;
44
use service::Service as AppService;
55

66
#[tokio::main]
7-
async fn main() {
7+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
88
let s = AppService::new();
9-
let svc = ServiceBuilder::default()
9+
let mut svc = ServiceBuilder::default()
1010
.native(Box::new(s))
1111
.with_features(vec![simple_api::new(), example::new()])
12-
.build();
12+
.build()?;
1313

14-
match svc {
15-
Ok(mut svc) => svc.start().await,
16-
Err(e) => panic!("{}", e.to_string()),
17-
}
14+
Ok(svc.start().await?)
1815
}

examples/apps/script/src/main.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ use mikros::service::builder::ServiceBuilder;
44
use service::Service as AppService;
55

66
#[tokio::main]
7-
async fn main() {
7+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
88
let s = AppService::new();
9-
let svc = ServiceBuilder::default()
9+
let mut svc = ServiceBuilder::default()
1010
.script(Box::new(s))
1111
.with_features(vec![example::new()])
12-
.build();
12+
.build()?;
1313

14-
match svc {
15-
Ok(mut svc) => svc.start().await,
16-
Err(e) => panic!("{}", e.to_string()),
17-
}
14+
Ok(svc.start().await?)
1815
}

examples/services/cronjob/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod builder;
22

3+
use std::collections::HashMap;
34
use std::sync::Arc;
45

56
use mikros::definition::ServiceKind;
@@ -55,7 +56,7 @@ impl plugin::service::Service for Cronjob {
5556
}
5657
}
5758

58-
fn initialize(&mut self, _envs: Arc<Env>, definitions: Arc<mikros::definition::Definitions>) -> merrors::Result<()> {
59+
fn initialize(&mut self, definitions: Arc<mikros::definition::Definitions>, _envs: Arc<Env>, _: HashMap<String, serde_json::Value>) -> merrors::Result<()> {
5960
self.definitions = definitions.load_service(self.kind())?;
6061
if self.definitions.is_none() {
6162
// TODO: return error here

src/service/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ macro_rules! link_grpc_service {
125125
let url = $context.client_connection_url($client_name);
126126
match $client::connect(url).await {
127127
Ok(c) => c,
128-
Err(e) => return Err(mikros::errors::Error::Custom($client_name.to_string(), e.to_string()))
128+
Err(e) => return Err(mikros::errors::Error::Custom(e.to_string()))
129129
}
130130
}
131131
};

0 commit comments

Comments
 (0)