Skip to content

Commit

Permalink
fix(run-async): Run server async
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbcn committed Feb 25, 2022
1 parent 9897716 commit 8b104f9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.exclude": {
"target": true
"target": true,
"**/node_modules": true
}
}
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ edition = "2018"
structopt = "0.3.21"
chrono = "0.4"
tasklist-server = { path = "tasklist-server" }
tokio = { version = "1.17.0", features = ["full"] }
actix-web = "3.3.3"

[dependencies.rusqlite]
version = "0.25.1"
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ mod cli;
mod core;
mod db;

fn main() -> Result<()> {
#[actix_web::main]
async fn main() -> Result<()> {
let args = cli::Commands::from_args();

let _ = handle_command(args);
let _ = handle_command(args).await;

Ok(())
}

fn handle_command(cli: cli::Commands) -> Result<()> {
async fn handle_command(cli: cli::Commands) -> Result<()> {
match cli {
cli::Commands::Init => {
let _ = db::init();
Expand All @@ -36,9 +37,9 @@ fn handle_command(cli: cli::Commands) -> Result<()> {
cli::Tasks::Complete(cfg) => {
let _ = db::complete_task(cfg.id);
}
}
},
cli::Commands::Run => {
let _ = server::start(); //TODO .await
let _ = server::start().await;
}
}

Expand Down
1 change: 1 addition & 0 deletions tasklist-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ actix-files = "0.5.0"
actix-web = "3.3.3"
actix-web-static-files = "3.0.5"
actix-web-actors = "3.0.0"
sys-info = "0.9.1"

env_logger = "0.9"
log = "0.4"
Expand Down
1 change: 0 additions & 1 deletion tasklist-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ async fn websocket(req: HttpRequest, stream: web::Payload) -> Result<HttpRespons
)
}

#[actix_web::main]
pub async fn start() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

Expand Down
6 changes: 3 additions & 3 deletions tasklist-server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
ws::Message::Pong(_) => {
self.hb = Instant::now();
}
ws::Message::Text(_) => {
ctx.text(sys_info::hostname().unwrap());
}
ws::Message::Binary(_) => println!("Unexpected binary"),
ws::Message::Close(reason) => {
ctx.close(reason);
Expand All @@ -86,9 +89,6 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
ctx.stop();
}
ws::Message::Nop => (),
_ => {
log::error!("Recieved unexpected message");
},
}
}
}
5 changes: 4 additions & 1 deletion tasklist-server/web/src/lib/header/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
socket = new WebSocket('ws://localhost:8080/ws');
socket.addEventListener('open', () => {
status = 'connected';
socket.send('test');
socket.send('test'); //call to get hostname
});
socket.addEventListener('close', () => {
status = 'disconnected';
});
socket.addEventListener('error', () => {
status = 'disconnected';
});
socket.addEventListener('message', function (event) { //recieve hostname
status = event.data;
});
});
</script>

Expand Down

0 comments on commit 8b104f9

Please sign in to comment.