Endpoint to serve sentences as JSON

This commit is contained in:
Brandon Thomas committed 2020-05-31 19:39:03 -04:00
1 parent 3c1bc0b1a8
commit 2cc5ea67ba
11 files changed
+71 -14

No files matched your search

Generated
+2
View File
@@ -536,6 +536,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -737,6 +738,7 @@ version = "1.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_derives 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mysqlclient-sys 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+2 -2
View File
@@ -19,8 +19,8 @@ actix-rt = "1.0.0"
actix-utils = "1.0.3"
actix-web = "2.0.0"
anyhow = "1.0.30"
chrono = "0.4.11"
diesel = { version = "1.4.4", features = ["mysql", "r2d2"] }
chrono = { version = "0.4.11", features = ["serde"] }
diesel = { version = "1.4.4", features = ["mysql", "r2d2", "chrono"] }
dotenv = "0.15.0" # for diesel
env_logger = "0.7.1"
hound = "3.4.0" # wav codec
@@ -1 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE sentences;
@@ -3,8 +3,8 @@
CREATE TABLE sentences (
id INT PRIMARY KEY AUTO_INCREMENT,
sentence varchar(1024) NOT NULL,
speaker varchar(15) DEFAULT NULL,
ip_address varchar(30) DEFAULT NULL,
speaker varchar(15) NOT NULL,
ip_address varchar(30) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
fulltext(sentence),
key(ip_address)
-1
View File
@@ -1,6 +1,5 @@
use anyhow::Result as AnyhowResult;
use diesel::mysql::MysqlConnection;
use diesel::prelude::*;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use diesel::r2d2::PooledConnection;
+17 -5
View File
@@ -1,10 +1,11 @@
use diesel::prelude::*;
use crate::schema::sentences;
use anyhow::Result as AnyhowResult;
use crate::database::connector::DatabaseConnector;
use chrono::NaiveDateTime;
use chrono::{NaiveDateTime, DateTime, Utc};
use crate::database::connector::{DatabaseConnector, MysqlPooledConnection};
use crate::schema::sentences;
//use crate::schema::sentences::dsl::*;
use diesel::prelude::*;
#[derive(Queryable)]
#[derive(Queryable, Serialize, Debug)]
pub struct Sentence {
pub id: i32,
pub sentence: String,
@@ -31,3 +32,14 @@ impl NewSentence {
Ok(())
}
}
impl Sentence {
pub fn load(db_connector: &DatabaseConnector, limit: i64) -> AnyhowResult<Vec<Sentence>> {
let pooled_connection : MysqlPooledConnection = db_connector.get_pooled_connection()?;
let results = sentences::table
.load::<Sentence>(&pooled_connection)?;
Ok(results)
}
}
+1 -1
View File
@@ -58,7 +58,7 @@ pub async fn legacy_get_speak(request: HttpRequest,
info!("Speaker: {}, Text: {}", speaker, text);
let mut app_state = app_state.into_inner();
let app_state = app_state.into_inner();
// NB: There is also `request.connection_info().remote()`, which contains
// proxy info via X-Forwarded-For, etc.
+1
View File
@@ -3,6 +3,7 @@ pub mod legacy_speak;
pub mod liveness;
pub mod models;
pub mod readiness;
pub mod sentences;
pub mod speak;
pub mod speakers;
pub mod tts;
+40
View File
@@ -0,0 +1,40 @@
use actix_web::web::{
Data,
Json,
};
use actix_web::http::StatusCode;
use actix_web::{HttpRequest, get, HttpResponse, Responder, Either};
use std::sync::Arc;
use crate::AppState;
use crate::config::{ModelConfigs, Speaker};
use crate::database::model::Sentence;
use anyhow::Error;
#[derive(Serialize, Debug)]
pub struct SentencesResult {
sentences: Vec<Sentence>,
}
#[get("/sentences")]
pub async fn get_sentences(
_request: HttpRequest,
app_state: Data<Arc<AppState>>
) -> Either<Json<SentencesResult>, std::io::Result<HttpResponse>>{
let app_state = app_state.into_inner();
let limit = 100;
let sentences = match Sentence::load(&app_state.database_connector, limit) {
Err(e) => {
error!("Couldn't query database for sentences: {:?}", e);
return Either::B(Ok(HttpResponse::build(StatusCode::INTERNAL_SERVER_ERROR)
.content_type("text/plain")
.body("Couldn't query database database")));
},
Ok(results) => results,
};
Either::A(Json(SentencesResult {
sentences: sentences,
}))
}
+2
View File
@@ -46,6 +46,7 @@ use crate::model::model_cache::ModelCache;
use crate::model::old_model::TacoMelModel;
use crate::text::text_to_arpabet_encoding;
use crate::database::connector::DatabaseConnector;
use crate::endpoints::sentences::get_sentences;
const BIND_ADDRESS : &'static str = "BIND_ADDRESS";
const ASSET_DIRECTORY : &'static str = "ASSET_DIRECTORY";
@@ -208,6 +209,7 @@ async fn run_server(app_state: AppState, server_args: ServerArgs) -> std::io::Re
.service(get_liveness)
.service(get_models)
.service(get_speakers)
.service(get_sentences)
.app_data(arc.clone())
)
.bind(bind_address)?
+2 -2
View File
@@ -2,8 +2,8 @@ table! {
sentences (id) {
id -> Integer,
sentence -> Varchar,
speaker -> Nullable<Varchar>,
ip_address -> Nullable<Varchar>,
speaker -> Varchar,
ip_address -> Varchar,
created_at -> Datetime,
}
}