add sqlx, anyhow, bcrypt and sessions
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user