main.rs
2.23 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
#[macro_use]
extern crate diesel;
mod error;
mod models;
mod routes;
mod schema;
use actix_web::{guard, web, App, HttpResponse, HttpServer};
use diesel::r2d2::{self, ConnectionManager};
use diesel::SqliteConnection;
use listenfd::ListenFd;
pub(crate) type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
let mut listenfd = ListenFd::from_env();
dotenv::dotenv().ok();
let database_url = std::env::var("DATABASE_URL").expect("NOT FOUND");
let database_pool = Pool::builder()
.build(ConnectionManager::new(database_url))
.unwrap();
let server = HttpServer::new(move || {
App::new() . data(database_pool.clone())
. route("/", web::get().to(routes::root))
. service(actix_files::Files::new("/static", "./static"))
. service( web::resource("/users")
. route(web::get().to(routes::get_users))
. route(web::put().to(routes::create_user)))
. service( web::resource("/users/{id}")
. route(web::delete().to(routes::delete_user))
. route(web::get().to(routes::get_user))
. route(web::put().to(routes::update_user)))
. service( web::scope("")
. route("/", web::get().to(routes::root))
. route("/index", web::get().to(routes::root))
. route("/index.html", web::get().to(routes::root))
. route("/favicon", web::get().to(routes::favicon))
. route("/favicon.ico", web::get().to(routes::favicon)))
. default_service( web::resource("")
. route(web::get().to(routes::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
}