allow unix sockets

This commit is contained in:
Anna 2023-01-20 15:13:52 -05:00
parent b28d6667ea
commit 6b909ebf46
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
4 changed files with 26 additions and 4 deletions

1
server/Cargo.lock generated
View File

@ -1050,6 +1050,7 @@ dependencies = [
"sha3", "sha3",
"sqlx", "sqlx",
"tokio", "tokio",
"tokio-stream",
"toml", "toml",
"uuid", "uuid",
"warp", "warp",

View File

@ -18,5 +18,6 @@ sha3 = "0.10"
sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "sqlite", "chrono"] } sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "sqlite", "chrono"] }
toml = "0.5" toml = "0.5"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] } tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tokio-stream = { version = "0.1", default-features = false, features = ["net"] }
uuid = { version = "1", features = ["serde", "v4"] } uuid = { version = "1", features = ["serde", "v4"] }
warp = "0.3" warp = "0.3"

View File

@ -1,11 +1,10 @@
use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Config { pub struct Config {
pub address: SocketAddr, pub address: String,
pub packs: PathBuf, pub packs: PathBuf,
pub database: String, pub database: String,
} }

View File

@ -1,14 +1,21 @@
#![feature(drain_filter)] #![feature(drain_filter)]
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::Permissions;
use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt;
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use sqlx::{Executor, Pool, Sqlite}; use sqlx::{Executor, Pool, Sqlite};
use sqlx::migrate::Migrator; use sqlx::migrate::Migrator;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use tokio::fs::File;
use tokio::net::{TcpListener, UnixListener};
use tokio::runtime::Handle; use tokio::runtime::Handle;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tokio_stream::wrappers::{TcpListenerStream, UnixListenerStream};
use uuid::Uuid; use uuid::Uuid;
use crate::config::Config; use crate::config::Config;
@ -108,9 +115,23 @@ async fn main() -> Result<()> {
state.update_packs().await?; state.update_packs().await?;
spawn_command_reader(Arc::clone(&state), Handle::current()); spawn_command_reader(Arc::clone(&state), Handle::current());
let address = state.config.address;
let address = state.config.address.clone();
let server = warp::serve(web::routes(state));
println!("listening at {}", address); println!("listening at {}", address);
warp::serve(web::routes(state)).run(address).await;
if address.starts_with("unix:") {
let listener = UnixListener::bind(&address[5..])?;
let stream = UnixListenerStream::new(listener);
server.run_incoming(stream).await;
} else {
let addr = SocketAddr::from_str(&address)?;
let listener = TcpListener::bind(addr).await?;
let stream = TcpListenerStream::new(listener);
server.run_incoming(stream).await;
}
Ok(()) Ok(())
} }