From f8f685bd4e9e0266ee2fa81b417cfe10dd45abad Mon Sep 17 00:00:00 2001 From: fef Date: Mon, 5 Dec 2022 02:01:35 +0100 Subject: [PATCH] add state --- .env.default | 2 +- scripts/dev-server.js | 2 +- src/conf.rs | 7 ++++--- src/main.rs | 33 +++++++++++++++------------------ src/state.rs | 17 +++++++++++++++++ 5 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 src/state.rs diff --git a/.env.default b/.env.default index b3f9649..769322e 100644 --- a/.env.default +++ b/.env.default @@ -11,7 +11,7 @@ BIND_ADDR=localhost # TCP port to listen on -BIND_PORT=6942 +BIND_PORT=1312 # Database connection string # Full schema: postgres://user:password@host:port/database diff --git a/scripts/dev-server.js b/scripts/dev-server.js index d2f4431..3d1f666 100755 --- a/scripts/dev-server.js +++ b/scripts/dev-server.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -const { spawn } = require("node:child_process"); +import { spawn } from "node:child_process"; console.log("Starting development server"); diff --git a/src/conf.rs b/src/conf.rs index fa8153b..cce6a8b 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -10,10 +10,11 @@ pub struct Config { impl Config { pub fn from_env() -> Config { Config { - bind_addr: env::var("BIND_ADDR") - .unwrap_or("::1".into()), + bind_addr: env::var("BIND_ADDR").unwrap_or("localhost".into()), bind_port: env::var("BIND_PORT") - .unwrap_or("6942".into()).parse().expect("BIND_PORT is invalid"), + .unwrap_or("1312".into()) + .parse() + .expect("BIND_PORT is invalid"), database_url: env::var("DATABASE_URL") .unwrap_or("postgres://nyanoblog@localhost/nyanoblog".into()), } diff --git a/src/main.rs b/src/main.rs index 3d9d7b2..d3b73b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,30 @@ use actix_web::{get, App, HttpResponse, HttpServer, Responder}; -use sqlx::{migrate, PgPool, Pool, query}; use sqlx::postgres::PgPoolOptions; - -#[get("/")] -async fn hello() -> impl Responder { - HttpResponse::Ok().body("hello, world") -} +use sqlx::{migrate, query, PgPool, Pool}; mod conf; +mod state; use conf::Config; +use state::AppState; + +#[get("/")] +async fn hello(state: AppState) -> impl Responder { + HttpResponse::Ok().body(format!("hello world from port {}", state.config.bind_port)) +} #[actix_web::main] async fn main() -> std::io::Result<()> { dotenvy::dotenv().ok(); let config = Config::from_env(); + let db_pool = init_db(&config).await.unwrap(); + let bind_params = (config.bind_addr.clone(), config.bind_port.clone()); + let state = state::new(config, db_pool); - let pool = init_db(&config).await.unwrap(); - - let row = query!("SELECT 1 AS one").fetch_one(&pool).await.unwrap(); - dbg!(row); - - HttpServer::new(|| { - App::new() - .service(hello) - }) - .bind(("localhost", config.bind_port))? - .run() - .await + HttpServer::new(move || App::new().app_data(state.clone()).service(hello)) + .bind(bind_params)? + .run() + .await } async fn init_db(conf: &Config) -> sqlx::Result { diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..8ae6079 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,17 @@ +use actix_web::web; +use sqlx::PgPool; +use std::sync::Arc; + +use crate::conf::Config; + +pub struct State { + pub config: Config, + pub db_pool: PgPool, +} + +pub type AppStateRaw = Arc; +pub type AppState = web::Data; + +pub fn new(config: Config, db_pool: PgPool) -> AppState { + web::Data::new(Arc::new(State { config, db_pool })) +}