1
0
Fork 0
forked from gorb/backend

refactor: move websocket into existing webserver and folder structure

keeps things consistent and avoids having 2 webservers running under actix, can be reverted if its not desirable however i think this is the best option
This commit is contained in:
Radical 2025-05-15 11:57:47 +00:00
parent 79cfa25855
commit cb3c1ee6e4
4 changed files with 41 additions and 50 deletions

View file

@ -1,4 +1,5 @@
pub mod messages; pub mod messages;
pub mod socket;
use actix_web::{delete, get, web, Error, HttpRequest, HttpResponse}; use actix_web::{delete, get, web, Error, HttpRequest, HttpResponse};
use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data}; use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data};

View file

@ -1,7 +1,8 @@
use actix_web::{rt, web, Error, HttpRequest, HttpResponse}; use actix_web::{get, rt, web, Error, HttpRequest, HttpResponse};
use actix_ws::AggregatedMessage; use actix_ws::AggregatedMessage;
use futures_util::StreamExt as _; use futures_util::StreamExt as _;
#[get("{uuid}/channels/{channel_uuid}/socket")]
pub async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> { pub async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
let (res, mut session, stream) = actix_ws::handle(&req, stream)?; let (res, mut session, stream) = actix_ws::handle(&req, stream)?;

View file

@ -17,6 +17,7 @@ pub fn web() -> Scope {
.service(channels::uuid::get) .service(channels::uuid::get)
.service(channels::uuid::delete) .service(channels::uuid::delete)
.service(channels::uuid::messages::get) .service(channels::uuid::messages::get)
.service(channels::uuid::socket::echo)
// Roles // Roles
.service(roles::get) .service(roles::get)
.service(roles::create) .service(roles::create)

View file

@ -2,15 +2,12 @@ use actix_cors::Cors;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use argon2::Argon2; use argon2::Argon2;
use clap::Parser; use clap::Parser;
use futures::try_join;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use sqlx::{PgPool, Pool, Postgres}; use sqlx::{PgPool, Pool, Postgres};
use wss::echo;
use std::time::SystemTime; use std::time::SystemTime;
mod config; mod config;
use config::{Config, ConfigBuilder}; use config::{Config, ConfigBuilder};
mod api; mod api;
mod wss;
pub mod utils; pub mod utils;
pub mod structs; pub mod structs;
@ -173,51 +170,42 @@ async fn main() -> Result<(), Error> {
start_time: SystemTime::now(), start_time: SystemTime::now(),
}; };
try_join!( HttpServer::new(move || {
HttpServer::new(move || { // Set CORS headers
// Set CORS headers let cors = Cors::default()
let cors = Cors::default() /*
/* Set Allowed-Control-Allow-Origin header to whatever
Set Allowed-Control-Allow-Origin header to whatever the request's Origin header is. Must be done like this
the request's Origin header is. Must be done like this rather than setting it to "*" due to CORS not allowing
rather than setting it to "*" due to CORS not allowing sending of credentials (cookies) with wildcard origin.
sending of credentials (cookies) with wildcard origin. */
*/ .allowed_origin_fn(|_origin, _req_head| {
.allowed_origin_fn(|_origin, _req_head| { true
true })
}) /*
/* Allows any request method in CORS preflight requests.
Allows any request method in CORS preflight requests. This will be restricted to only ones actually in use later.
This will be restricted to only ones actually in use later. */
*/ .allow_any_method()
.allow_any_method() /*
/* Allows any header(s) in request in CORS preflight requests.
Allows any header(s) in request in CORS preflight requests. This wll be restricted to only ones actually in use later.
This wll be restricted to only ones actually in use later. */
*/ .allow_any_header()
.allow_any_header() /*
/* Allows browser to include cookies in requests.
Allows browser to include cookies in requests. This is needed for receiving the secure HttpOnly refresh_token cookie.
This is needed for receiving the secure HttpOnly refresh_token cookie. */
*/ .supports_credentials();
.supports_credentials();
App::new() App::new()
.app_data(web::Data::new(data.clone())) .app_data(web::Data::new(data.clone()))
.wrap(cors) .wrap(cors)
.service(api::web()) .service(api::web())
}) })
.bind((web.url, web.port))? .bind((web.url, web.port))?
.run(), .run()
.await?;
HttpServer::new(|| {
App::new()
.route("/servers/{server_id}/channels/{channel_id}",
web::get().to(echo))
})
.bind(("0.0.0.0", 4382))?
.run()
)?;
Ok(()) Ok(())
} }