Get to MVP
This commit is contained in:
parent
91186e5f43
commit
bbebe8b119
2 changed files with 26 additions and 8 deletions
|
@ -1,5 +1,7 @@
|
||||||
#[derive(Debug, serde::Serialize)]
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
pub author: Option<String>, // null is Anonymous
|
pub author: Option<String>, // None/null is Anonymous
|
||||||
pub text: String
|
pub text: String
|
||||||
}
|
}
|
||||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -4,30 +4,46 @@ pub use comment::Comment;
|
||||||
mod database;
|
mod database;
|
||||||
pub use database::Database;
|
pub use database::Database;
|
||||||
|
|
||||||
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
|
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
db: Mutex<Database>
|
db: Mutex<Database>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/comments")]
|
#[get("/")]
|
||||||
async fn get_comments(data: web::Data<AppState>) -> impl Responder {
|
async fn get_comments(data: web::Data<AppState>) -> impl Responder {
|
||||||
let db = &data.db.lock().unwrap();
|
let db = &data.db.lock().unwrap();
|
||||||
HttpResponse::Ok().json(&db.get_comments().unwrap())
|
HttpResponse::Ok().json(&db.get_comments().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[post("/")]
|
||||||
|
async fn post_comments(data: web::Data<AppState>, bytes: web::Bytes) -> impl Responder {
|
||||||
|
match String::from_utf8(bytes.to_vec()) {
|
||||||
|
Ok(text) => {
|
||||||
|
let db = match data.db.lock() {
|
||||||
|
Ok(db) => db,
|
||||||
|
Err(_) => return HttpResponse::InternalServerError(),
|
||||||
|
};
|
||||||
|
let comment: Comment = match serde_json::from_str(&text) {
|
||||||
|
Ok(comment) => comment,
|
||||||
|
Err(_) => return HttpResponse::BadRequest(),
|
||||||
|
};
|
||||||
|
db.create_comment(&comment).unwrap();
|
||||||
|
HttpResponse::Ok()
|
||||||
|
},
|
||||||
|
Err(_) => HttpResponse::BadRequest().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let db = Database::new().unwrap();
|
let db = Database::new().unwrap();
|
||||||
db.create_comment(&Comment {
|
|
||||||
author: None,
|
|
||||||
text: "This is anonymous test comment!".to_string(),
|
|
||||||
}).unwrap();
|
|
||||||
let state = web::Data::new(AppState { db: Mutex::new(db) });
|
let state = web::Data::new(AppState { db: Mutex::new(db) });
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.service(get_comments)
|
.service(get_comments)
|
||||||
|
.service(post_comments)
|
||||||
.app_data(state.clone())
|
.app_data(state.clone())
|
||||||
})
|
})
|
||||||
.bind(("127.0.0.1", 8080))?
|
.bind(("127.0.0.1", 8080))?
|
||||||
|
|
Loading…
Add table
Reference in a new issue