mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
fmt
This commit is contained in:
@ -1,6 +1,4 @@
|
||||
use actix_web::{
|
||||
error, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
use actix_web::{error, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -18,9 +16,7 @@ fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error
|
||||
|
||||
let detail = err.to_string();
|
||||
let resp = match &err {
|
||||
JsonPayloadError::ContentType => {
|
||||
HttpResponse::UnsupportedMediaType().body(detail)
|
||||
}
|
||||
JsonPayloadError::ContentType => HttpResponse::UnsupportedMediaType().body(detail),
|
||||
JsonPayloadError::Deserialize(json_err) if json_err.is_data() => {
|
||||
HttpResponse::UnprocessableEntity().body(detail)
|
||||
}
|
||||
|
@ -23,8 +23,7 @@ impl ResponseError for Error {
|
||||
// builds the actual response to send back when an error occurs
|
||||
fn error_response(&self) -> web::HttpResponse {
|
||||
let err_json = json!({ "error": self.msg });
|
||||
web::HttpResponse::build(StatusCode::from_u16(self.status).unwrap())
|
||||
.json(err_json)
|
||||
web::HttpResponse::build(StatusCode::from_u16(self.status).unwrap()).json(err_json)
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,11 +39,9 @@ async fn main() -> io::Result<()> {
|
||||
let ip_address = "127.0.0.1:8000";
|
||||
println!("Running server on {}", ip_address);
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().service(web::resource("/").route(web::get().to(index)))
|
||||
})
|
||||
.bind(ip_address)
|
||||
.expect("Can not bind to port 8000")
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(web::resource("/").route(web::get().to(index))))
|
||||
.bind(ip_address)
|
||||
.expect("Can not bind to port 8000")
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
use actix_web::{
|
||||
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||
};
|
||||
use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use futures::StreamExt;
|
||||
use json::JsonValue;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -92,10 +90,9 @@ mod tests {
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_index() {
|
||||
let app = test::init_service(
|
||||
App::new().service(web::resource("/").route(web::post().to(index))),
|
||||
)
|
||||
.await;
|
||||
let app =
|
||||
test::init_service(App::new().service(web::resource("/").route(web::post().to(index))))
|
||||
.await;
|
||||
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/")
|
||||
|
@ -18,10 +18,7 @@ use serde_json::Value;
|
||||
mod convention;
|
||||
|
||||
/// The main handler for JSONRPC server.
|
||||
async fn rpc_handler(
|
||||
body: Bytes,
|
||||
app_state: web::Data<AppState>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
async fn rpc_handler(body: Bytes, app_state: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
||||
let reqjson: convention::Request = match serde_json::from_slice(body.as_ref()) {
|
||||
Ok(ok) => ok,
|
||||
Err(_) => {
|
||||
@ -90,10 +87,7 @@ async fn rpc_select(
|
||||
|
||||
pub trait ImplNetwork {
|
||||
fn ping(&self) -> String;
|
||||
fn wait(
|
||||
&self,
|
||||
d: u64,
|
||||
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>>;
|
||||
fn wait(&self, d: u64) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>>;
|
||||
|
||||
fn get(&self) -> u32;
|
||||
fn inc(&mut self);
|
||||
@ -114,10 +108,7 @@ impl ImplNetwork for ObjNetwork {
|
||||
String::from("pong")
|
||||
}
|
||||
|
||||
fn wait(
|
||||
&self,
|
||||
d: u64,
|
||||
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>> {
|
||||
fn wait(&self, d: u64) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn error::Error>>>>> {
|
||||
async move {
|
||||
actix_web::rt::time::sleep(Duration::from_secs(d)).await;
|
||||
Ok(String::from("pong"))
|
||||
|
Reference in New Issue
Block a user