mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
Update main.rs
This commit is contained in:
parent
c4f264d8b9
commit
325d30127f
@ -6,6 +6,7 @@ extern crate futures;
|
|||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error, http, middleware, multipart, server, App, Error, FutureResponse, HttpMessage,
|
error, http, middleware, multipart, server, App, Error, FutureResponse, HttpMessage,
|
||||||
@ -15,9 +16,11 @@ use actix_web::{
|
|||||||
use futures::future;
|
use futures::future;
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
|
|
||||||
pub fn save_file(
|
pub struct AppState {
|
||||||
field: multipart::Field<HttpRequest>,
|
pub counter: Cell<usize>,
|
||||||
) -> Box<Future<Item = i64, Error = Error>> {
|
}
|
||||||
|
|
||||||
|
pub fn save_file( field: multipart::Field<HttpRequest<AppState>>) -> Box<Future<Item = i64, Error = Error>> {
|
||||||
let file_path_string = "upload.png";
|
let file_path_string = "upload.png";
|
||||||
let mut file = match fs::File::create(file_path_string) {
|
let mut file = match fs::File::create(file_path_string) {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
@ -42,9 +45,7 @@ pub fn save_file(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_multipart_item(
|
pub fn handle_multipart_item( item: multipart::MultipartItem<HttpRequest<AppState>>) -> Box<Stream<Item = i64, Error = Error>> {
|
||||||
item: multipart::MultipartItem<HttpRequest>,
|
|
||||||
) -> Box<Stream<Item = i64, Error = Error>> {
|
|
||||||
match item {
|
match item {
|
||||||
multipart::MultipartItem::Field(field) => {
|
multipart::MultipartItem::Field(field) => {
|
||||||
Box::new(save_file(field).into_stream())
|
Box::new(save_file(field).into_stream())
|
||||||
@ -57,7 +58,9 @@ pub fn handle_multipart_item(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload(req: HttpRequest) -> FutureResponse<HttpResponse> {
|
pub fn upload(req: HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
|
||||||
|
req.state().counter.set(req.state().counter.get() + 1);
|
||||||
|
println!("{:?}", req.state().counter.get());
|
||||||
Box::new(
|
Box::new(
|
||||||
req.clone()
|
req.clone()
|
||||||
.multipart()
|
.multipart()
|
||||||
@ -73,7 +76,7 @@ pub fn upload(req: HttpRequest) -> FutureResponse<HttpResponse> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> Result<HttpResponse, error::Error> {
|
fn index(_req: HttpRequest<AppState>) -> Result<HttpResponse, error::Error> {
|
||||||
let html = r#"<html>
|
let html = r#"<html>
|
||||||
<head><title>Upload Test</title></head>
|
<head><title>Upload Test</title></head>
|
||||||
<body>
|
<body>
|
||||||
@ -93,7 +96,7 @@ fn main() {
|
|||||||
let sys = actix::System::new("multipart-example");
|
let sys = actix::System::new("multipart-example");
|
||||||
|
|
||||||
server::new(|| {
|
server::new(|| {
|
||||||
App::new()
|
App::with_state(AppState{counter: Cell::new(0)})
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.resource("/", |r| {
|
.resource("/", |r| {
|
||||||
r.method(http::Method::GET).with(index);
|
r.method(http::Method::GET).with(index);
|
||||||
|
Loading…
Reference in New Issue
Block a user