|
@@ -11,12 +11,16 @@ mod templates;
|
|
use templates::FortunesTemplate;
|
|
use templates::FortunesTemplate;
|
|
|
|
|
|
use ohkami::prelude::*;
|
|
use ohkami::prelude::*;
|
|
|
|
+use ohkami::format::{JSON, Query};
|
|
use ohkami::Memory;
|
|
use ohkami::Memory;
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
#[tokio::main]
|
|
async fn main() {
|
|
async fn main() {
|
|
- Ohkami::with((SetServer, Postgres::init().await), (
|
|
|
|
|
|
+ Ohkami::with((
|
|
|
|
+ SetServer,
|
|
|
|
+ Memory::new(Postgres::new().await),
|
|
|
|
+ ), (
|
|
"/json" .GET(json_serialization),
|
|
"/json" .GET(json_serialization),
|
|
"/db" .GET(single_database_query),
|
|
"/db" .GET(single_database_query),
|
|
"/queries" .GET(multiple_database_query),
|
|
"/queries" .GET(multiple_database_query),
|
|
@@ -26,40 +30,44 @@ async fn main() {
|
|
)).howl("0.0.0.0:8000").await
|
|
)).howl("0.0.0.0:8000").await
|
|
}
|
|
}
|
|
|
|
|
|
-async fn json_serialization() -> Message {
|
|
|
|
- Message {
|
|
|
|
|
|
+async fn json_serialization() -> JSON<Message> {
|
|
|
|
+ JSON(Message {
|
|
message: "Hello, World!"
|
|
message: "Hello, World!"
|
|
- }
|
|
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
-async fn single_database_query(p: Memory<'_, Postgres>) -> World {
|
|
|
|
- p.select_random_world().await
|
|
|
|
|
|
+async fn single_database_query(p: Memory<'_, Postgres>) -> JSON<World> {
|
|
|
|
+ let world = p.select_random_world().await;
|
|
|
|
+ JSON(world)
|
|
}
|
|
}
|
|
|
|
|
|
-async fn multiple_database_query(q: WorldsQuery<'_>, p: Memory<'_, Postgres>) -> Vec<World> {
|
|
|
|
|
|
+async fn multiple_database_query(
|
|
|
|
+ Query(q): Query<WorldsQuery<'_>>,
|
|
|
|
+ p: Memory<'_, Postgres>
|
|
|
|
+) -> JSON<Vec<World>> {
|
|
let n = q.parse();
|
|
let n = q.parse();
|
|
- p.select_n_random_worlds(n).await
|
|
|
|
|
|
+ let worlds = p.select_n_random_worlds(n).await;
|
|
|
|
+ JSON(worlds)
|
|
}
|
|
}
|
|
|
|
|
|
async fn fortunes(p: Memory<'_, Postgres>) -> FortunesTemplate {
|
|
async fn fortunes(p: Memory<'_, Postgres>) -> FortunesTemplate {
|
|
let mut fortunes = p.select_all_fortunes().await;
|
|
let mut fortunes = p.select_all_fortunes().await;
|
|
-
|
|
|
|
fortunes.push(Fortune {
|
|
fortunes.push(Fortune {
|
|
id: 0,
|
|
id: 0,
|
|
message: String::from("Additional fortune added at request time."),
|
|
message: String::from("Additional fortune added at request time."),
|
|
});
|
|
});
|
|
fortunes.sort_unstable_by(|a, b| str::cmp(&a.message, &b.message));
|
|
fortunes.sort_unstable_by(|a, b| str::cmp(&a.message, &b.message));
|
|
-
|
|
|
|
FortunesTemplate { fortunes }
|
|
FortunesTemplate { fortunes }
|
|
}
|
|
}
|
|
|
|
|
|
-async fn database_updates(q: WorldsQuery<'_>, p: Memory<'_, Postgres>) -> Vec<World> {
|
|
|
|
|
|
+async fn database_updates(
|
|
|
|
+ Query(q): Query<WorldsQuery<'_>>,
|
|
|
|
+ p: Memory<'_, Postgres>
|
|
|
|
+) -> JSON<Vec<World>> {
|
|
let n = q.parse();
|
|
let n = q.parse();
|
|
let mut worlds = p.select_n_random_worlds(n).await;
|
|
let mut worlds = p.select_n_random_worlds(n).await;
|
|
-
|
|
|
|
p.update_random_ids_of_worlds(&mut worlds).await;
|
|
p.update_random_ids_of_worlds(&mut worlds).await;
|
|
-
|
|
|
|
- worlds
|
|
|
|
|
|
+ JSON(worlds)
|
|
}
|
|
}
|
|
|
|
|
|
async fn plaintext() -> &'static str {
|
|
async fn plaintext() -> &'static str {
|