remove db stuff from backend
This commit is contained in:
parent
a2853c4a95
commit
7c089fe532
|
@ -142,12 +142,6 @@ version = "1.3.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
|
@ -200,21 +194,6 @@ dependencies = [
|
|||
"subtle 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "datalogger"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
"diesel",
|
||||
"dotenv",
|
||||
"libsqlite3-sys",
|
||||
"rocket",
|
||||
"rocket_contrib",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "devise"
|
||||
version = "0.2.0"
|
||||
|
@ -285,6 +264,19 @@ version = "0.15.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||
|
||||
[[package]]
|
||||
name = "draw-game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
"dotenv",
|
||||
"rocket",
|
||||
"rocket_contrib",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fake-simd"
|
||||
version = "0.1.2"
|
||||
|
@ -522,7 +514,6 @@ version = "0.18.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e704a02bcaecd4a08b93a23f6be59d0bd79cd161e0963e9499165a0a35df7bd"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "datalogger"
|
||||
name = "draw-game"
|
||||
version = "0.1.0"
|
||||
authors = ["Vivian Lim <vivlim@pm.me>"]
|
||||
edition = "2018"
|
||||
|
@ -11,8 +11,6 @@ rocket = "0.4.5"
|
|||
rocket_contrib = { version = "0.4.5", default-features = false, features = ["json", "diesel_sqlite_pool", "serve"] }
|
||||
anyhow = "1.0"
|
||||
chrono = "0.4"
|
||||
diesel = { version = "1.4.5", features = ["sqlite"] }
|
||||
dotenv = "0.15.0"
|
||||
libsqlite3-sys = { version = "0.18.0", features = ["bundled"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
|
@ -1,2 +0,0 @@
|
|||
-- This file should undo anything in `up.sql`
|
||||
drop table events
|
|
@ -1,7 +0,0 @@
|
|||
-- Your SQL goes here
|
||||
CREATE TABLE events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
devicename VARCHAR NOT NULL,
|
||||
value INTEGER NOT NULL,
|
||||
timestamp VARCHAR NOT NULL
|
||||
)
|
54
src/main.rs
54
src/main.rs
|
@ -14,9 +14,6 @@ extern crate serde;
|
|||
|
||||
extern crate chrono;
|
||||
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
use chrono::prelude::*;
|
||||
use rocket::{
|
||||
http::RawStr,
|
||||
|
@ -31,17 +28,10 @@ use std::{
|
|||
};
|
||||
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
extern crate dotenv;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use dotenv::dotenv;
|
||||
|
||||
use self::models::*;
|
||||
|
||||
#[database("sqlite_events")]
|
||||
struct DbConn(diesel::SqliteConnection);
|
||||
|
||||
/*
|
||||
#[get("/")]
|
||||
fn index() -> Result<String, anyhow::Error> {
|
||||
|
@ -52,58 +42,35 @@ fn index() -> Result<String, anyhow::Error> {
|
|||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
#[get("/log/<device_name>/<value>")]
|
||||
fn log_new(device_name: &RawStr, value: &RawStr, conn: DbConn) -> Result<String, anyhow::Error> {
|
||||
fn log_new(device_name: &RawStr, value: &RawStr) -> Result<String, anyhow::Error> {
|
||||
let new_event = NewEvent {
|
||||
devicename: device_name,
|
||||
value: value.parse()?,
|
||||
timestamp: &Local::now().to_rfc3339(),
|
||||
};
|
||||
|
||||
diesel::insert_into(self::schema::events::table)
|
||||
.values(&new_event)
|
||||
.execute(&*conn)?;
|
||||
|
||||
Ok(String::from("Logged"))
|
||||
}
|
||||
|
||||
#[get("/log/<device_name>")]
|
||||
fn log_get(device_name: &RawStr, conn: DbConn) -> Result<String, anyhow::Error> {
|
||||
use self::schema::events::dsl::*;
|
||||
let results = events
|
||||
.filter(devicename.eq(device_name.as_str()))
|
||||
.load::<Event>(&*conn)?;
|
||||
fn log_get(device_name: &RawStr) -> Result<String, anyhow::Error> {
|
||||
|
||||
// convert each result into debug-print lines
|
||||
let lines = results
|
||||
.into_iter()
|
||||
.map(|e| format!("{:?}", e))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
Ok(lines)
|
||||
Ok("what")
|
||||
}
|
||||
|
||||
#[get("/devices")]
|
||||
fn devices_get(conn: DbConn) -> Result<String, anyhow::Error> {
|
||||
use self::schema::events::dsl::*;
|
||||
let devices = events
|
||||
.select(devicename)
|
||||
.distinct()
|
||||
.load::<String>(&*conn)?;
|
||||
fn devices_get() -> Result<String, anyhow::Error> {
|
||||
|
||||
Ok(serde_json::to_string(&devices)?)
|
||||
Ok("get")
|
||||
}
|
||||
|
||||
#[get("/device/<name>")]
|
||||
fn device_get(name: &RawStr, conn: DbConn) -> Result<String, anyhow::Error> {
|
||||
use self::schema::events::dsl::*;
|
||||
let results = events
|
||||
.filter(devicename.eq(name.as_str()))
|
||||
.load::<Event>(&*conn)?;
|
||||
|
||||
Ok(serde_json::to_string(&results)?)
|
||||
fn device_get(name: &RawStr) -> Result<String, anyhow::Error> {
|
||||
Ok("get")
|
||||
}
|
||||
*/
|
||||
|
||||
#[catch(404)]
|
||||
fn not_found() -> Option<NamedFile> {
|
||||
|
@ -112,12 +79,11 @@ fn not_found() -> Option<NamedFile> {
|
|||
|
||||
fn main() {
|
||||
rocket::ignite()
|
||||
.attach(DbConn::fairing())
|
||||
.mount(
|
||||
"/",
|
||||
rocket_contrib::serve::StaticFiles::from("frontend/build"),
|
||||
)
|
||||
.mount("/api", routes![log_new, log_get, devices_get, device_get])
|
||||
//.mount("/api", routes![log_new, log_get, devices_get, device_get])
|
||||
.register(catchers![not_found])
|
||||
.launch();
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
use super::schema::events;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Queryable, Debug, Serialize)]
|
||||
pub struct Event {
|
||||
pub id: i32,
|
||||
pub devicename: String,
|
||||
pub value: i32,
|
||||
pub timestamp: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "events"]
|
||||
pub struct NewEvent<'a> {
|
||||
pub devicename: &'a str,
|
||||
pub value: i32,
|
||||
pub timestamp: &'a str,
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
table! {
|
||||
events (id) {
|
||||
id -> Integer,
|
||||
devicename -> Text,
|
||||
value -> Integer,
|
||||
timestamp -> Text,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue