feat: read environment variables
This commit is contained in:
parent
d85bc8c579
commit
4675329334
61
src/env.rs
Normal file
61
src/env.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use std::ffi::OsString;
|
||||
use std::net::{AddrParseError, SocketAddr};
|
||||
use std::str::FromStr;
|
||||
|
||||
use naan::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Api {
|
||||
pub addr: SocketAddr,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Postgres {
|
||||
pub user: String,
|
||||
pub pass: String,
|
||||
pub host: String,
|
||||
pub port: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Env {
|
||||
pub postgres: Postgres,
|
||||
pub api: Api,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
VarNotUnicode(String, OsString),
|
||||
VarNotSocketAddr(String, AddrParseError),
|
||||
RequiredVarNotSet(String),
|
||||
}
|
||||
|
||||
impl Env {
|
||||
pub fn try_read() -> Result<Self, Error> {
|
||||
fn get(k: &'static str) -> Result<Option<String>, Error> {
|
||||
std::env::var("POSTGRES_USER").map(Some)
|
||||
.recover(|e| match e {
|
||||
| std::env::VarError::NotPresent => Ok(None),
|
||||
| std::env::VarError::NotUnicode(os) => {
|
||||
Err(Error::VarNotUnicode(k.into(), os))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn get_required(k: &'static str) -> Result<String, Error> {
|
||||
get(k)?.ok_or_else(|| Error::RequiredVarNotSet(k.into()))
|
||||
}
|
||||
|
||||
let postgres = Postgres { user: get_required("POSTGRES_USER")?,
|
||||
host: get_required("POSTGRES_HOST")?,
|
||||
pass: get_required("POSTGRES_PASS")?,
|
||||
port: get_required("POSTGRES_PORT")? };
|
||||
|
||||
let api_addr = get("API_ADDR")?.unwrap_or("127.0.0.1:4444".into());
|
||||
|
||||
let api = Api { addr: api_addr.parse()
|
||||
.map_err(|e| Error::VarNotSocketAddr("API_ADDR".into(), e))? };
|
||||
|
||||
Ok(Env { postgres, api })
|
||||
}
|
||||
}
|
17
src/main.rs
17
src/main.rs
@ -7,10 +7,12 @@ use toad_msg::alloc::Message;
|
||||
use toad_msg::Type;
|
||||
|
||||
mod app;
|
||||
mod env;
|
||||
mod hashed_text;
|
||||
mod postgres;
|
||||
mod repo;
|
||||
mod user;
|
||||
mod uuid;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! newtype {
|
||||
@ -94,8 +96,7 @@ fn handle_request(req: Addrd<Req<ToadT>>) -> Result<Addrd<Message>, String> {
|
||||
|
||||
if path_segments.peek() == Some(&"users") {
|
||||
let mut path_segments = path_segments.clone();
|
||||
path_segments.next();
|
||||
let _id = path_segments.next();
|
||||
let _id = path_segments.nth(2);
|
||||
let msg = Message::builder(Type::Ack, toad::resp::code::NOT_FOUND).build();
|
||||
Ok(Addrd(msg, req.addr()))
|
||||
} else {
|
||||
@ -122,13 +123,13 @@ fn server_worker(p: &'static Toad) {
|
||||
fn main() {
|
||||
simple_logger::init().unwrap();
|
||||
|
||||
static mut TOAD: Option<Toad> = None;
|
||||
let env = env::Env::try_read().unwrap();
|
||||
|
||||
unsafe {
|
||||
TOAD = Some(Toad::try_new("0.0.0.0:4444", Config::default()).unwrap());
|
||||
}
|
||||
let toad = Toad::try_new(env.api.addr, Config::default()).unwrap();
|
||||
|
||||
let r = unsafe { TOAD.as_ref().unwrap() };
|
||||
// SAFETY
|
||||
// this is safe because the server worker cannot outlive the main thread
|
||||
let toad_ref: &'static Toad = unsafe { core::mem::transmute(&toad) };
|
||||
|
||||
server_worker(r);
|
||||
server_worker(toad_ref);
|
||||
}
|
||||
|
1
src/uuid.rs
Normal file
1
src/uuid.rs
Normal file
@ -0,0 +1 @@
|
||||
|
Loading…
Reference in New Issue
Block a user