Added backend
This commit is contained in:
parent
0be02352a0
commit
513f437ee6
3429 changed files with 39630 additions and 0 deletions
24
backend/src/api.rs
Normal file
24
backend/src/api.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use actix_web::{HttpResponse, Responder, get, web::Data};
|
||||
use serde::Serialize;
|
||||
use sqlx::FromRow;
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
#[derive(Serialize, FromRow)]
|
||||
struct Sample {
|
||||
sampleid: i32,
|
||||
samplename: String,
|
||||
}
|
||||
|
||||
#[get("/sample")]
|
||||
pub async fn sample(app_state: Data<AppState>) -> impl Responder {
|
||||
let query =
|
||||
sqlx::query_as::<_, Sample>("SELECT * FROM sample")
|
||||
.fetch_all(&app_state.pool)
|
||||
.await;
|
||||
|
||||
match query {
|
||||
Ok(result) => HttpResponse::Ok().json(result),
|
||||
Err(_) => HttpResponse::BadRequest().into(),
|
||||
}
|
||||
}
|
||||
33
backend/src/main.rs
Normal file
33
backend/src/main.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use actix_web::{App, HttpServer, web::Data};
|
||||
use dotenvy::dotenv;
|
||||
|
||||
use sqlx::mysql::{MySqlPool, MySqlPoolOptions};
|
||||
use std::env;
|
||||
|
||||
mod api;
|
||||
use api::{sample};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppState {
|
||||
pool: MySqlPool,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
let db_connection = env::var("DATABASE_URL").unwrap();
|
||||
let pool: MySqlPool = MySqlPoolOptions::new()
|
||||
.connect(db_connection.as_str())
|
||||
.await
|
||||
.unwrap();
|
||||
let app_state = AppState { pool };
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(Data::new(app_state.pool.clone()))
|
||||
.service(sample)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue