|
@@ -1,4 +1,3 @@
|
|
|
-#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
#[macro_use]
|
|
|
extern crate lazy_static;
|
|
|
#[macro_use]
|
|
@@ -9,7 +8,6 @@ extern crate dotenv;
|
|
|
mod models;
|
|
|
mod random;
|
|
|
mod database;
|
|
|
-mod request;
|
|
|
|
|
|
use dotenv::dotenv;
|
|
|
use std::net::{IpAddr, Ipv4Addr};
|
|
@@ -26,7 +24,6 @@ use figment::Figment;
|
|
|
use models::{World, Fortune, Message};
|
|
|
use database::HelloWorld;
|
|
|
use random::random_number;
|
|
|
-use request::RequestId;
|
|
|
|
|
|
#[get("/plaintext")]
|
|
|
async fn plaintext() -> &'static str {
|
|
@@ -42,8 +39,8 @@ async fn json() -> Json<models::Message> {
|
|
|
}
|
|
|
|
|
|
#[get("/db")]
|
|
|
-async fn db(mut db: Connection<HelloWorld>, id: RequestId) -> Json<World> {
|
|
|
- let number = random_number(&id);
|
|
|
+async fn db(mut db: Connection<HelloWorld>) -> Json<World> {
|
|
|
+ let number = random_number();
|
|
|
|
|
|
let result : World = sqlx::query_as("SELECT id, randomnumber FROM World WHERE id = $1").bind(number)
|
|
|
.fetch_one(&mut *db).await.ok().expect("error loading world");
|
|
@@ -52,12 +49,12 @@ async fn db(mut db: Connection<HelloWorld>, id: RequestId) -> Json<World> {
|
|
|
}
|
|
|
|
|
|
#[get("/queries")]
|
|
|
-async fn queries_empty(db: Connection<HelloWorld>, id: RequestId) -> Json<Vec<World>> {
|
|
|
- queries(db, id,1).await
|
|
|
+async fn queries_empty(db: Connection<HelloWorld>) -> Json<Vec<World>> {
|
|
|
+ queries(db,1).await
|
|
|
}
|
|
|
|
|
|
#[get("/queries?<q>")]
|
|
|
-async fn queries(mut db: Connection<HelloWorld>, id: RequestId, q: u16) -> Json<Vec<World>> {
|
|
|
+async fn queries(mut db: Connection<HelloWorld>, q: u16) -> Json<Vec<World>> {
|
|
|
let q = if q == 0 {
|
|
|
1
|
|
|
} else if q > 500 {
|
|
@@ -69,7 +66,7 @@ async fn queries(mut db: Connection<HelloWorld>, id: RequestId, q: u16) -> Json<
|
|
|
let mut results = Vec::with_capacity(q as usize);
|
|
|
|
|
|
for _ in 0..q {
|
|
|
- let query_id = random_number(&id);
|
|
|
+ let query_id = random_number();
|
|
|
|
|
|
let result :World = sqlx::query_as("SELECT * FROM World WHERE id = $1").bind(query_id)
|
|
|
.fetch_one(&mut *db).await.ok().expect("error loading world");
|
|
@@ -108,12 +105,12 @@ async fn fortunes(mut db: Connection<HelloWorld>) -> RawHtml<String> {
|
|
|
}
|
|
|
|
|
|
#[get("/updates")]
|
|
|
-async fn updates_empty(db: Connection<HelloWorld>, id: RequestId) -> Json<Vec<World>> {
|
|
|
- updates(db, id,1).await
|
|
|
+async fn updates_empty(db: Connection<HelloWorld>) -> Json<Vec<World>> {
|
|
|
+ updates(db,1).await
|
|
|
}
|
|
|
|
|
|
#[get("/updates?<q>")]
|
|
|
-async fn updates(mut db: Connection<HelloWorld>, id: RequestId, q: u16) -> Json<Vec<World>> {
|
|
|
+async fn updates(mut db: Connection<HelloWorld>, q: u16) -> Json<Vec<World>> {
|
|
|
let q = if q == 0 {
|
|
|
1
|
|
|
} else if q > 500 {
|
|
@@ -125,11 +122,11 @@ async fn updates(mut db: Connection<HelloWorld>, id: RequestId, q: u16) -> Json<
|
|
|
let mut results = Vec::with_capacity(q as usize);
|
|
|
|
|
|
for _ in 0..q {
|
|
|
- let query_id = random_number(&id);
|
|
|
+ let query_id = random_number();
|
|
|
let mut result :World = sqlx::query_as("SELECT * FROM World WHERE id = $1").bind(query_id)
|
|
|
.fetch_one(&mut *db).await.ok().expect("World was not found");
|
|
|
|
|
|
- result.random_number = random_number(&id);
|
|
|
+ result.random_number = random_number();
|
|
|
results.push(result);
|
|
|
}
|
|
|
|
|
@@ -192,104 +189,3 @@ pub fn launch() -> Rocket<Build> {
|
|
|
.attach(HelloWorld::init())
|
|
|
}
|
|
|
|
|
|
-#[cfg(test)]
|
|
|
-mod tests
|
|
|
-{
|
|
|
- use std::env;
|
|
|
- use rocket::{Rocket, Build};
|
|
|
- use rocket::{local::blocking::Client};
|
|
|
- use crate::database::HelloWorld;
|
|
|
- use rocket::fairing::{self, AdHoc};
|
|
|
- use rocket_db_pools::Database;
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn plaintext() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/plaintext").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "Hello, World!");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn json() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/json").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "{\"message\":\"Hello, World!\"}");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn db() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/db").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "{\"id\":1,\"randomNumber\":101}");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn queries_empty() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/queries").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "[{\"id\":1,\"randomNumber\":101}]");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn queries_non_empty() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/queries?q=3").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "[{\"id\":1,\"randomNumber\":101},{\"id\":2,\"randomNumber\":102},{\"id\":3,\"randomNumber\":103}]");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn fortunes() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/fortunes").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr><tr><td>11</td><td><script>alert("This should not be displayed in a browser alert box.");</script></td></tr><tr><td>4</td><td>A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1</td></tr><tr><td>5</td><td>A computer program does what you tell it to do, not what you want it to do.</td></tr><tr><td>2</td><td>A computer scientist is someone who fixes things that aren't broken.</td></tr><tr><td>8</td><td>A list is only as strong as its weakest link. — Donald Knuth</td></tr><tr><td>0</td><td>Additional fortune added at request time.</td></tr><tr><td>3</td><td>After enough decimal places, nobody gives a damn.</td></tr><tr><td>7</td><td>Any program that runs right is obsolete.</td></tr><tr><td>10</td><td>Computers make very fast, very accurate mistakes.</td></tr><tr><td>6</td><td>Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen</td></tr><tr><td>9</td><td>Feature: A bug with seniority.</td></tr><tr><td>1</td><td>fortune: No such file or directory</td></tr><tr><td>12</td><td>フレームワークのベンチマーク</td></tr></table></body></html>");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn updates_empty() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/updates").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "[{\"id\":1,\"randomNumber\":2}]");
|
|
|
- }
|
|
|
-
|
|
|
- #[test]
|
|
|
- fn updates_non_empty() {
|
|
|
- let client = create_client();
|
|
|
-
|
|
|
- let response = client.get("/updates?q=3").dispatch();
|
|
|
- assert_eq!(response.into_string().unwrap(), "[{\"id\":1,\"randomNumber\":2},{\"id\":3,\"randomNumber\":4},{\"id\":5,\"randomNumber\":6}]");
|
|
|
- }
|
|
|
-
|
|
|
- fn create_client() -> Client {
|
|
|
- env::set_var("ROCKET_BENCHMARK_DATABASE_URL", "sqlite::memory:");
|
|
|
-
|
|
|
- let rocket = create_rocket();
|
|
|
- let client = Client::debug(rocket).unwrap();
|
|
|
- client
|
|
|
- }
|
|
|
-
|
|
|
- fn create_rocket() -> Rocket<Build> {
|
|
|
- super::launch().attach(AdHoc::try_on_ignite("SQLx Migrations", run_migrations))
|
|
|
- }
|
|
|
-
|
|
|
- async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
|
|
|
- match HelloWorld::fetch(&rocket) {
|
|
|
- Some(db) => match sqlx::migrate!("db/migrations").run(&**db).await {
|
|
|
- Ok(_) => Ok(rocket),
|
|
|
- Err(e) => {
|
|
|
- error!("Failed to initialize HelloWorld database: {}", e);
|
|
|
- Err(rocket)
|
|
|
- }
|
|
|
- }
|
|
|
- None => Err(rocket),
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|