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:
parent
79cfa25855
commit
cb3c1ee6e4
4 changed files with 41 additions and 50 deletions
|
@ -1,4 +1,5 @@
|
|||
pub mod messages;
|
||||
pub mod socket;
|
||||
|
||||
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};
|
||||
|
|
|
@ -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 futures_util::StreamExt as _;
|
||||
|
||||
#[get("{uuid}/channels/{channel_uuid}/socket")]
|
||||
pub async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||
let (res, mut session, stream) = actix_ws::handle(&req, stream)?;
|
||||
|
||||
|
@ -37,4 +38,4 @@ pub async fn echo(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse
|
|||
|
||||
// respond immediately with response connected to WS session
|
||||
Ok(res)
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ pub fn web() -> Scope {
|
|||
.service(channels::uuid::get)
|
||||
.service(channels::uuid::delete)
|
||||
.service(channels::uuid::messages::get)
|
||||
.service(channels::uuid::socket::echo)
|
||||
// Roles
|
||||
.service(roles::get)
|
||||
.service(roles::create)
|
||||
|
|
84
src/main.rs
84
src/main.rs
|
@ -2,15 +2,12 @@ use actix_cors::Cors;
|
|||
use actix_web::{web, App, HttpServer};
|
||||
use argon2::Argon2;
|
||||
use clap::Parser;
|
||||
use futures::try_join;
|
||||
use simple_logger::SimpleLogger;
|
||||
use sqlx::{PgPool, Pool, Postgres};
|
||||
use wss::echo;
|
||||
use std::time::SystemTime;
|
||||
mod config;
|
||||
use config::{Config, ConfigBuilder};
|
||||
mod api;
|
||||
mod wss;
|
||||
|
||||
pub mod utils;
|
||||
pub mod structs;
|
||||
|
@ -173,51 +170,42 @@ async fn main() -> Result<(), Error> {
|
|||
start_time: SystemTime::now(),
|
||||
};
|
||||
|
||||
try_join!(
|
||||
HttpServer::new(move || {
|
||||
// Set CORS headers
|
||||
let cors = Cors::default()
|
||||
/*
|
||||
Set Allowed-Control-Allow-Origin header to whatever
|
||||
the request's Origin header is. Must be done like this
|
||||
rather than setting it to "*" due to CORS not allowing
|
||||
sending of credentials (cookies) with wildcard origin.
|
||||
*/
|
||||
.allowed_origin_fn(|_origin, _req_head| {
|
||||
true
|
||||
})
|
||||
/*
|
||||
Allows any request method in CORS preflight requests.
|
||||
This will be restricted to only ones actually in use later.
|
||||
*/
|
||||
.allow_any_method()
|
||||
/*
|
||||
Allows any header(s) in request in CORS preflight requests.
|
||||
This wll be restricted to only ones actually in use later.
|
||||
*/
|
||||
.allow_any_header()
|
||||
/*
|
||||
Allows browser to include cookies in requests.
|
||||
This is needed for receiving the secure HttpOnly refresh_token cookie.
|
||||
*/
|
||||
.supports_credentials();
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(data.clone()))
|
||||
.wrap(cors)
|
||||
.service(api::web())
|
||||
})
|
||||
.bind((web.url, web.port))?
|
||||
.run(),
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/servers/{server_id}/channels/{channel_id}",
|
||||
web::get().to(echo))
|
||||
})
|
||||
.bind(("0.0.0.0", 4382))?
|
||||
.run()
|
||||
)?;
|
||||
HttpServer::new(move || {
|
||||
// Set CORS headers
|
||||
let cors = Cors::default()
|
||||
/*
|
||||
Set Allowed-Control-Allow-Origin header to whatever
|
||||
the request's Origin header is. Must be done like this
|
||||
rather than setting it to "*" due to CORS not allowing
|
||||
sending of credentials (cookies) with wildcard origin.
|
||||
*/
|
||||
.allowed_origin_fn(|_origin, _req_head| {
|
||||
true
|
||||
})
|
||||
/*
|
||||
Allows any request method in CORS preflight requests.
|
||||
This will be restricted to only ones actually in use later.
|
||||
*/
|
||||
.allow_any_method()
|
||||
/*
|
||||
Allows any header(s) in request in CORS preflight requests.
|
||||
This wll be restricted to only ones actually in use later.
|
||||
*/
|
||||
.allow_any_header()
|
||||
/*
|
||||
Allows browser to include cookies in requests.
|
||||
This is needed for receiving the secure HttpOnly refresh_token cookie.
|
||||
*/
|
||||
.supports_credentials();
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(data.clone()))
|
||||
.wrap(cors)
|
||||
.service(api::web())
|
||||
})
|
||||
.bind((web.url, web.port))?
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue