mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-01 07:58:23 +00:00
begin testing websockets
This commit is contained in:
1 parent
fe7b8a8d07
commit
39906ae9c2
9 files changed
+157
-150
No files matched your search
Generated
+13
-90
@@ -107,26 +107,6 @@ dependencies = [
|
||||
"smallvec 1.6.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-files"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c51e8a9146c12fce92a6e4c24b8c4d9b05268130bfd8d61bc587e822c32ce689"
|
||||
dependencies = [
|
||||
"actix-service 1.0.6",
|
||||
"actix-web 3.3.2",
|
||||
"bitflags",
|
||||
"bytes 0.5.6",
|
||||
"derive_more",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"log",
|
||||
"mime",
|
||||
"mime_guess",
|
||||
"percent-encoding 2.1.0",
|
||||
"v_htmlescape",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-files"
|
||||
version = "0.6.0-beta.8"
|
||||
@@ -634,7 +614,7 @@ checksum = "796540673305a66d127804eef19ad696f1f204b8c1025aaca4958c17eab32877"
|
||||
dependencies = [
|
||||
"getrandom 0.2.2",
|
||||
"once_cell",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -645,7 +625,7 @@ checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98"
|
||||
dependencies = [
|
||||
"getrandom 0.2.2",
|
||||
"once_cell",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -923,15 +903,6 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "buf-min"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa17aa1cf56bdd6bb30518767d00e58019d326f3f05d8c3e0730b549d332ea83"
|
||||
dependencies = [
|
||||
"bytes 0.5.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "build_const"
|
||||
version = "0.2.2"
|
||||
@@ -1120,7 +1091,7 @@ checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951"
|
||||
dependencies = [
|
||||
"percent-encoding 2.1.0",
|
||||
"time 0.2.26",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1131,7 +1102,7 @@ checksum = "d5f1c7727e460397e56abc4bddc1d49e07a1ad78fc98eb2e1c8f032a58a2f80d"
|
||||
dependencies = [
|
||||
"percent-encoding 2.1.0",
|
||||
"time 0.2.26",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1653,7 +1624,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2474,16 +2445,6 @@ dependencies = [
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "4.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"version_check 0.1.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "6.1.2"
|
||||
@@ -2494,7 +2455,7 @@ dependencies = [
|
||||
"funty",
|
||||
"lexical-core",
|
||||
"memchr",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2623,7 +2584,7 @@ version = "0.0.1"
|
||||
dependencies = [
|
||||
"actix",
|
||||
"actix-cors 0.5.4",
|
||||
"actix-files 0.5.0",
|
||||
"actix-files",
|
||||
"actix-http 3.0.0-beta.11",
|
||||
"actix-rt 2.4.0",
|
||||
"actix-service 2.0.1",
|
||||
@@ -3677,7 +3638,7 @@ checksum = "6d86e3c77ff882a828346ba401a7ef4b8e440df804491c6064fe8295765de71c"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"maplit",
|
||||
"nom 6.1.2",
|
||||
"nom",
|
||||
"regex",
|
||||
"unicode_categories",
|
||||
]
|
||||
@@ -3876,7 +3837,7 @@ version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e113fb6f3de07a243d434a56ec6f186dfd51cb08448239fe7bcae73f87ff28ff"
|
||||
dependencies = [
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3940,7 +3901,7 @@ version = "0.0.1"
|
||||
dependencies = [
|
||||
"actix",
|
||||
"actix-cors 0.6.0-beta.3",
|
||||
"actix-files 0.6.0-beta.8",
|
||||
"actix-files",
|
||||
"actix-http 3.0.0-beta.11",
|
||||
"actix-multipart",
|
||||
"actix-rt 2.4.0",
|
||||
@@ -4145,7 +4106,7 @@ dependencies = [
|
||||
"standback",
|
||||
"stdweb",
|
||||
"time-macros",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
@@ -4588,7 +4549,7 @@ dependencies = [
|
||||
"twitch_oauth2",
|
||||
"typed-builder",
|
||||
"url 2.2.2",
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4656,7 +4617,7 @@ version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
|
||||
dependencies = [
|
||||
"version_check 0.9.3",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4754,50 +4715,12 @@ dependencies = [
|
||||
"getrandom 0.2.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "v_escape"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3e0ab5fab1db278a9413d2ea794cb66f471f898c5b020c3c394f6447625d9d4"
|
||||
dependencies = [
|
||||
"buf-min",
|
||||
"v_escape_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "v_escape_derive"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c860ad1273f4eee7006cee05db20c9e60e5d24cba024a32e1094aa8e574f3668"
|
||||
dependencies = [
|
||||
"nom 4.2.3",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "v_htmlescape"
|
||||
version = "0.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f9a8af610ad6f7fc9989c9d2590d9764bc61f294884e9ee93baa58795174572"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"v_escape",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cbdbff6266a24120518560b5dc983096efb98462e51d0d68169895b237be3e5d"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
|
||||
@@ -72,7 +72,7 @@ lru_time_cache = "0.11.11"
|
||||
# Web
|
||||
actix = "0.12.0"
|
||||
actix-cors = "0.5.4"
|
||||
actix-files= "0.5"
|
||||
actix-files= "0.6.0-beta.8"
|
||||
actix-http = "3.0.0-beta.11"
|
||||
actix-rt = "2.4.0"
|
||||
actix-service = "2.0.1"
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod obs_gateway_websocket_handler;
|
||||
pub mod pubsub_gateway;
|
||||
@@ -0,0 +1,95 @@
|
||||
use actix::prelude::*;
|
||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web_actors::ws;
|
||||
use crate::server_state::ObsGatewayServerState;
|
||||
use log::error;
|
||||
use log::warn;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Endpoint
|
||||
pub async fn obs_gateway_websocket_handler(
|
||||
request: HttpRequest,
|
||||
server_state: web::Data<Arc<ObsGatewayServerState>>,
|
||||
stream: web::Payload,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
error!(">>>>>> obs_ws_index");
|
||||
let websocket = ObsGatewayWebSocket::new();
|
||||
ws::start(websocket, &request, stream)
|
||||
}
|
||||
|
||||
/// Websocket behavior
|
||||
struct ObsGatewayWebSocket {}
|
||||
|
||||
impl ObsGatewayWebSocket {
|
||||
fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Actor for ObsGatewayWebSocket {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
|
||||
fn started(&mut self, _ctx: &mut Self::Context) {
|
||||
error!(">>>>>> obs actor started");
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for ObsGatewayWebSocket {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
if let Ok(msg) = msg {
|
||||
error!(">>>>>> obs streamhandler::handle (msg = {:?})", msg);
|
||||
match msg {
|
||||
ws::Message::Text(text) => ctx.text(text),
|
||||
ws::Message::Binary(bin) => ctx.binary(bin),
|
||||
ws::Message::Ping(bytes) => ctx.pong(&bytes),
|
||||
ws::Message::Close(reason) => {
|
||||
ctx.close(reason);
|
||||
ctx.stop();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
error!(">>>>>> obs streamhandler::STOP");
|
||||
ctx.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// From the Actix homepage:
|
||||
|
||||
//use actix::{Actor, StreamHandler};
|
||||
//use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
//use actix_web_actors::ws;
|
||||
//
|
||||
///// Define HTTP actor
|
||||
//struct MyWs;
|
||||
//
|
||||
//impl Actor for MyWs {
|
||||
// type Context = ws::WebsocketContext<Self>;
|
||||
//}
|
||||
//
|
||||
///// Handler for ws::Message message
|
||||
//impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
|
||||
// fn handle(
|
||||
// &mut self,
|
||||
// msg: Result<ws::Message, ws::ProtocolError>,
|
||||
// ctx: &mut Self::Context,
|
||||
// ) {
|
||||
// match msg {
|
||||
// Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
|
||||
// Ok(ws::Message::Text(text)) => ctx.text(text),
|
||||
// Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
|
||||
// _ => (),
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//pub async fn twitch_pubsub_gateway(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||
// let resp = ws::start(MyWs {}, &req, stream);
|
||||
// println!("{:?}", resp);
|
||||
// resp
|
||||
//}
|
||||
File renamed without changes.
@@ -13,7 +13,7 @@
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
pub mod endpoints;
|
||||
pub mod pubsub_gateway;
|
||||
pub mod endpoints_ws;
|
||||
pub mod server_state;
|
||||
pub mod twitch;
|
||||
pub mod util;
|
||||
@@ -26,7 +26,11 @@ use config::shared_constants::DEFAULT_MYSQL_CONNECTION_STRING;
|
||||
use config::shared_constants::DEFAULT_REDIS_CONNECTION_STRING;
|
||||
use config::shared_constants::DEFAULT_RUST_LOG;
|
||||
use crate::endpoints::oauth_begin::oauth_begin_enroll;
|
||||
use crate::pubsub_gateway::ws_index;
|
||||
use crate::endpoints::oauth_begin_redirect::oauth_begin_enroll_redirect;
|
||||
use crate::endpoints::oauth_end::oauth_end_enroll_from_redirect;
|
||||
use crate::endpoints_ws::obs_gateway_websocket_handler::obs_gateway_websocket_handler;
|
||||
use crate::endpoints_ws::pubsub_gateway::ws_index;
|
||||
use crate::server_state::{ObsGatewayServerState, EnvConfig, TwitchOauthSecrets, TwitchOauthTemp};
|
||||
use crate::twitch::twitch_client_wrapper::TwitchClientWrapper;
|
||||
use crate::twitch::twitch_secrets::TwitchSecrets;
|
||||
use crate::twitch::websocket_client::PollingTwitchWebsocketClient;
|
||||
@@ -48,9 +52,6 @@ use twitch_api2::pubsub::Topic;
|
||||
use twitch_api2::pubsub;
|
||||
use twitch_oauth2::tokens::UserTokenBuilder;
|
||||
use twitch_oauth2::{AppAccessToken, Scope, TwitchToken, tokens::errors::AppAccessTokenError, ClientId, ClientSecret};
|
||||
use crate::server_state::{ObsGatewayServerState, EnvConfig, TwitchOauthSecrets};
|
||||
use crate::endpoints::oauth_begin_redirect::oauth_begin_enroll_redirect;
|
||||
use crate::endpoints::oauth_end::oauth_end_enroll_from_redirect;
|
||||
|
||||
const DEFAULT_BIND_ADDRESS : &'static str = "0.0.0.0:54321";
|
||||
|
||||
@@ -107,59 +108,9 @@ async fn main() -> AnyhowResult<()> {
|
||||
|
||||
|
||||
// // ==================== OAUTH FLOW ====================
|
||||
//
|
||||
// println!("Oauth flow...");
|
||||
//
|
||||
// let redirect_url = twitch_oauth2::url::Url::parse("http://localhost/test")?;
|
||||
// let mut builder = UserTokenBuilder::new(
|
||||
// client_id.clone(),
|
||||
// client_secret.clone(),
|
||||
// redirect_url)
|
||||
// .set_scopes(Scope::all())
|
||||
// .force_verify(true);
|
||||
//
|
||||
// //builder.add_scope(Scope::BitsRead);
|
||||
//
|
||||
// let (url, _csrf_token) = builder.generate_url();
|
||||
//
|
||||
// println!("Go to this page: {}", url);
|
||||
//
|
||||
//
|
||||
//
|
||||
// let input = rpassword::prompt_password_stdout(
|
||||
// "Paste in the resulting adress after authenticating (input hidden): ",
|
||||
// )?;
|
||||
//
|
||||
// let u = twitch_oauth2::url::Url::parse(&input)?;
|
||||
//
|
||||
// let map: std::collections::HashMap<_, _> = u.query_pairs().collect();
|
||||
//
|
||||
// let user_token = match (map.get("state"), map.get("code")) {
|
||||
// (Some(state), Some(code)) => {
|
||||
// let token = builder
|
||||
// .get_user_token(
|
||||
// &reqwest::Client::builder()
|
||||
// .redirect(reqwest::redirect::Policy::none())
|
||||
// .build()?,
|
||||
// state,
|
||||
// code,
|
||||
// )
|
||||
// .await?;
|
||||
// println!("Got token: {:?}", token);
|
||||
//
|
||||
// token
|
||||
// }
|
||||
// _ => match (map.get("error"), map.get("error_description")) {
|
||||
// (std::option::Option::Some(error), std::option::Option::Some(error_description)) => {
|
||||
// anyhow::bail!(
|
||||
// "twitch errored with error: {} - {}",
|
||||
// error,
|
||||
// error_description
|
||||
// );
|
||||
// }
|
||||
// _ => anyhow::bail!("invalid url passed"),
|
||||
// },
|
||||
// };
|
||||
|
||||
|
||||
|
||||
//
|
||||
// let auth_token = user_token.access_token.as_str();
|
||||
//
|
||||
@@ -267,6 +218,10 @@ async fn main() -> AnyhowResult<()> {
|
||||
"TWITCH_OAUTH_REDIRECT_URL",
|
||||
"http://localhost:54321/twitch/oauth_redirect");
|
||||
|
||||
// TODO: These are temporary.
|
||||
let temp_oauth_access_token = easyenv::get_env_string_or_default("TEMP_TWITCH_OAUTH_ACCESS", "");
|
||||
let temp_oauth_refresh_token = easyenv::get_env_string_or_default("TEMP_TWITCH_OAUTH_REFRESH", "");
|
||||
|
||||
let server_state = ObsGatewayServerState {
|
||||
env_config: EnvConfig {
|
||||
num_workers,
|
||||
@@ -281,6 +236,10 @@ async fn main() -> AnyhowResult<()> {
|
||||
client_secret: secrets.app_client_secret.clone(),
|
||||
redirect_url: oauth_redirect_url,
|
||||
},
|
||||
twitch_oauth_temp: TwitchOauthTemp {
|
||||
temp_oauth_access_token,
|
||||
temp_oauth_refresh_token,
|
||||
},
|
||||
hostname: server_hostname,
|
||||
};
|
||||
|
||||
@@ -333,7 +292,16 @@ pub async fn serve(server_state: ObsGatewayServerState) -> AnyhowResult<()>
|
||||
.route(web::head().to(|| HttpResponse::Ok()))
|
||||
)
|
||||
)
|
||||
//.default_service( web::route().to(default_route_404))
|
||||
.service(web::resource("/obs")
|
||||
.route(web::get().to(obs_gateway_websocket_handler))
|
||||
.route(web::head().to(|| HttpResponse::Ok()))
|
||||
)
|
||||
.service(
|
||||
actix_files::Files::new("/static", "static")
|
||||
.show_files_listing()
|
||||
.use_last_modified(true),
|
||||
)
|
||||
//.default_service( web::route().to(default_route_404))
|
||||
})
|
||||
.bind(bind_address)?
|
||||
.workers(num_workers)
|
||||
|
||||
@@ -8,6 +8,7 @@ pub struct ObsGatewayServerState {
|
||||
pub env_config: EnvConfig,
|
||||
pub hostname: String,
|
||||
pub twitch_oauth_secrets: TwitchOauthSecrets,
|
||||
pub twitch_oauth_temp: TwitchOauthTemp,
|
||||
}
|
||||
|
||||
// TODO: Many of these do not need to be passed around past server init.
|
||||
@@ -29,3 +30,12 @@ pub struct TwitchOauthSecrets {
|
||||
pub client_secret: String,
|
||||
pub redirect_url: String,
|
||||
}
|
||||
|
||||
/// TODO: THIS IS JUST FOR FEATURE DEVELOPMENT. KILL THIS.
|
||||
/// Being lazy here until I have a decent DB model and know what I want to build.
|
||||
#[deprecated]
|
||||
#[derive(Clone)]
|
||||
pub struct TwitchOauthTemp {
|
||||
pub temp_oauth_access_token: String,
|
||||
pub temp_oauth_refresh_token: String,
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,9 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Websocket Test</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Websocket Test</h1>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user