add sqlx, anyhow, bcrypt and sessions

This commit is contained in:
2023-10-28 15:20:36 -04:00
parent bcdbcef636
commit 5e837e68ca
4 changed files with 369 additions and 7 deletions

View File

@@ -1,13 +1,17 @@
use anyhow::Result;
use bcrypt::{hash, verify, DEFAULT_COST};
use futures_util::{SinkExt, StreamExt};
use poem::{
get, handler,
listener::TcpListener,
session::{CookieConfig, MemoryStorage, ServerSession, Session},
web::{
websocket::{Message, WebSocket},
Data, Html, Path,
},
EndpointExt, IntoResponse, Route, Server,
};
use sqlx::SqlitePool;
#[handler]
fn index() -> Html<&'static str> {
@@ -91,18 +95,28 @@ fn ws(
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
async fn main() -> Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "poem=debug");
}
tracing_subscriber::fmt::init();
let app = Route::new().at("/api/", get(index)).at(
"/api/ws/:name",
get(ws.data(tokio::sync::broadcast::channel::<String>(32).0)),
);
let session = ServerSession::new(CookieConfig::default(), MemoryStorage::new());
let dbpool = SqlitePool::connect("sqlite:chat.db").await?;
let app = Route::new()
.at("/api/", get(index))
.at(
"/api/ws/:name",
get(ws.data(tokio::sync::broadcast::channel::<String>(32).0)),
)
.with(session)
.data(dbpool);
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
.await?;
Ok(())
}