Created
December 14, 2018 07:39
-
-
Save syntheticsh/c2a07d9ca5821eb741af7d6c3ab87642 to your computer and use it in GitHub Desktop.
actix_test
This file contains hidden or 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
| [package] | |
| name = "actix_play" | |
| version = "0.1.0" | |
| edition = "2018" | |
| [dependencies] | |
| actix = "0.7" | |
| actix-web = { version = "0.7.8", features = ["tls"] } | |
| futures = "0.1" | |
| serde = "1.0" | |
| serde_json = "1.0" | |
| serde_derive = "1.0" | |
| json = "0.11.13" |
This file contains hidden or 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
| #[macro_use] | |
| extern crate serde_derive; | |
| use actix_web::{ http::Method, server, App, client, Error, HttpMessage, HttpResponse, Json}; | |
| use futures::{future::ok as fut_ok, Future}; | |
| use std::time::Duration; | |
| use std::fs::OpenOptions; | |
| use std::io::prelude::*; | |
| #[derive(Debug, Serialize, Deserialize)] | |
| pub struct Request { | |
| payload: String, | |
| } | |
| #[derive(Serialize)] | |
| struct Response { | |
| result: String, | |
| } | |
| fn make_response(status: i32, message: &str) -> impl Future<Item = HttpResponse, Error = Error> { | |
| fut_ok( | |
| HttpResponse::Ok() | |
| .status(actix_web::http::StatusCode::from_u16(status as u16).unwrap()) | |
| .json(Response { result: String::from(message) }), | |
| ) | |
| } | |
| fn get_url(url: &str) -> impl Future<Item = Vec<u8>, Error = Error> { | |
| client::ClientRequest::get(url) | |
| .finish() | |
| .unwrap() | |
| .send() | |
| .conn_timeout(Duration::from_secs(5)) | |
| .map_err(Error::from) | |
| .and_then(|response| { | |
| response | |
| .body() | |
| .from_err() | |
| .and_then(|bytes| fut_ok(bytes.to_vec())) | |
| }) | |
| } | |
| pub fn index(val: Json<Request>) -> impl Future<Item = HttpResponse, Error = Error> { | |
| let mut file = match OpenOptions::new().write(true).create(true).open("/tmp/test") { | |
| Ok(file) => file, | |
| Err(err) => return make_response(500, &err.to_string()), | |
| }; | |
| get_url(&val.payload) | |
| .or_else(|err| make_response(500, &err.to_string())) | |
| } | |
| fn main() { | |
| let sys = actix::System::new("Сервер принтера"); | |
| server::new(|| { | |
| App::new().resource("/", |resource| { | |
| resource.method(Method::POST).with_async(index) | |
| }) | |
| }) | |
| .bind("127.0.0.1:8888") | |
| .unwrap() | |
| .shutdown_timeout(1) | |
| .workers(1) | |
| .start(); | |
| println!("Сервер стартанул: 127.0.0.1:8888", ); | |
| let _ = sys.run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment