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 structopt::StructOpt;
|
||||||
use std::path::PathBuf;
|
use std::{
|
||||||
|
net::IpAddr,
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
/// Path to config
|
/// 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,
|
pub config: PathBuf,
|
||||||
|
|
||||||
/// Don't use config
|
/// Don't use config
|
||||||
@ -18,4 +21,12 @@ pub struct Options {
|
|||||||
/// Prints default config
|
/// Prints default config
|
||||||
#[structopt(long)]
|
#[structopt(long)]
|
||||||
pub generate_config: bool,
|
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,
|
Deserialize,
|
||||||
Serialize,
|
Serialize,
|
||||||
};
|
};
|
||||||
use std::path::PathBuf;
|
use std::{
|
||||||
|
net::IpAddr,
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::AsyncReadExt,
|
io::AsyncReadExt,
|
||||||
@ -13,6 +16,8 @@ use crate::cli::Options;
|
|||||||
#[serde(default, rename_all = "kebab-case")]
|
#[serde(default, rename_all = "kebab-case")]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub database_path: PathBuf,
|
pub database_path: PathBuf,
|
||||||
|
pub ip_address: IpAddr,
|
||||||
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
@ -38,6 +43,8 @@ impl Default for Config {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Config {
|
Config {
|
||||||
database_path: crate::DEFAULT_DATABASE_PATH.into(),
|
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::{
|
use std::{
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
error::Error,
|
error::Error,
|
||||||
|
net::{
|
||||||
|
IpAddr,
|
||||||
|
Ipv4Addr,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use crate::{cli::Options, config::Config};
|
use crate::{cli::Options, config::Config};
|
||||||
|
|
||||||
@ -12,7 +16,18 @@ mod net;
|
|||||||
mod sqlite;
|
mod sqlite;
|
||||||
mod graphql;
|
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_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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
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();
|
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);
|
dbg!(&config);
|
||||||
|
|
||||||
#[cfg(feature = "sqlite")]
|
#[cfg(feature = "sqlite")]
|
||||||
@ -38,7 +61,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
#[cfg(feature = "sqlite")]
|
#[cfg(feature = "sqlite")]
|
||||||
sqlite::load_template(&sqlite_connection).await?;
|
sqlite::load_template(&sqlite_connection).await?;
|
||||||
|
|
||||||
net::web(sqlite_connection).await;
|
net::web(config.ip_address, config.port, sqlite_connection).await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
13
src/net.rs
13
src/net.rs
@ -1,16 +1,13 @@
|
|||||||
|
use std::net::IpAddr;
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use warp::{
|
use warp::Filter;
|
||||||
Filter,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::graphql::{
|
use crate::graphql::{
|
||||||
Context,
|
Context,
|
||||||
schema,
|
schema,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub async fn web(ip_address: IpAddr, port: u16, db: SqlitePool) {
|
||||||
pub async fn web(db: SqlitePool){
|
|
||||||
|
|
||||||
std::env::set_var("RUST_LOG", "warp_async");
|
std::env::set_var("RUST_LOG", "warp_async");
|
||||||
let state = warp::any().map(move || Context { db: db.clone() });
|
let state = warp::any().map(move || Context { db: db.clone() });
|
||||||
let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed());
|
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(warp::path("graphiql"))
|
||||||
.and(juniper_warp::graphiql_filter("/graphql", None))
|
.and(juniper_warp::graphiql_filter("/graphql", None))
|
||||||
.or(warp::path("graphql").and(graphql_filter)),
|
.or(warp::path("graphql").and(graphql_filter)),
|
||||||
)
|
).run((ip_address, port)).await;
|
||||||
.run(([0, 0, 0, 0], 8080))
|
|
||||||
.await;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user