Add config/options for IP and port
This commit is contained in:
parent
c9a59f4731
commit
cc2a453c97
15
src/cli.rs
15
src/cli.rs
@ -1,10 +1,13 @@
|
||||
use structopt::StructOpt;
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
net::IpAddr,
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct Options {
|
||||
/// Path to config
|
||||
#[structopt(short, long, value_name = "path", default_value = "/etc/noise-server/config.toml")]
|
||||
#[structopt(short, long, value_name = "path", default_value = crate::DEFAULT_CONFIG_PATH)]
|
||||
pub config: PathBuf,
|
||||
|
||||
/// Don't use config
|
||||
@ -18,4 +21,12 @@ pub struct Options {
|
||||
/// Prints default config
|
||||
#[structopt(long)]
|
||||
pub generate_config: bool,
|
||||
|
||||
/// IP address to bind to
|
||||
#[structopt(short = "a", long, value_name = "ip", default_value = crate::DEFAULT_IP_ADDRESS_STR)]
|
||||
pub ip_address: IpAddr,
|
||||
|
||||
/// Port to bind to
|
||||
#[structopt(short, long, value_name = "port", default_value = crate::DEFAULT_PORT_STR)]
|
||||
pub port: u16,
|
||||
}
|
@ -2,7 +2,10 @@ use serde::{
|
||||
Deserialize,
|
||||
Serialize,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
net::IpAddr,
|
||||
path::PathBuf,
|
||||
};
|
||||
use tokio::{
|
||||
fs::File,
|
||||
io::AsyncReadExt,
|
||||
@ -13,6 +16,8 @@ use crate::cli::Options;
|
||||
#[serde(default, rename_all = "kebab-case")]
|
||||
pub struct Config {
|
||||
pub database_path: PathBuf,
|
||||
pub ip_address: IpAddr,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@ -38,6 +43,8 @@ impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
database_path: crate::DEFAULT_DATABASE_PATH.into(),
|
||||
ip_address: crate::DEFAULT_IP_ADDRESS,
|
||||
port: crate::DEFAULT_PORT,
|
||||
}
|
||||
}
|
||||
}
|
25
src/main.rs
25
src/main.rs
@ -2,6 +2,10 @@ use structopt::StructOpt;
|
||||
use std::{
|
||||
boxed::Box,
|
||||
error::Error,
|
||||
net::{
|
||||
IpAddr,
|
||||
Ipv4Addr,
|
||||
},
|
||||
};
|
||||
use crate::{cli::Options, config::Config};
|
||||
|
||||
@ -12,7 +16,18 @@ mod net;
|
||||
mod sqlite;
|
||||
mod graphql;
|
||||
|
||||
const DEFAULT_CONFIG_PATH: &'static str = "/etc/noise-server/config.toml";
|
||||
const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite";
|
||||
const DEFAULT_IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
|
||||
const DEFAULT_IP_ADDRESS_STR: &'static str = "0.0.0.0";
|
||||
#[cfg(debug_assertions)]
|
||||
const DEFAULT_PORT: u16 = 8080;
|
||||
#[cfg(debug_assertions)]
|
||||
const DEFAULT_PORT_STR: &'static str = "8080";
|
||||
#[cfg(not(debug_assertions))]
|
||||
const DEFAULT_PORT: u16 = 80;
|
||||
#[cfg(not(debug_assertions))]
|
||||
const DEFAULT_PORT_STR: &'static str = "80";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
@ -30,6 +45,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
config.database_path = options.database_path.clone();
|
||||
}
|
||||
|
||||
if clap_matches.occurrences_of("ip-address") > 0 {
|
||||
config.ip_address = options.ip_address;
|
||||
}
|
||||
|
||||
if clap_matches.occurrences_of("port") > 0 {
|
||||
config.port = options.port;
|
||||
}
|
||||
|
||||
dbg!(&config);
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
@ -38,7 +61,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[cfg(feature = "sqlite")]
|
||||
sqlite::load_template(&sqlite_connection).await?;
|
||||
|
||||
net::web(sqlite_connection).await;
|
||||
net::web(config.ip_address, config.port, sqlite_connection).await;
|
||||
|
||||
Ok(())
|
||||
}
|
13
src/net.rs
13
src/net.rs
@ -1,16 +1,13 @@
|
||||
use std::net::IpAddr;
|
||||
use sqlx::SqlitePool;
|
||||
use warp::{
|
||||
Filter,
|
||||
};
|
||||
use warp::Filter;
|
||||
|
||||
use crate::graphql::{
|
||||
Context,
|
||||
schema,
|
||||
};
|
||||
|
||||
|
||||
pub async fn web(db: SqlitePool){
|
||||
|
||||
pub async fn web(ip_address: IpAddr, port: u16, db: SqlitePool) {
|
||||
std::env::set_var("RUST_LOG", "warp_async");
|
||||
let state = warp::any().map(move || Context { db: db.clone() });
|
||||
let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed());
|
||||
@ -20,7 +17,5 @@ pub async fn web(db: SqlitePool){
|
||||
.and(warp::path("graphiql"))
|
||||
.and(juniper_warp::graphiql_filter("/graphql", None))
|
||||
.or(warp::path("graphql").and(graphql_filter)),
|
||||
)
|
||||
.run(([0, 0, 0, 0], 8080))
|
||||
.await;
|
||||
).run((ip_address, port)).await;
|
||||
}
|
Loading…
Reference in New Issue
Block a user