Skip to content

Commit

Permalink
Merge pull request #61 from polyphony-chat/gateway
Browse files Browse the repository at this point in the history
Gateway
  • Loading branch information
bitfl0wer authored Oct 22, 2024
2 parents 092e1e9 + f81e7f2 commit 2484f88
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 41 deletions.
10 changes: 5 additions & 5 deletions fixtures/notes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ INSERT INTO users (
mfa_enabled, webauthn_enabled, totp_secret, totp_last_ticket, created_at,
premium_since, verified, disabled, deleted, email, flags, public_flags,
purchased_flags, premium_usage_flags, rights, data, fingerprints, extended_settings,
settings_index, relevant_events
settings_index
)
VALUES
(7250861145186111490, 'john_doe_notes', '1296', 'avatar1.png', 16777215, 'banner1.png', NULL,
'he/him', '+1234567890', true, false, true, 1, false, 'This is my bio', false,
true, true, true, 'secret123', 'ticket456', '2024-01-01 12:00:00',
'2024-01-02 12:00:00', true, false, false, '[email protected]', 100, 10,
5, 1, 100, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint1', '{"setting": "value"}', 7251169922837909504,
'[]'),
5, 1, 100, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint1', '{"setting": "value"}', 7251169922837909504
),
(7250861145186111491, 'jane_smith_notes', '5678', 'avatar2.png', 123456, NULL, NULL,
'she/her', NULL, false, true, false, 0, false, '', false,
false, false, false, NULL, NULL, '2024-02-01 15:30:00',
NULL, false, false, false, '[email protected]', 50, 5,
0, 0, 50, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint2', '{"setting": "another_value"}', 7251169922837909505,
'[]');
0, 0, 50, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint2', '{"setting": "another_value"}', 7251169922837909505
);
COMMIT;

INSERT INTO notes (
Expand Down
10 changes: 5 additions & 5 deletions fixtures/private_channels.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ INSERT INTO users (
mfa_enabled, webauthn_enabled, totp_secret, totp_last_ticket, created_at,
premium_since, verified, disabled, deleted, email, flags, public_flags,
purchased_flags, premium_usage_flags, rights, data, fingerprints, extended_settings,
settings_index, relevant_events
settings_index
)
VALUES
(7250861145186111490, 'john_doe_private_channels', '1296', 'avatar1.png', 16777215, 'banner1.png', NULL,
'he/him', '+1234567890', true, false, true, 1, false, 'This is my bio', false,
true, true, true, 'secret123', 'ticket456', '2024-01-01 12:00:00',
'2024-01-02 12:00:00', true, false, false, '[email protected]', 100, 10,
5, 1, 100, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint1', '{"setting": "value"}', 7250861145186111495,
'[]'),
5, 1, 100, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint1', '{"setting": "value"}', 7250861145186111495
),
(7250861145186111491, 'jane_smith_private_channels', '5678', 'avatar2.png', 123456, NULL, NULL,
'she/her', NULL, false, true, false, 0, false, '', false,
false, false, false, NULL, NULL, '2024-02-01 15:30:00',
NULL, false, false, false, '[email protected]', 50, 5,
0, 0, 50, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint2', '{"setting": "another_value"}', 7250861145186111496,
'[]');
0, 0, 50, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint2', '{"setting": "another_value"}', 7250861145186111496
);
COMMIT;

INSERT INTO channels (
Expand Down
22 changes: 11 additions & 11 deletions fixtures/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,37 @@ INSERT INTO users (
mfa_enabled, webauthn_enabled, totp_secret, totp_last_ticket, created_at,
premium_since, verified, disabled, deleted, email, flags, public_flags,
purchased_flags, premium_usage_flags, rights, data, fingerprints, extended_settings,
settings_index, relevant_events
settings_index
)
VALUES
(7248639845155737600, 'john_doe', '1296', 'avatar1.png', 16777215, 'banner1.png', NULL,
'he/him', '+1234567890', true, false, true, 1, false, 'This is my bio', false,
true, true, true, 'secret123', 'ticket456', '2024-01-01 12:00:00',
'2024-01-02 12:00:00', true, false, false, '[email protected]', 100, 10,
5, 1, 100, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint1', '{"setting": "value"}', 1,
'[]'),
5, 1, 100, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint1', '{"setting": "value"}', 1
),
(7248639891561517057, 'jane_smith', '5678', 'avatar2.png', 123456, NULL, NULL,
'she/her', NULL, false, true, false, 0, false, '', false,
false, false, false, NULL, NULL, '2024-02-01 15:30:00',
NULL, false, false, false, '[email protected]', 50, 5,
0, 0, 50, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint2', '{"setting": "another_value"}', 2,
'[]'),
0, 0, 50, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint2', '{"setting": "another_value"}', 2
),
(7248640296244744192, 'alice_malice', '9876', 'avatar3.png', 987654, 'banner3.png', NULL,
'they/them', '+1230987654', true, true, false, 0, false, 'Life is an adventure',
false, false, true, false, 'secret789', 'ticket123', '2024-03-01 08:45:00',
NULL, false, false, false, '[email protected]', 200, 20,
2, 1, 150, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint3', '{"setting3": "value3"}', 3,
'[]'),
2, 1, 150, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint3', '{"setting3": "value3"}', 3
),
(7248640311927246848, 'bob_the_woz', '1122', 'avatar4.png', 556677, NULL, NULL,
'he/him', NULL, true, false, true, 1, false, 'Building the future',
false, true, false, true, 'secret456', 'ticket789', '2024-03-15 14:30:00',
'2024-04-01 14:30:00', true, false, false, '[email protected]', 250, 25,
3, 2, 200, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint4', '{"setting4": "value4"}', 4,
'[]'),
3, 2, 200, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint4', '{"setting4": "value4"}', 4
),
(7248640327265816576, 'xenia', '3344', 'avatar5.png', NULL, 'banner5.png', NULL,
'he/him', '+4567890123', false, true, false, 0, false, 'Chocolate is life',
false, false, false, true, 'secret555', 'ticket987', '2024-04-10 10:00:00',
NULL, false, false, false, '[email protected]', 180, 15,
0, 0, 180, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint5', '{"setting5": "value5"}', 5,
'[]');
0, 0, 180, '{"hash": "$2b$14$AbcNYTh5GcOYaB4v4P7OWeGW0hQkd/ysvmY4fGyIA/tyEnK0chGNu", "valid_tokens_since": "2024-10-07T20:22:06.162070616Z"}', 'fingerprint5', '{"setting5": "value5"}', 5
);
COMMIT;
1 change: 0 additions & 1 deletion migrations/20231007011757_users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ create table if not exists users (
fingerprints text not null,
extended_settings json not null,
settings_index numeric(20, 0) null constraint chk_settings_index_range check (settings_index >= 0 AND settings_index <= 18446744073709551615),
relevant_events json not null default '[]',
constraint users_settings_index_uindex unique (settings_index),
constraint users_user_settings_index_fk foreign key (settings_index) references user_settings (index),
constraint check_theme_colors_elements check (
Expand Down
30 changes: 22 additions & 8 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

static DEFAULT_API_BIND: &str = "0.0.0.0:3001";

use poem::middleware::Cors;
use poem::{
listener::TcpListener,
middleware::{NormalizePath, TrailingSlash},
web::Json,
EndpointExt, IntoResponse, Route, Server,
};
use reqwest::Method;
use serde_json::json;
use sqlx::PgPool;

Expand All @@ -31,6 +33,13 @@ use crate::{
mod middleware;
mod routes;

lazy_static::lazy_static! {
pub static ref BIND_API: String = std::env::var("API_BIND").unwrap_or_else(|_| {
log::warn!(target: "symfonia::db", "You did not specify API_BIND environment variable. Defaulting to '{DEFAULT_API_BIND}'.");
DEFAULT_API_BIND.to_string()
});
}

pub async fn start_api(
db: PgPool,
connected_users: ConnectedUsers,
Expand Down Expand Up @@ -86,25 +95,30 @@ pub async fn start_api(
.data(config)
.data(connected_users)
.with(NormalizePath::new(TrailingSlash::Trim))
.with(Cors::new().allow_methods(&[
Method::CONNECT,
Method::DELETE,
Method::GET,
Method::HEAD,
Method::OPTIONS,
Method::PATCH,
Method::POST,
Method::PUT,
Method::TRACE,
]))
.catch_all_error(custom_error);

let bind = &std::env::var("API_BIND").unwrap_or_else(|_| {
log::warn!(target: "symfonia::db", "You did not specify API_BIND environment variable. Defaulting to '{DEFAULT_API_BIND}'.");
DEFAULT_API_BIND.to_string()
});
let bind_clone = bind.clone();

log::info!(target: "symfonia::api", "Starting HTTP Server");

tokio::task::spawn(async move {
Server::new(TcpListener::bind(bind_clone))
Server::new(TcpListener::bind(BIND_API.to_string()))
.run(v9_api)
.await
.expect("Failed to start HTTP server");
log::info!(target: "symfonia::api", "HTTP Server stopped");
});

log::info!(target: "symfonia::api", "HTTP Server listening on {bind}");
log::info!(target: "symfonia::api", "HTTP Server listening on {}", BIND_API.as_str());
Ok(())
}

Expand Down
22 changes: 12 additions & 10 deletions src/api/routes/policies/instance/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ pub async fn domain(
Data(db): Data<&sqlx::PgPool>,
Data(cfg): Data<&Config>,
) -> Result<impl IntoResponse, APIError> {
let cdn = if let Some(endpoint) = &cfg.cdn.endpoint_public {
endpoint.to_owned()
} else if let Ok(endpoint) = std::env::var("CDN") {
let cdn = if let Ok(endpoint) = std::env::var("CDN") {
endpoint
} else if let Some(endpoint) = &cfg.cdn.endpoint_public {
endpoint.to_owned()
} else {
"http://localhost:3001".to_string()
"http://localhost:3002".to_string()
};

let gateway = if let Some(endpoint) = &cfg.gateway.endpoint_public {
endpoint.to_owned()
} else if let Ok(endpoint) = std::env::var("GATEWAY") {
let gateway = if let Ok(endpoint) = std::env::var("GATEWAY") {
endpoint
} else if let Some(endpoint) = &cfg.gateway.endpoint_public {
endpoint.to_owned()
} else {
"ws://localhost:3001".to_string()
"http://localhost:3003".to_string()
};

let api = if let Some(endpoint) = &cfg.api.endpoint_public {
let api = if let Ok(endpoint) = std::env::var("API") {
endpoint
} else if let Some(endpoint) = &cfg.api.endpoint_public {
endpoint.to_owned()
} else {
"http://localhost:3001/api".to_string()
"http://localhost:3001".to_string()
};

Ok(Json(json!({
Expand Down
1 change: 0 additions & 1 deletion src/database/entities/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub struct User {
#[sqlx(skip)]
#[serde(skip)]
pub publisher: SharedEventPublisher,
pub relevant_events: sqlx::types::Json<Vec<Snowflake>>,
}

impl Deref for User {
Expand Down
29 changes: 29 additions & 0 deletions src/logo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub(super) fn print_logo() {
let logo = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%***#%%%%%%%%%%%%%%%%%#**%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%#----=++**#######*++=----#%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%#*+=--------------=++*%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%#===*#%%%%%%##***++****#%%%%%%#*===*%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%=-----=+*#%%%%%%%%%%%%%%%%#*+=-----=%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%*=--------=++**####***+==-------=*#%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%#*+=----------------------=+*#%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%#=---+*#%%%%%%%%#*+==----------==+**#%%%%%%%%*+=--=#%%%%%%%%%
%%%%%%%%%--------=*#%%%%%%%%%%%%#######%%%%%%%%%%%#*+--------%%%%%%%%%
%%%%%%%%%+----------=+*##%%%%%%%%%%%%%%%%%%%%##*+=----------+%%%%%%%%%
%%%%%%%%%%#*=------------==+**##########**++=------------=*#%%%%%%%%%%
%%%%%%%%%%%%%#*+=------------------------------------=+*#%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%#*+=----------------------------=+*#%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%#*++=----------------=++*#%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##********###%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
";
for line in logo.lines() {
log::info!(target: "symfonia", "{line}")
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use log4rs::{
filter::Filter,
Config,
};
use logo::print_logo;
use parking_lot::RwLock;
use pubserve::Publisher;
use tokio::sync::Mutex;
Expand All @@ -40,6 +41,7 @@ mod cdn;
mod database;
mod errors;
mod gateway;
mod logo;
mod util;

pub type SharedEventPublisher = Arc<RwLock<Publisher<Event>>>;
Expand Down Expand Up @@ -179,6 +181,7 @@ async fn main() {
let _handle = log4rs::init_config(config).unwrap();

log::info!(target: "symfonia", "Starting up Symfonia");
print_logo();

match loglevel {
LevelFilter::Debug | LevelFilter::Trace => {
Expand Down

0 comments on commit 2484f88

Please sign in to comment.