Browse Source

sync db

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
c958c0b3f8
  1. 3
      server/src/api.rs
  2. 21
      server/src/services/data_service.rs

3
server/src/api.rs

@ -70,11 +70,12 @@ impl API<'_> {
});
let metrics = metric::get_route(self.metric_service.clone());
let login = auth::get_route(self.user_service.clone());
let segments = segments::get_route(self.data_service.clone());
let public = warp::fs::dir("public");
println!("Start server on 8000 port");
// TODO: move it to "server"
let routes = login.or(metrics).or(options).or(public).or(not_found);
let routes = login.or(metrics).or(segments).or(options).or(public).or(not_found);
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;
}
}

21
server/src/services/data_service.rs

@ -2,6 +2,9 @@ use rusqlite::{ Connection, params };
use serde::Serialize;
use std::sync::{Arc, Mutex};
#[derive(Debug, Serialize)]
pub struct Segment {
pub id: Option<u64>,
@ -9,9 +12,12 @@ pub struct Segment {
pub end: u64,
}
// TODO: find a way to remove this unsafe
unsafe impl Sync for DataService {}
pub struct DataService {
connection: Connection
connection: Arc<Mutex<Connection>>
}
@ -27,14 +33,12 @@ impl DataService {
[],
)?;
Ok(DataService { connection: conn })
Ok(DataService { connection: Arc::new(Mutex::new(conn)) })
}
pub fn insert_segment(&self, segment: &Segment) -> anyhow::Result<u64> {
// TODO: merge with other segments
self.connection.execute(
self.connection.lock().unwrap().execute(
"INSERT INTO segment (start, end) VALUES (?1, ?2)",
params![segment.start, segment.end],
)?;
@ -42,7 +46,8 @@ impl DataService {
}
pub fn get_segments(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> {
let mut stmt = self.connection.prepare(
let conn = self.connection.lock().unwrap();
let mut stmt = conn.prepare(
"SELECT id, start, end FROM person WHERE ?1 < start AND end < ?2"
)?;
@ -53,10 +58,6 @@ impl DataService {
end: row.get(2)?
})
})?.map(|e| e.unwrap()).collect();
Ok(res)
}
}

Loading…
Cancel
Save