Refactor project into workspaces

This commit is contained in:
Tobias Reisinger 2023-11-22 20:06:20 +01:00
parent 131bdeec78
commit bacea1e3e9
31 changed files with 119 additions and 99 deletions

View file

@ -1,70 +0,0 @@
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use sqlx::Error;
#[derive(Debug)]
pub enum DatabaseError {
DeleteError,
InsertError,
InsertGetError,
NotFound,
Protected,
UpdateError,
Unknown,
}
impl DatabaseError {
pub fn get_code(&self) -> StatusCode {
match self {
DatabaseError::NotFound => StatusCode::NOT_FOUND,
DatabaseError::Protected => StatusCode::FORBIDDEN,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
impl Serialize for DatabaseError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("error", 3)?;
s.serialize_field("type", "database-error")?;
s.serialize_field("code", &self.get_code().as_u16())?;
s.serialize_field("description", &String::from(self))?;
s.end()
}
}
impl From<&DatabaseError> for String {
fn from(err: &DatabaseError) -> Self {
match err {
DatabaseError::InsertError => String::from("error on inserting into database"),
DatabaseError::InsertGetError => {
String::from("error on retrieving new entry from database (your entry was saved)")
}
DatabaseError::NotFound => String::from("model was not found in database"),
DatabaseError::DeleteError => String::from("error on deleting from database"),
DatabaseError::Protected => String::from("model is protected"),
DatabaseError::UpdateError => String::from("error on updating the model"),
DatabaseError::Unknown => String::from("unknown error"),
}
}
}
impl From<DatabaseError> for HttpResponse {
fn from(err: DatabaseError) -> Self {
HttpResponse::build(err.get_code()).json(err)
}
}
impl From<Error> for DatabaseError {
fn from(value: Error) -> Self {
match value {
Error::RowNotFound => DatabaseError::NotFound,
_ => DatabaseError::Unknown,
}
}
}

View file

@ -1,78 +0,0 @@
use log::{info, trace};
use sqlx::migrate::Migrator;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::{Pool, Sqlite};
use crate::db::errors::DatabaseError;
use crate::db::model_utils::Period;
use crate::db::schedules::{Periods, Schedule};
use crate::types::EmgauwaUid;
pub mod errors;
pub mod models;
pub mod schedules;
pub mod tag;
mod model_utils;
static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations"
pub async fn run_migrations(pool: &Pool<Sqlite>) {
info!("Running migrations");
MIGRATOR.run(pool).await.expect("Failed to run migrations.");
}
async fn init_schedule(
pool: &Pool<Sqlite>,
uid: &EmgauwaUid,
name: &str,
periods: Periods,
) -> Result<(), DatabaseError> {
trace!("Initializing schedule {:?}", name);
match Schedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await {
Ok(_) => Ok(()),
Err(err) => match err {
DatabaseError::NotFound => {
trace!("Schedule {:?} not found, inserting", name);
sqlx::query_as!(
Schedule,
"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
uid,
name,
periods,
)
.fetch_optional(pool)
.await?
.ok_or(DatabaseError::InsertGetError)
.map(|_| ())
}
_ => Err(err),
},
}
}
pub async fn init(db: &str) -> Pool<Sqlite> {
let pool: Pool<Sqlite> = SqlitePoolOptions::new()
.acquire_timeout(std::time::Duration::from_secs(1))
.max_connections(5)
.connect(db)
.await
.expect("Error connecting to database.");
run_migrations(&pool).await;
init_schedule(&pool, &EmgauwaUid::Off, "Off", Periods(vec![]))
.await
.expect("Error initializing schedule Off");
init_schedule(
&pool,
&EmgauwaUid::On,
"On",
Periods(vec![Period::new_on()]),
)
.await
.expect("Error initializing schedule On");
pool
}

View file

@ -1,111 +0,0 @@
use crate::db::schedules::Periods;
use chrono::{NaiveTime, Timelike};
use serde::{Deserialize, Serialize};
use sqlx::database::HasArguments;
use sqlx::encode::IsNull;
use sqlx::error::BoxDynError;
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
use sqlx::{Decode, Encode, Sqlite, Type};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Period {
#[serde(with = "period_format")]
pub start: NaiveTime,
#[serde(with = "period_format")]
pub end: NaiveTime,
}
mod period_format {
use chrono::NaiveTime;
use serde::{self, Deserialize, Deserializer, Serializer};
const FORMAT: &str = "%H:%M";
pub fn serialize<S>(time: &NaiveTime, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = format!("{}", time.format(FORMAT));
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveTime, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
NaiveTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
}
}
impl Period {
pub fn new(start: NaiveTime, end: NaiveTime) -> Self {
Period { start, end }
}
pub fn new_on() -> Self {
Period {
start: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
end: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
}
}
}
impl Type<Sqlite> for Periods {
fn type_info() -> SqliteTypeInfo {
<&[u8] as Type<Sqlite>>::type_info()
}
fn compatible(ty: &SqliteTypeInfo) -> bool {
<&[u8] as Type<Sqlite>>::compatible(ty)
}
}
impl<'q> Encode<'q, Sqlite> for Periods {
//noinspection DuplicatedCode
fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
<&Vec<u8> as Encode<Sqlite>>::encode(&Vec::from(self), buf)
}
}
impl<'r> Decode<'r, Sqlite> for Periods {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
let blob = <&[u8] as Decode<Sqlite>>::decode(value)?;
Ok(Periods::from(Vec::from(blob)))
}
}
impl From<&Periods> for Vec<u8> {
fn from(periods: &Periods) -> Vec<u8> {
periods
.0
.iter()
.flat_map(|period| {
let vec = vec![
period.start.hour() as u8,
period.start.minute() as u8,
period.end.hour() as u8,
period.end.minute() as u8,
];
vec
})
.collect()
}
}
impl From<Vec<u8>> for Periods {
fn from(value: Vec<u8>) -> Self {
let mut vec = Vec::new();
for i in (3..value.len()).step_by(4) {
let start_val_h: u32 = value[i - 3] as u32;
let start_val_m: u32 = value[i - 2] as u32;
let end_val_h: u32 = value[i - 1] as u32;
let end_val_m: u32 = value[i] as u32;
vec.push(Period {
start: NaiveTime::from_hms_opt(start_val_h, start_val_m, 0).unwrap(),
end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0).unwrap(),
});
}
Periods(vec)
}
}

View file

@ -1,8 +0,0 @@
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct Relay {
#[serde(skip)]
pub id: i64,
// TODO
}

View file

@ -1,152 +0,0 @@
use serde_derive::{Deserialize, Serialize};
use std::borrow::Borrow;
use std::ops::DerefMut;
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use crate::db::errors::DatabaseError;
use crate::db::model_utils::Period;
use crate::db::tag::Tag;
use crate::types::EmgauwaUid;
#[derive(Debug, Serialize, Clone)]
pub struct Schedule {
#[serde(skip)]
pub id: i64,
#[serde(rename(serialize = "id"))]
pub uid: EmgauwaUid,
pub name: String,
pub periods: Periods,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Periods(pub Vec<Period>);
impl Schedule {
pub async fn get_all(
conn: &mut PoolConnection<Sqlite>,
) -> Result<Vec<Schedule>, DatabaseError> {
Ok(sqlx::query_as!(Schedule, "SELECT * FROM schedules")
.fetch_all(conn.deref_mut())
.await?)
}
pub async fn get_by_uid(
conn: &mut PoolConnection<Sqlite>,
filter_uid: &EmgauwaUid,
) -> Result<Schedule, DatabaseError> {
sqlx::query_as!(
Schedule,
"SELECT * FROM schedules WHERE uid = ?",
filter_uid
)
.fetch_optional(conn.deref_mut())
.await
.map(|s| s.ok_or(DatabaseError::NotFound))?
}
pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>,
tag: &Tag,
) -> Result<Vec<Schedule>, DatabaseError> {
Ok(sqlx::query_as!(Schedule, "SELECT schedule.* FROM schedules AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
.fetch_all(conn.deref_mut())
.await?)
}
pub async fn delete_by_uid(
conn: &mut PoolConnection<Sqlite>,
filter_uid: EmgauwaUid,
) -> Result<(), DatabaseError> {
let filter_uid = match filter_uid {
EmgauwaUid::Off => Err(DatabaseError::Protected),
EmgauwaUid::On => Err(DatabaseError::Protected),
EmgauwaUid::Any(_) => Ok(filter_uid),
}?;
sqlx::query!("DELETE FROM schedules WHERE uid = ?", filter_uid)
.execute(conn.deref_mut())
.await
.map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError),
_ => Ok(()),
})?
}
pub async fn create(
conn: &mut PoolConnection<Sqlite>,
new_name: &str,
new_periods: &Periods,
) -> Result<Schedule, DatabaseError> {
let uid = EmgauwaUid::default();
sqlx::query_as!(
Schedule,
"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
uid,
new_name,
new_periods,
)
.fetch_optional(conn.deref_mut())
.await?
.ok_or(DatabaseError::InsertGetError)
}
pub async fn update(
&self,
conn: &mut PoolConnection<Sqlite>,
new_name: &str,
new_periods: &Periods,
) -> Result<Schedule, DatabaseError> {
// overwrite periods on protected schedules
let new_periods = match self.uid {
EmgauwaUid::Off | EmgauwaUid::On => self.periods.borrow(),
EmgauwaUid::Any(_) => new_periods,
};
sqlx::query!(
"UPDATE schedules SET name = ?, periods = ? WHERE id = ?",
new_name,
new_periods,
self.id,
)
.execute(conn.deref_mut())
.await?;
Schedule::get_by_uid(conn, &self.uid).await
}
pub async fn get_tags(
&self,
conn: &mut PoolConnection<Sqlite>,
) -> Result<Vec<String>, DatabaseError> {
Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.schedule_id = ?", self.id)
.fetch_all(conn.deref_mut())
.await?)
}
pub async fn set_tags(
&self,
conn: &mut PoolConnection<Sqlite>,
new_tags: &[String],
) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM junction_tag WHERE schedule_id = ?", self.id)
.execute(conn.deref_mut())
.await?;
for new_tag in new_tags {
let tag: Option<Tag> =
sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", new_tag)
.fetch_optional(conn.deref_mut())
.await?;
let tag = match tag {
Some(id) => id,
None => Tag::create(conn, new_tag).await?,
};
tag.link_schedule(conn, self).await?;
}
Ok(())
}
}

View file

@ -1,81 +0,0 @@
use serde_derive::Serialize;
use std::ops::DerefMut;
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use crate::db::errors::DatabaseError;
use crate::db::models::*;
use crate::db::schedules::Schedule;
#[derive(Debug, Serialize, Clone)]
pub struct Tag {
pub id: i64,
pub tag: String,
}
pub struct JunctionTag {
pub id: i64,
pub tag_id: i64,
pub relay_id: Option<i64>,
pub schedule_id: Option<i64>,
}
impl Tag {
pub async fn create(
conn: &mut PoolConnection<Sqlite>,
new_tag: &str,
) -> Result<Tag, DatabaseError> {
sqlx::query_as!(
Tag,
"INSERT INTO tags (tag) VALUES (?) RETURNING *",
new_tag
)
.fetch_optional(conn.deref_mut())
.await?
.ok_or(DatabaseError::InsertGetError)
}
pub async fn get(
conn: &mut PoolConnection<Sqlite>,
target_tag: &str,
) -> Result<Tag, DatabaseError> {
sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag)
.fetch_optional(conn.deref_mut())
.await
.map(|t| t.ok_or(DatabaseError::NotFound))?
}
#[allow(dead_code)]
pub async fn link_relay(
&self,
conn: &mut PoolConnection<Sqlite>,
target_relay: &Relay,
) -> Result<JunctionTag, DatabaseError> {
sqlx::query_as!(
JunctionTag,
"INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *",
self.id,
target_relay.id
)
.fetch_optional(conn.deref_mut())
.await?
.ok_or(DatabaseError::InsertGetError)
}
pub async fn link_schedule(
&self,
conn: &mut PoolConnection<Sqlite>,
target_schedule: &Schedule,
) -> Result<JunctionTag, DatabaseError> {
sqlx::query_as!(
JunctionTag,
"INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *",
self.id,
target_schedule.id
)
.fetch_optional(conn.deref_mut())
.await?
.ok_or(DatabaseError::InsertGetError)
}
}

View file

@ -1,82 +0,0 @@
use crate::db::errors::DatabaseError;
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum ApiError {
BadUid,
ProtectedSchedule,
DatabaseError(DatabaseError),
InternalError(String),
}
impl ApiError {
fn get_code(&self) -> StatusCode {
match self {
ApiError::BadUid => StatusCode::BAD_REQUEST,
ApiError::ProtectedSchedule => StatusCode::FORBIDDEN,
ApiError::DatabaseError(db_error) => db_error.get_code(),
ApiError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
impl Serialize for ApiError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("error", 2)?;
s.serialize_field("code", &self.get_code().as_u16())?;
s.serialize_field("description", &String::from(self))?;
s.end()
}
}
impl From<&ApiError> for String {
fn from(err: &ApiError) -> Self {
match err {
ApiError::BadUid => String::from("the uid is in a bad format"),
ApiError::ProtectedSchedule => String::from("the targeted schedule is protected"),
ApiError::DatabaseError(db_err) => String::from(db_err),
ApiError::InternalError(msg) => msg.clone(),
}
}
}
impl From<&ApiError> for HttpResponse {
fn from(err: &ApiError) -> Self {
HttpResponse::build(err.get_code()).json(err)
}
}
impl Display for ApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.get_code(), String::from(self))
}
}
impl actix_web::error::ResponseError for ApiError {
fn status_code(&self) -> StatusCode {
self.get_code()
}
fn error_response(&self) -> HttpResponse {
HttpResponse::from(self)
}
}
impl From<sqlx::Error> for ApiError {
fn from(err: sqlx::Error) -> Self {
ApiError::DatabaseError(DatabaseError::from(err))
}
}
impl From<DatabaseError> for ApiError {
fn from(err: DatabaseError) -> Self {
ApiError::DatabaseError(err)
}
}

View file

@ -1,38 +0,0 @@
use actix_web::{error, Error, HttpRequest, HttpResponse};
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
pub(crate) mod errors;
pub mod v1;
enum EmgauwaJsonPayLoadError {
Error(error::JsonPayloadError),
}
impl Serialize for EmgauwaJsonPayLoadError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("error", 3)?;
s.serialize_field("type", "json-payload-error")?;
s.serialize_field("code", &400)?;
s.serialize_field(
"description",
&match self {
EmgauwaJsonPayLoadError::Error(err) => format!("{}", err),
},
)?;
s.end()
}
}
pub fn json_error_handler(err: error::JsonPayloadError, _: &HttpRequest) -> Error {
error::InternalError::from_response(
"",
HttpResponse::BadRequest()
.content_type("application/json")
.json(EmgauwaJsonPayLoadError::Error(err)),
)
.into()
}

View file

@ -1,2 +0,0 @@
pub mod schedules;
pub mod ws;

View file

@ -1,189 +0,0 @@
use std::borrow::Borrow;
use std::convert::TryFrom;
use actix_web::{delete, get, post, put, web, HttpResponse};
use serde::{Deserialize, Serialize};
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use crate::db::errors::DatabaseError;
use crate::db::schedules::*;
use crate::db::tag::Tag;
use crate::handlers::errors::ApiError;
use crate::return_models::ReturnSchedule;
use crate::types::EmgauwaUid;
use crate::utils::vec_has_error;
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestSchedule {
name: String,
periods: Periods,
tags: Vec<String>,
}
#[get("/api/v1/schedules")]
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let schedules = Schedule::get_all(&mut pool_conn).await?;
let mut return_schedules: Vec<ReturnSchedule> =
schedules.iter().map(ReturnSchedule::from).collect();
for schedule in return_schedules.iter_mut() {
schedule.load_tags(&mut pool_conn);
}
Ok(HttpResponse::Ok().json(return_schedules))
}
#[get("/api/v1/schedules/tag/{tag}")]
pub async fn tagged(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (tag,) = path.into_inner();
let tag_db = Tag::get(&mut pool_conn, &tag).await?;
let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
let mut return_schedules: Vec<ReturnSchedule> =
schedules.iter().map(ReturnSchedule::from).collect();
for schedule in return_schedules.iter_mut() {
schedule.load_tags(&mut pool_conn);
}
Ok(HttpResponse::Ok().json(return_schedules))
}
#[get("/api/v1/schedules/{schedule_id}")]
pub async fn show(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let mut return_schedule = ReturnSchedule::from(schedule);
return_schedule.load_tags(&mut pool_conn);
Ok(HttpResponse::Ok().json(return_schedule))
}
#[post("/api/v1/schedules")]
pub async fn add(
pool: web::Data<Pool<Sqlite>>,
data: web::Json<RequestSchedule>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let new_schedule = Schedule::create(&mut pool_conn, &data.name, &data.periods).await?;
new_schedule
.set_tags(&mut pool_conn, data.tags.as_slice())
.await?;
let mut return_schedule = ReturnSchedule::from(new_schedule);
return_schedule.load_tags(&mut pool_conn);
Ok(HttpResponse::Created().json(return_schedule))
}
async fn add_list_single(
conn: &mut PoolConnection<Sqlite>,
request_schedule: &RequestSchedule,
) -> Result<Schedule, DatabaseError> {
let new_schedule =
Schedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
new_schedule
.set_tags(conn, request_schedule.tags.as_slice())
.await?;
Ok(new_schedule)
}
#[post("/api/v1/schedules/list")]
pub async fn add_list(
pool: web::Data<Pool<Sqlite>>,
data: web::Json<Vec<RequestSchedule>>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let result: Vec<Result<Schedule, DatabaseError>> = data
.as_slice()
.iter()
.map(|request_schedule| {
futures::executor::block_on(add_list_single(&mut pool_conn, request_schedule))
})
.collect();
match vec_has_error(&result) {
true => Ok(HttpResponse::from(
result
.into_iter()
.find(|r| r.is_err())
.unwrap()
.unwrap_err(),
)),
false => {
let mut return_schedules: Vec<ReturnSchedule> = result
.iter()
.map(|s| ReturnSchedule::from(s.as_ref().unwrap()))
.collect();
for schedule in return_schedules.iter_mut() {
schedule.load_tags(&mut pool_conn);
}
Ok(HttpResponse::Created().json(return_schedules))
}
}
}
#[put("/api/v1/schedules/{schedule_id}")]
pub async fn update(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
data: web::Json<RequestSchedule>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = schedule
.update(&mut pool_conn, data.name.as_str(), data.periods.borrow())
.await?;
schedule
.set_tags(&mut pool_conn, data.tags.as_slice())
.await?;
let mut return_schedule = ReturnSchedule::from(schedule);
return_schedule.load_tags(&mut pool_conn);
Ok(HttpResponse::Ok().json(return_schedule))
}
#[delete("/api/v1/schedules/{schedule_id}")]
pub async fn delete(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (schedule_uid,) = path.into_inner();
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
match emgauwa_uid {
EmgauwaUid::Off => Err(ApiError::ProtectedSchedule),
EmgauwaUid::On => Err(ApiError::ProtectedSchedule),
EmgauwaUid::Any(_) => {
Schedule::delete_by_uid(&mut pool_conn, emgauwa_uid).await?;
Ok(HttpResponse::Ok().json("schedule got deleted"))
}
}
}

View file

@ -1,53 +0,0 @@
use crate::db::schedules::Schedule;
use crate::handlers::errors::ApiError;
use actix::{Actor, StreamHandler};
use actix_web::{get, web, HttpRequest, HttpResponse};
use actix_web_actors::ws;
use actix_web_actors::ws::ProtocolError;
use sqlx::{Pool, Sqlite};
use ws::Message;
struct ControllerWs {
pub pool: Pool<Sqlite>,
}
impl Actor for ControllerWs {
type Context = ws::WebsocketContext<Self>;
}
async fn get_schedules(pool: &mut Pool<Sqlite>) -> Result<Vec<Schedule>, ApiError> {
let mut pool_conn = pool.acquire().await?;
Ok(Schedule::get_all(&mut pool_conn).await?)
}
/// Handler for ws::Message message
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
let schedules = futures::executor::block_on(get_schedules(&mut self.pool)).unwrap();
let schedules_json = serde_json::to_string(&schedules).unwrap();
match msg {
Ok(Message::Ping(msg)) => ctx.pong(&msg),
Ok(Message::Text(_text)) => ctx.text(schedules_json),
_ => {}
}
}
}
#[get("/api/v1/ws/controllers")]
pub async fn index(
pool: web::Data<Pool<Sqlite>>,
req: HttpRequest,
stream: web::Payload,
) -> Result<HttpResponse, ApiError> {
let resp = ws::start(
ControllerWs {
pool: pool.get_ref().clone(),
},
&req,
stream,
)
.map_err(|_| ApiError::InternalError(String::from("error starting websocket")));
println!("{:?}", resp);
resp
}

View file

@ -1 +0,0 @@
pub mod controllers;

View file

@ -1,55 +0,0 @@
use std::str::FromStr;
use actix_web::middleware::TrailingSlash;
use actix_web::{middleware, web, App, HttpServer};
use log::{trace, LevelFilter};
use simple_logger::SimpleLogger;
mod db;
mod handlers;
mod return_models;
mod settings;
mod types;
mod utils;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
settings::init();
let settings = settings::get();
let log_level: LevelFilter = log::LevelFilter::from_str(&settings.logging.level)
.unwrap_or_else(|_| panic!("Error parsing log level."));
trace!("Log level set to {:?}", log_level);
SimpleLogger::new()
.with_level(log_level)
.init()
.unwrap_or_else(|_| panic!("Error initializing logger."));
let pool = db::init(&settings.database).await;
HttpServer::new(move || {
App::new()
.wrap(
middleware::DefaultHeaders::new()
.add(("Access-Control-Allow-Origin", "*"))
.add(("Access-Control-Allow-Headers", "*"))
.add(("Access-Control-Allow-Methods", "*")),
)
.wrap(middleware::Logger::default())
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
.app_data(web::Data::new(pool.clone()))
.service(handlers::v1::schedules::index)
.service(handlers::v1::schedules::tagged)
.service(handlers::v1::schedules::show)
.service(handlers::v1::schedules::add)
.service(handlers::v1::schedules::add_list)
.service(handlers::v1::schedules::update)
.service(handlers::v1::schedules::delete)
.service(handlers::v1::ws::controllers::index)
})
.bind(format!("{}:{}", settings.host, settings.port))?
.run()
.await
}

View file

@ -1,33 +0,0 @@
use crate::db::schedules::Schedule;
use futures::executor;
use serde::Serialize;
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
#[derive(Debug, Serialize)]
pub struct ReturnSchedule {
#[serde(flatten)]
pub schedule: Schedule,
pub tags: Vec<String>,
}
impl ReturnSchedule {
pub fn load_tags(&mut self, conn: &mut PoolConnection<Sqlite>) {
self.tags = executor::block_on(self.schedule.get_tags(conn)).unwrap();
}
}
impl From<Schedule> for ReturnSchedule {
fn from(schedule: Schedule) -> Self {
ReturnSchedule {
schedule,
tags: vec![],
}
}
}
impl From<&Schedule> for ReturnSchedule {
fn from(schedule: &Schedule) -> Self {
ReturnSchedule::from(schedule.clone())
}
}

View file

@ -1,67 +0,0 @@
use std::sync::RwLock;
use config::Config;
use lazy_static::lazy_static;
use serde_derive::Deserialize;
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
#[allow(unused)]
pub struct Logging {
pub level: String,
pub file: String,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
#[allow(unused)]
pub struct Settings {
pub database: String,
pub port: u16,
pub host: String,
pub logging: Logging,
}
impl Default for Settings {
fn default() -> Self {
Settings {
database: String::from("sqlite://emgauwa-core.sqlite"),
port: 5000,
host: String::from("127.0.0.1"),
logging: Logging::default(),
}
}
}
impl Default for Logging {
fn default() -> Self {
Logging {
level: String::from("info"),
file: String::from("stdout"),
}
}
}
lazy_static! {
static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default());
}
pub fn init() {
let settings = Config::builder()
.add_source(config::File::with_name("emgauwa-core"))
.add_source(
config::Environment::with_prefix("EMGAUWA")
.prefix_separator("_")
.separator("__"),
)
.build()
.unwrap()
.try_deserialize::<Settings>()
.unwrap_or_else(|_| panic!("Error reading settings."));
*SETTINGS.write().unwrap() = settings;
}
pub fn get() -> Settings {
SETTINGS.read().unwrap().clone()
}

View file

@ -1,10 +0,0 @@
use uuid::Uuid;
pub mod emgauwa_uid;
#[derive(PartialEq, Clone)]
pub enum EmgauwaUid {
Off,
On,
Any(Uuid),
}

View file

@ -1,141 +0,0 @@
use std::convert::TryFrom;
use std::fmt::{Debug, Formatter};
use std::str::FromStr;
use serde::{Serialize, Serializer};
use sqlx::database::HasArguments;
use sqlx::encode::IsNull;
use sqlx::error::BoxDynError;
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
use sqlx::{Decode, Encode, Sqlite, Type};
use uuid::Uuid;
use crate::types::EmgauwaUid;
impl EmgauwaUid {
const OFF_STR: &'static str = "off";
const ON_STR: &'static str = "on";
const OFF_U8: u8 = 0;
const ON_U8: u8 = 1;
const OFF_U128: u128 = 0;
const ON_U128: u128 = 1;
}
impl Default for EmgauwaUid {
fn default() -> Self {
EmgauwaUid::Any(Uuid::new_v4())
}
}
impl Debug for EmgauwaUid {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
EmgauwaUid::Off => EmgauwaUid::OFF_STR.fmt(f),
EmgauwaUid::On => EmgauwaUid::ON_STR.fmt(f),
EmgauwaUid::Any(value) => value.fmt(f),
}
}
}
impl Type<Sqlite> for EmgauwaUid {
fn type_info() -> SqliteTypeInfo {
<&[u8] as Type<Sqlite>>::type_info()
}
fn compatible(ty: &SqliteTypeInfo) -> bool {
<&[u8] as Type<Sqlite>>::compatible(ty)
}
}
impl<'q> Encode<'q, Sqlite> for EmgauwaUid {
//noinspection DuplicatedCode
fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
<&Vec<u8> as Encode<Sqlite>>::encode(&Vec::from(self), buf)
}
}
impl<'r> Decode<'r, Sqlite> for EmgauwaUid {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
Ok(EmgauwaUid::from(<&[u8] as Decode<Sqlite>>::decode(value)?))
}
}
impl Serialize for EmgauwaUid {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
String::from(self).serialize(serializer)
}
}
impl From<Uuid> for EmgauwaUid {
fn from(uid: Uuid) -> EmgauwaUid {
match uid.as_u128() {
EmgauwaUid::OFF_U128 => EmgauwaUid::Off,
EmgauwaUid::ON_U128 => EmgauwaUid::On,
_ => EmgauwaUid::Any(uid),
}
}
}
impl TryFrom<&str> for EmgauwaUid {
type Error = uuid::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
EmgauwaUid::OFF_STR => Ok(EmgauwaUid::Off),
EmgauwaUid::ON_STR => Ok(EmgauwaUid::On),
any => match Uuid::from_str(any) {
Ok(uuid) => Ok(EmgauwaUid::Any(uuid)),
Err(err) => Err(err),
},
}
}
}
impl From<&EmgauwaUid> for Uuid {
fn from(emgauwa_uid: &EmgauwaUid) -> Uuid {
match emgauwa_uid {
EmgauwaUid::Off => Uuid::from_u128(EmgauwaUid::OFF_U128),
EmgauwaUid::On => Uuid::from_u128(EmgauwaUid::ON_U128),
EmgauwaUid::Any(value) => *value,
}
}
}
impl From<&EmgauwaUid> for String {
fn from(emgauwa_uid: &EmgauwaUid) -> String {
match emgauwa_uid {
EmgauwaUid::Off => String::from(EmgauwaUid::OFF_STR),
EmgauwaUid::On => String::from(EmgauwaUid::ON_STR),
EmgauwaUid::Any(value) => value.as_hyphenated().to_string(),
}
}
}
impl From<&EmgauwaUid> for Vec<u8> {
fn from(emgauwa_uid: &EmgauwaUid) -> Vec<u8> {
match emgauwa_uid {
EmgauwaUid::Off => vec![EmgauwaUid::OFF_U8],
EmgauwaUid::On => vec![EmgauwaUid::ON_U8],
EmgauwaUid::Any(value) => value.as_bytes().to_vec(),
}
}
}
impl From<&[u8]> for EmgauwaUid {
fn from(value: &[u8]) -> Self {
match value {
[EmgauwaUid::OFF_U8] => EmgauwaUid::Off,
[EmgauwaUid::ON_U8] => EmgauwaUid::On,
value_bytes => EmgauwaUid::Any(Uuid::from_slice(value_bytes).unwrap()),
}
}
}
impl From<Vec<u8>> for EmgauwaUid {
fn from(value: Vec<u8>) -> Self {
EmgauwaUid::from(value.as_slice())
}
}

View file

@ -1,3 +0,0 @@
pub fn vec_has_error<T, E>(target: &[Result<T, E>]) -> bool {
target.iter().any(|t| t.is_err())
}