Want to contribute? Fork me on Codeberg.org!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
908 B

mod comment;
pub use comment::Comment;
mod database;
pub use database::Database;
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
use std::sync::Mutex;
struct AppState {
db: Mutex<Database>
}
#[get("/comments")]
async fn get_comments(data: web::Data<AppState>) -> impl Responder {
let db = &data.db.lock().unwrap();
HttpResponse::Ok().json(&db.get_comments().unwrap())
}
#[actix_web::main]
async fn main() -> Result<(), std::io::Error> {
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) });
HttpServer::new(move || {
App::new()
.service(get_comments)
.app_data(state.clone())
})
.bind(("127.0.0.1", 8080))?
.run()
.await
2 years ago
}