first working example
This commit is contained in:
parent
527ae6433c
commit
9dc56c9c7f
1795
Cargo.lock
generated
Normal file
1795
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
26
Cargo.toml
Normal file
26
Cargo.toml
Normal file
|
@ -0,0 +1,26 @@
|
|||
[package]
|
||||
name = "HambiMap"
|
||||
version = "0.1.0"
|
||||
authors = ["Gandalf <gandalfderbunte@riseup.net>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "0.2", features = ["macros"] }
|
||||
warp = "0.2"
|
||||
mobc-postgres = { version = "0.5", features = ["with-chrono-0_4"] }
|
||||
mobc = "0.5"
|
||||
serde = {version = "1.0", features = ["derive"] }
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
thiserror = "1.0"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
# tokio is our async runtime, which we need to execute futures
|
||||
# warp is our web framework
|
||||
# mobc / mobc-postgres represents an asynchronous connection pool for our database connections
|
||||
# serde is for serializing and deserializing objects (e.g., to/from JSON)
|
||||
# thiserror is a utility library we’ll use for error handling
|
||||
# chrono represents time and date utilities
|
||||
|
||||
# Questions: async vs sync
|
||||
# which database to use?
|
|
@ -1,3 +1,8 @@
|
|||
# HambiMap
|
||||
|
||||
Code for a Website that collects stories and photos from 11 years of Hambach Forest occupation, with the goal of producing info tables to put up in the forest
|
||||
Code for a Website that collects stories and photos from 11 years of Hambach Forest occupation, with the goal of producing info tables to put up in the forest
|
||||
|
||||
## Branch tutorial1
|
||||
After the first tutorial (branch 'tutorial') was incomplete and unfunctional, let's try [this one](https://blog.logrocket.com/async-crud-web-service-rust-warp/#handling-errors-with-warp) just for the backend.
|
||||
|
||||
|
||||
|
|
21
src/db.rs
Normal file
21
src/db.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use crate::{DBCon, DBPool};
|
||||
use mobc_postgres::{tokio_postgres, PgConnectionManager};
|
||||
use tokio_postgres::{Config, Error, NoTls};
|
||||
use std::fs;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
|
||||
const DB_POOL_MAX_OPEN: u64 = 32;
|
||||
const DB_POOL_MAX_IDLE: u64 = 8;
|
||||
const DB_POOL_TIMEOUT_SECONDS: u64 = 15;
|
||||
|
||||
pub fn create_pool() -> std::result::Result<DBPool, mobc::Error<Error>> {
|
||||
let config = Config::from_str("postgres://postgres@127.0.0.1:7878/postgres")?;
|
||||
|
||||
let manager = PgConnectionManager::new(config, NoTls);
|
||||
Ok(Pool::builder()
|
||||
.max_open(DB_POOL_MAX_OPEN)
|
||||
.max_idle(DB_POOL_MAX_IDLE)
|
||||
.get_timeout(Some(Duration::from_secs(DB_POOL_TIMEOUT_SECONDS)))
|
||||
.build(manager))
|
||||
}
|
22
src/main.rs
22
src/main.rs
|
@ -1,3 +1,21 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// mod data;
|
||||
// mod db;
|
||||
// mod error;
|
||||
// mod handler;
|
||||
|
||||
use warp::{http::StatusCode, Filter};
|
||||
use mobc::{Connection, Pool};
|
||||
use mobc_postgres::{tokio_postgres, PgConnectionManager};
|
||||
use tokio_postgres::NoTls;
|
||||
|
||||
type DBCon = Connection<PgConnectionManager<NoTls>>;
|
||||
type DBPool = Pool<PgConnectionManager<NoTls>>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let health_route = warp::path!("health")
|
||||
.map(|| StatusCode::OK);
|
||||
let routes = health_route
|
||||
.with(warp::cors().allow_any_origin());
|
||||
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue