main.rs
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#[macro_use]
extern crate diesel;
mod error;
mod models;
mod routes;
mod schema;
mod uuid;
mod upload;
use async_std::channel::Receiver;
use models::image::Image;
use routes::markdown::*;
use routes::other::*;
use routes::user::*;
use routes::upload::*;
use crate::upload::get_sample;
use crate::uuid::Uuid;
use actix_web::{guard, web, App, HttpResponse, HttpServer};
use async_std::{ channel::{ Sender, bounded }
, fs::File };
use diesel::r2d2::{self, ConnectionManager};
use diesel::SqliteConnection;
use futures::{ FutureExt, StreamExt, select
, stream::FuturesUnordered };
use listenfd::ListenFd;
use std::convert::TryFrom;
use std::sync::Arc;
pub(crate) type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
#[derive(Clone)]
pub struct AppData {
pub database_pool: Arc<Pool>,
pub tx_upload_worker: Sender<Image>,
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
let mut listenfd = ListenFd::from_env();
dotenv::dotenv().ok();
let (tx_upload_worker, rx_upload_worker)
: (Sender<Image>, Receiver<Image>) = bounded(32);
let _upload_worker = actix_rt::spawn(async move {
let mut workers = FuturesUnordered::new();
loop {
select! {
filename = rx_upload_worker.recv().fuse() => {
match filename {
Err(_) => break,
Ok(upload) => workers.push(async move {
let upload_uuid = Uuid::try_from(upload.upload_uuid.unwrap()).unwrap();
let upload_filename = format!("/tmp/upload_{}", upload_uuid);
let mut f = File::open(&upload_filename).await.unwrap();
let mut buf = vec!['.' as u8; 3 * 4096];
get_sample(&mut f, buf.as_mut()).await.unwrap();
println!( "[UPLOAD WORKER] filename: {}"
, upload_filename );
println!( "[UPLOAD WORKER] uuid: {}"
, Uuid::get( "some.unique.namespace"
, buf.as_mut() ) );
})
}
},
_result = workers.next() => {},
}
}
while workers.len() > 0 {
workers.next().await;
}
});
let database_url = std::env::var("DATABASE_URL").expect("NOT FOUND");
let database_pool = Pool::builder()
.build(ConnectionManager::new(database_url))
.unwrap();
let database_pool = Arc::new(database_pool);
let app_data = AppData { database_pool, tx_upload_worker };
let server = HttpServer::new(move || {
App::new() . data(app_data.clone())
. service(actix_files::Files::new("/static", "./static"))
. service( web::scope("/api/v0")
. service( web::resource("/upload")
. route(web::post().to(upload))
)
. service( web::resource("/markdowns")
. route(web::get().to(get_markdowns))
)
. service( web::resource("/markdowns/{id}")
. route(web::get().to(get_markdown))
. route(web::put().to(update_markdown))
)
. service( web::resource("/markdowns/{id}/patches")
. route(web::get().to(get_patches))
)
. service( web::resource("/users")
. route(web::get().to(get_users))
. route(web::put().to(create_user))
)
. service( web::resource("/users/{id}")
. route(web::delete().to(delete_user))
. route(web::get().to(get_user))
. route(web::put().to(update_user))
)
)
. service( web::scope("")
. route("/", web::get().to(root))
. route("/api.html", web::get().to(apidoc))
. route("/index", web::get().to(root))
. route("/index.html", web::get().to(root))
. route("/favicon", web::get().to(favicon))
. route("/favicon.ico", web::get().to(favicon))
)
. default_service( web::resource("")
. route( web::get().to(p404) )
. route( web::route()
. guard( guard::Not(guard::Get()) )
. to(HttpResponse::MethodNotAllowed)
)
)
});
let server = match listenfd.take_tcp_listener(0).unwrap() {
Some(l) => server.listen(l)?,
None => server.bind("localhost:8080")?,
};
server.run().await
}