-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
53 lines (46 loc) · 1.28 KB
/
main.rs
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
use ntex::web;
use ntex_files as fs;
pub mod view;
use view::*;
use sqlx::{postgres::PgPoolOptions, Error, PgPool, pool::PoolConnectionMetadata, Postgres};
use std::env;
async fn get_pool() -> Result<PgPool, Error> {
// Get database address
let db_address = env::var("DATABASE_URL");
match db_address {
Ok(addr) => {
return PgPoolOptions::new().max_connections(5).connect(&addr).await;
}
Err(_) => panic!("DATABASE_URL not set. Please define this env variable."),
}
}
#[web::get("/")]
async fn index(
pool: web::types::State<PgPool>,
) -> impl web::Responder {
let todos = sqlx::query_as::<_, Todo>("SELECT title, content FROM todo")
.fetch_all(&*pool)
.await
.unwrap();
web::HttpResponse::Ok().body(
MainPage {
title: "Welcome",
name: "Mike",
todos: &todos,
}
.to_string(),
)
}
#[ntex::main]
async fn main() -> std::io::Result<()> {
let pool = get_pool().await.expect("error starting connection pool");
web::HttpServer::new(move || {
web::App::new()
.state(pool.clone())
.service(index)
.service(fs::Files::new("/", "./static_assets/"))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}