add state
This commit is contained in:
parent
1e6a127b5a
commit
f8f685bd4e
5 changed files with 38 additions and 23 deletions
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -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()),
|
||||
}
|
||||
|
|
33
src/main.rs
33
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<PgPool> {
|
||||
|
|
17
src/state.rs
Normal file
17
src/state.rs
Normal file
|
@ -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<State>;
|
||||
pub type AppState = web::Data<AppStateRaw>;
|
||||
|
||||
pub fn new(config: Config, db_pool: PgPool) -> AppState {
|
||||
web::Data::new(Arc::new(State { config, db_pool }))
|
||||
}
|
Loading…
Reference in a new issue