fn new_paste(
    config: web::Data<Config>,
    paste: web::Form<Paste>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
    web::block(move || {
        let mut rng = thread_rng();

        // Paste IDs (= paste file names) are 8 character alphanumeric strings.
        // Here we generate a random ID that is not already in use,
        // and create (and open) a paste file with that ID as its name.
        let (mut file, paste_id) = loop {
            let id: String = iter::repeat(())
                .map(|()| rng.sample(Alphanumeric))
                .take(8)
                .collect();
            let full_path = format!("{}/{}", config.paste_dir, id);
            if let Ok(file) = OpenOptions::new().write(true).create(true).open(full_path) {
                break (file, id);
            }
        };

        let paste_url = format!("{}/{}", config.url_base, paste_id);
        file.write_all(paste.data.as_bytes()).and(Ok(paste_url))
    })
    .then(|res| match res {
        Ok(paste_url) => Ok(HttpResponse::Created()
            .set_header("Location", paste_url.clone())
            .content_type("text/plain")
            .body(paste_url)),
        Err(_) => Ok(HttpResponse::InternalServerError().into()),
    })
}
// TODO: Consider using multipart formdata instead of urlencoded.
// XXX: This will hang in an infinite loop if the paste directory does not exist.
// We'll probably make sure it exists while parsing config, not here.
Consider using multipart formdata instead of urlencoded.
XXX: This will hang in an infinite loop if the paste directory does not exist.
We'll probably make sure it exists while parsing config, not here.
Consider using multipart formdata instead of urlencoded . XXX : This will hang in an infinite loop if the paste directory does not exist . We ' ll probably make sure it exists while parsing config not here .
fn new_paste(
    config: web::Data<Config>,
    paste: web::Form<Paste>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
    web::block(move || {
        let mut rng = thread_rng();
        // Paste IDs (= paste file names) are 8 character alphanumeric strings.
        // Here we generate a random ID that is not already in use,
        // and create (and open) a paste file with that ID as its name.
        let (mut file, paste_id) = loop {
            let id: String = iter::repeat(())
                .map(|()| rng.sample(Alphanumeric))
                .take(8)
                .collect();
            let full_path = format!("{}/{}", config.paste_dir, id);
            if let Ok(file) = OpenOptions::new().write(true).create(true).open(full_path) {
                break (file, id);
            }
        };
        let paste_url = format!("{}/{}", config.url_base, paste_id);
        file.write_all(paste.data.as_bytes()).and(Ok(paste_url))
    })
    .then(|res| match res {
        Ok(paste_url) => Ok(HttpResponse::Created()
            .set_header("Location", paste_url.clone())
            .content_type("text/plain")
            .body(paste_url)),
        Err(_) => Ok(HttpResponse::InternalServerError().into()),
    })
}
fn new_paste ( config : web :: Data < Config > , paste : web :: Form < Paste > , ) -> impl Future < Item = HttpResponse , Error = actix_web :: Error > { web :: block ( move | | { let mut rng = thread_rng ( ) ; // Paste IDs (= paste file names) are 8 character alphanumeric strings. // Here we generate a random ID that is not already in use, // and create (and open) a paste file with that ID as its name. let ( mut file , paste_id ) = loop { let id : String = iter :: repeat ( ( ) ) . map ( | ( ) | rng . sample ( Alphanumeric ) ) . take ( 8 ) . collect ( ) ; let full_path = format ! ( " " config paste_dir id ) ; if let Ok ( file ) = OpenOptions :: new ( ) . write ( true ) . create ( true ) . open ( full_path ) { break ( file , id ) ; } } ; let paste_url = format ! ( " " config url_base paste_id ) ; file . write_all ( paste . data . as_bytes ( ) ) . and ( Ok ( paste_url ) ) } ) . then ( | res | match res { Ok ( paste_url ) => Ok ( HttpResponse :: Created ( ) . set_header ( " " , paste_url . clone ( ) ) . content_type ( " " ) . body ( paste_url ) ) , Err ( _ ) => Ok ( HttpResponse :: InternalServerError ( ) . into ( ) ) , } ) }
TODO: Consider using multipart formdata instead of urlencoded.
TODO : Consider using multipart formdata instead of urlencoded .
config
web::Data<Config>
paste
web::Form<Paste>
config
web::Data<Config>
paste
web::Form<Paste>