Compare commits
3 Commits
c1a5425127
...
731a7aff4c
Author | SHA1 | Date | |
---|---|---|---|
731a7aff4c | |||
c2eec55e54 | |||
f086982037 |
4
rust-backend/.gitignore
vendored
Normal file
4
rust-backend/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
target
|
||||
database.sqlite3
|
||||
|
||||
|
1498
rust-backend/Cargo.lock
generated
Normal file
1498
rust-backend/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
rust-backend/Cargo.toml
Normal file
10
rust-backend/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "skantravels"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
refinery = { version = "0.8.14", features = ["rusqlite"] }
|
||||
rusqlite = "0.31"
|
||||
|
7
rust-backend/migrations/V1__create_favorites_table.sql
Normal file
7
rust-backend/migrations/V1__create_favorites_table.sql
Normal file
@ -0,0 +1,7 @@
|
||||
CREATE TABLE favorites (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id TEXT NOT NULL,
|
||||
lat REAL NOT NULL,
|
||||
lng REAL NOT NULL
|
||||
);
|
||||
|
57
rust-backend/src/main.rs
Normal file
57
rust-backend/src/main.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use actix_web::{get, Responder, HttpResponse, HttpServer, App, web};
|
||||
use std::sync::{Mutex, Arc};
|
||||
|
||||
mod embedded {
|
||||
use refinery::embed_migrations;
|
||||
embed_migrations!("./migrations");
|
||||
}
|
||||
|
||||
struct AppData {
|
||||
database: Arc<Mutex<rusqlite::Connection>>,
|
||||
}
|
||||
|
||||
#[get("/hc")]
|
||||
async fn healthcheck(data: web::Data<AppData>) -> impl Responder {
|
||||
let db = data.database.lock().unwrap();
|
||||
|
||||
match db.pragma_query(None, "integrity_check", |_| Ok(())) {
|
||||
Ok(_) => HttpResponse::Ok().body("OK"),
|
||||
Err(_) => HttpResponse::InternalServerError().body("Error"),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let port = std::env::var("RUST_BACKEND_PORT")
|
||||
.ok()
|
||||
.and_then(|port| port.parse::<u16>().ok())
|
||||
.unwrap_or(8080);
|
||||
|
||||
let database_path = std::env::var("RUST_BACKEND_DB")
|
||||
.unwrap_or("database.sqlite3".to_string());
|
||||
|
||||
println!("Opening database: {}", database_path);
|
||||
|
||||
let mut conn = rusqlite::Connection::open(database_path.clone()).unwrap();
|
||||
|
||||
embedded::migrations::runner().run(&mut conn).unwrap();
|
||||
|
||||
println!("Starting web server at port {}", port);
|
||||
|
||||
HttpServer::new(|| {
|
||||
let database_path = std::env::var("RUST_BACKEND_DB")
|
||||
.unwrap_or("database.sqlite3".to_string());
|
||||
|
||||
let conn = Arc::new(Mutex::new(rusqlite::Connection::open(database_path).unwrap()));
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(AppData {
|
||||
database: conn,
|
||||
}))
|
||||
.service(healthcheck)
|
||||
})
|
||||
.bind(("0.0.0.0", port))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Loading…
Reference in New Issue
Block a user