lila-chat/src/main.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate log;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
2021-07-26 18:09:10 +00:00
use chat::accept_connection;
use rocket::fairing::AdHoc;
2021-07-26 18:09:10 +00:00
use rocket_contrib::serve::StaticFiles;
use std::io::Error;
use tokio::net::{TcpListener, TcpStream};
mod auth;
2021-07-17 23:41:41 +00:00
mod chat;
mod file_io;
mod message;
mod user;
2021-07-26 18:09:10 +00:00
#[tokio::main]
async fn startup_websocket() -> Result<(), Error> {
let addr = "127.0.0.1:1312".to_string();
// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;
let listener = try_socket.expect("Failed to bind");
info!("Listening on: {}", addr);
while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(accept_connection(stream));
}
Ok(())
}
fn main() {
env_logger::init();
2021-07-26 18:09:10 +00:00
// Start thread for tungstenite websockets
std::thread::spawn(|| startup_websocket());
let cors_fairing = AdHoc::on_response("CORS", |_, res| {
2021-07-22 18:23:59 +00:00
res.set_raw_header("Access-Control-Allow-Origin", "http://localhost:8000");
});
info!("Built CORS fairing");
2021-07-26 18:09:10 +00:00
info!("Started up rocket");
rocket::ignite()
.mount(
2021-07-18 17:16:00 +00:00
"/api",
routes![
auth::get_user,
auth::register,
2021-07-23 13:48:57 +00:00
auth::login,
2021-07-18 15:37:11 +00:00
chat::send_message,
2021-07-18 20:36:23 +00:00
chat::fetch_messages,
auth::change_info,
2021-07-22 18:23:59 +00:00
auth::check_token,
auth::logout,
auth::moderation_actions
],
)
2021-07-18 17:16:00 +00:00
.mount("/", StaticFiles::from("frontend"))
.attach(cors_fairing)
.launch();
}