Renamed everything called "App" to "Server"
This commit is contained in:
parent
107bb05d47
commit
3bc6e6928e
3 changed files with 14 additions and 11 deletions
|
@ -10,13 +10,13 @@ mod state;
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
use routes::{get_posts, login, new_post, register, test};
|
use routes::{get_posts, login, new_post, register, test};
|
||||||
use state::AppState;
|
use state::ServerState;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();
|
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");
|
info!("Spinning up server on http://localhost:8080");
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::jwt::token_factory;
|
use crate::jwt::token_factory;
|
||||||
use crate::types::{NewPost, Post};
|
use crate::types::{NewPost, Post};
|
||||||
use crate::AppState;
|
use crate::ServerState;
|
||||||
|
|
||||||
use actix_web::web::{Data, Path};
|
use actix_web::web::{Data, Path};
|
||||||
use actix_web::{get, post, web::Json, HttpResponse, Responder, Result};
|
use actix_web::{get, post, web::Json, HttpResponse, Responder, Result};
|
||||||
|
@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub async fn get_posts(data: Data<AppState>) -> impl Responder {
|
pub async fn get_posts(data: Data<ServerState>) -> impl Responder {
|
||||||
match data.posts.lock() {
|
match data.posts.lock() {
|
||||||
Ok(posts) => {
|
Ok(posts) => {
|
||||||
let posts: Vec<Post> = posts.values().cloned().collect();
|
let posts: Vec<Post> = posts.values().cloned().collect();
|
||||||
|
@ -29,7 +29,7 @@ pub async fn get_posts(data: Data<AppState>) -> impl Responder {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/")]
|
#[post("/")]
|
||||||
pub async fn new_post(new_post: Json<NewPost>, data: Data<AppState>) -> impl Responder {
|
pub async fn new_post(new_post: Json<NewPost>, data: Data<ServerState>) -> impl Responder {
|
||||||
let post = Post::from(new_post.into_inner());
|
let post = Post::from(new_post.into_inner());
|
||||||
info!("Created post {:?}", post.uuid);
|
info!("Created post {:?}", post.uuid);
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ pub async fn new_post(new_post: Json<NewPost>, data: Data<AppState>) -> impl Res
|
||||||
|
|
||||||
// This is a test route, returns "Hello, world!"
|
// This is a test route, returns "Hello, world!"
|
||||||
#[get("/test")]
|
#[get("/test")]
|
||||||
pub async fn test(data: Data<AppState>) -> impl Responder {
|
pub async fn test(data: Data<ServerState>) -> impl Responder {
|
||||||
match data.posts.lock() {
|
match data.posts.lock() {
|
||||||
Ok(posts) => {
|
Ok(posts) => {
|
||||||
let posts: Vec<Post> = posts.values().cloned().collect();
|
let posts: Vec<Post> = posts.values().cloned().collect();
|
||||||
|
@ -77,7 +77,7 @@ pub enum VoteDirection {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("vote/{uuid}/{direction}")]
|
#[post("vote/{uuid}/{direction}")]
|
||||||
pub async fn vote(params: Path<(Uuid, VoteDirection)>, data: Data<AppState>) -> impl Responder {
|
pub async fn vote(params: Path<(Uuid, VoteDirection)>, data: Data<ServerState>) -> impl Responder {
|
||||||
let (uuid, direction) = params.into_inner();
|
let (uuid, direction) = params.into_inner();
|
||||||
println!("Voting {:?} on post {:?}", direction, uuid);
|
println!("Voting {:?} on post {:?}", direction, uuid);
|
||||||
|
|
||||||
|
@ -111,7 +111,10 @@ pub struct RegisterData {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/register")]
|
#[post("/register")]
|
||||||
pub async fn register(data: Json<RegisterData>, state: Data<AppState>) -> Result<impl Responder> {
|
pub async fn register(
|
||||||
|
data: Json<RegisterData>,
|
||||||
|
state: Data<ServerState>,
|
||||||
|
) -> Result<impl Responder> {
|
||||||
let q = "SELECT password FROM users WHERE username = ?";
|
let q = "SELECT password FROM users WHERE username = ?";
|
||||||
let query = sqlx::query(q).bind(&data.username);
|
let query = sqlx::query(q).bind(&data.username);
|
||||||
let result = query.fetch_one(&state.pool).await.ok();
|
let result = query.fetch_one(&state.pool).await.ok();
|
||||||
|
@ -151,7 +154,7 @@ struct LoginResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/login")]
|
#[post("/login")]
|
||||||
pub async fn login(data: Json<LoginData>, state: Data<AppState>) -> Result<impl Responder> {
|
pub async fn login(data: Json<LoginData>, state: Data<ServerState>) -> Result<impl Responder> {
|
||||||
let q = "SELECT password FROM users WHERE username = ?";
|
let q = "SELECT password FROM users WHERE username = ?";
|
||||||
let query = sqlx::query(q).bind(&data.username);
|
let query = sqlx::query(q).bind(&data.username);
|
||||||
let result = query.fetch_one(&state.pool).await.ok();
|
let result = query.fetch_one(&state.pool).await.ok();
|
||||||
|
|
|
@ -8,12 +8,12 @@ use std::sync::Mutex;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct ServerState {
|
||||||
pub posts: Arc<Mutex<BTreeMap<Uuid, Post>>>,
|
pub posts: Arc<Mutex<BTreeMap<Uuid, Post>>>,
|
||||||
pub pool: Pool<Sqlite>,
|
pub pool: Pool<Sqlite>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl ServerState {
|
||||||
pub async fn new() -> Self {
|
pub async fn new() -> Self {
|
||||||
let pool = sqlite::SqlitePoolOptions::new()
|
let pool = sqlite::SqlitePoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
|
|
Loading…
Reference in a new issue