diff --git a/server/src/main.rs b/server/src/main.rs index 72a3898..ab9f334 100755 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -10,13 +10,13 @@ mod state; mod types; use routes::{get_posts, login, new_post, register, test}; -use state::AppState; +use state::ServerState; #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init(); - let data = AppState::new().await; + let data = ServerState::new().await; info!("Spinning up server on http://localhost:8080"); HttpServer::new(move || { diff --git a/server/src/routes.rs b/server/src/routes.rs index 95f212e..ef19c75 100755 --- a/server/src/routes.rs +++ b/server/src/routes.rs @@ -1,6 +1,6 @@ use crate::jwt::token_factory; use crate::types::{NewPost, Post}; -use crate::AppState; +use crate::ServerState; use actix_web::web::{Data, Path}; use actix_web::{get, post, web::Json, HttpResponse, Responder, Result}; @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; #[get("/")] -pub async fn get_posts(data: Data) -> impl Responder { +pub async fn get_posts(data: Data) -> impl Responder { match data.posts.lock() { Ok(posts) => { let posts: Vec = posts.values().cloned().collect(); @@ -29,7 +29,7 @@ pub async fn get_posts(data: Data) -> impl Responder { } #[post("/")] -pub async fn new_post(new_post: Json, data: Data) -> impl Responder { +pub async fn new_post(new_post: Json, data: Data) -> impl Responder { let post = Post::from(new_post.into_inner()); info!("Created post {:?}", post.uuid); @@ -54,7 +54,7 @@ pub async fn new_post(new_post: Json, data: Data) -> impl Res // This is a test route, returns "Hello, world!" #[get("/test")] -pub async fn test(data: Data) -> impl Responder { +pub async fn test(data: Data) -> impl Responder { match data.posts.lock() { Ok(posts) => { let posts: Vec = posts.values().cloned().collect(); @@ -77,7 +77,7 @@ pub enum VoteDirection { } #[post("vote/{uuid}/{direction}")] -pub async fn vote(params: Path<(Uuid, VoteDirection)>, data: Data) -> impl Responder { +pub async fn vote(params: Path<(Uuid, VoteDirection)>, data: Data) -> impl Responder { let (uuid, direction) = params.into_inner(); println!("Voting {:?} on post {:?}", direction, uuid); @@ -111,7 +111,10 @@ pub struct RegisterData { } #[post("/register")] -pub async fn register(data: Json, state: Data) -> Result { +pub async fn register( + data: Json, + state: Data, +) -> Result { let q = "SELECT password FROM users WHERE username = ?"; let query = sqlx::query(q).bind(&data.username); let result = query.fetch_one(&state.pool).await.ok(); @@ -151,7 +154,7 @@ struct LoginResponse { } #[post("/login")] -pub async fn login(data: Json, state: Data) -> Result { +pub async fn login(data: Json, state: Data) -> Result { let q = "SELECT password FROM users WHERE username = ?"; let query = sqlx::query(q).bind(&data.username); let result = query.fetch_one(&state.pool).await.ok(); diff --git a/server/src/state.rs b/server/src/state.rs index f59c98f..8699d93 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -8,12 +8,12 @@ use std::sync::Mutex; use uuid::Uuid; #[derive(Clone)] -pub struct AppState { +pub struct ServerState { pub posts: Arc>>, pub pool: Pool, } -impl AppState { +impl ServerState { pub async fn new() -> Self { let pool = sqlite::SqlitePoolOptions::new() .max_connections(5)