Port now defaults to 4536 and is configurable with -p

This commit is contained in:
shockrah 2021-02-01 16:47:43 -08:00
parent 1e6a9ac844
commit 2ed07a519e
3 changed files with 18 additions and 9 deletions

View File

@ -10,5 +10,5 @@ REDIS_URL=redis://127.0.0.1:6379
# Server meta things
SERVER_NAME="Freechat Dev Server"
SERVER_DESCRIPTION="Server for sick development things"
SERVER_URL=localhost
SERVER_PORT=8888
SERVER_HOSTNAME=localhost
SERVER_PORT=4536

View File

@ -112,9 +112,9 @@ async fn shutdown_signal() {
.expect("Failed to capture ctrl-c signal");
}
async fn start_server(ecode: u16) -> u16 {
println!("Servering on localhost:8888");
let addr = SocketAddr::from(([127,0,0,1], 8888));
async fn start_server(ecode: u16, port: u16) -> u16 {
println!("Servering on localhost:{}", port);
let addr = SocketAddr::from(([127,0,0,1], port));
let service = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(main_responder))
});
@ -196,6 +196,11 @@ async fn main() -> Result<(), u16>{
.short("s")
.long("server")
.help("Starts the API server"))
.arg(Arg::with_name("port")
.short("p")
.long("port")
.default_value("4536")
.help("Set the port to use: Default is 4536"))
.get_matches();
@ -221,13 +226,17 @@ OPTIONS:
set_var("DATABASE_URL", db_url);
}
// safe because we have a default value set in code
let port = args.value_of("port").unwrap().to_string();
let port: u16 = port.parse().unwrap_or(4536);
if let Some(owner_name) = args.value_of("create-owner") {
attempt_owner_creation(owner_name).await;
}
if args.is_present("server") {
if main_ret == NO_ERR {
main_ret = start_server(main_ret).await;
main_ret = start_server(main_ret, port).await;
}
}

View File

@ -9,7 +9,7 @@ use serde::Serialize;
struct Config {
name: String,
description: String,
url: String,
hostname: String,
port: u16
}
@ -18,8 +18,8 @@ pub async fn server_meta(response: &mut Response<Body>) {
let payload = Config {
name: var("SERVER_NAME").unwrap_or("No name".into()),
description: var("SERVER_DESCRIPTION").unwrap_or("No description".into()),
url: var("SERVER_URL").expect("Couldn't get url from environment"),
hostname: var("SERVER_HOSTNAME").expect("Couldn't get url from environment"),
port: var("SERVER_PORT").expect("Couldn't get port from environment").parse::<u16>().unwrap(),
};
*response.body_mut() = Body::from(to_string(&payload).unwrap());
}
}