Browse Source

Update ohkami & Refactor some (#8955)

kanarus 1 year ago
parent
commit
449f9f1359

+ 7 - 7
frameworks/Rust/ohkami/Cargo.lock

@@ -770,9 +770,9 @@ dependencies = [
 
 [[package]]
 name = "ohkami"
-version = "0.17.1"
+version = "0.18.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ffca2ddf66df3484fd9dfa7f88f923e2082b9934dc7e247616002f8bf71ec81c"
+checksum = "d810447a32af4750a45183a4915db5ca4183bb6d44b8e7731e63f627c581d93e"
 dependencies = [
  "byte_reader",
  "hmac",
@@ -787,7 +787,7 @@ dependencies = [
 
 [[package]]
 name = "ohkami_framework_benchmarks"
-version = "0.17.1"
+version = "0.18.2"
 dependencies = [
  "futures-util",
  "ohkami",
@@ -799,9 +799,9 @@ dependencies = [
 
 [[package]]
 name = "ohkami_lib"
-version = "0.2.1"
+version = "0.2.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a306cca964938b3e37157c716b4423f09026ceb4439ec5f9353265d03d217b72"
+checksum = "4433e6c4c6c67a72afc306783ca8c641e69c9d171279fcab51f1f0c2d875e121"
 dependencies = [
  "byte_reader",
  "percent-encoding",
@@ -810,9 +810,9 @@ dependencies = [
 
 [[package]]
 name = "ohkami_macros"
-version = "0.7.1"
+version = "0.7.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1cdb435788e84e7262f0ee3fb1d206a4c1830ec17856eb0924b9d8c5f75c7519"
+checksum = "ffed4cd7e8c12a62c077273cb0f0fedf67ca3912488e5de1b325be85ca86bb06"
 dependencies = [
  "proc-macro2",
  "quote",

+ 2 - 2
frameworks/Rust/ohkami/Cargo.toml

@@ -1,11 +1,11 @@
 [package]
 name    = "ohkami_framework_benchmarks"
-version = "0.17.1"
+version = "0.18.2"
 edition = "2021"
 authors = ["kanarus <[email protected]>"]
 
 [dependencies]
-ohkami       = { version = "=0.17.1", features = ["rt_tokio"] }
+ohkami       = { version = "=0.18.2", features = ["rt_tokio"] }
 tokio        = { version = "1.37.0" , features = ["full"] }
 rand         = { version = "0.8.5"  , features = ["small_rng"] }
 sqlx         = { version = "0.7.4"  , features = ["postgres", "macros", "runtime-tokio-native-tls"] }

+ 1 - 1
frameworks/Rust/ohkami/ohkami.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.77-slim-buster
+FROM rust:1.78-slim-buster
 WORKDIR /ohkami_framework_benchmarks
 
 ENV DATABASE_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world

+ 14 - 60
frameworks/Rust/ohkami/src/fangs.rs

@@ -11,12 +11,17 @@ impl FangAction for SetServer {
     }
 }
 
-#[derive(Clone)]
-pub struct UsePostgres(
-    Postgres
-);
-impl UsePostgres {
-    pub async fn init() -> Self {
+impl Postgres {
+    pub async fn init() -> impl FangAction {
+        #[derive(Clone)]
+        pub struct UsePostgres(Postgres);
+        impl FangAction for UsePostgres {
+            #[inline(always)]
+            async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
+                Ok(req.memorize(self.0.clone()))
+            }
+        }
+
         macro_rules! load_env {
             ($($name:ident as $t:ty)*) => {$(
                 #[allow(non_snake_case)]
@@ -33,64 +38,13 @@ impl UsePostgres {
             MIN_CONNECTIONS as u32
             DATABASE_URL    as String
         }
-
+            
         let pool = sqlx::postgres::PgPoolOptions::new()
             .max_connections(MAX_CONNECTIONS)
             .min_connections(MIN_CONNECTIONS)
             .connect(&DATABASE_URL).await
             .unwrap();
-
-        Self(pool.into())
-    }
-}
-impl FangAction for UsePostgres {
-    #[inline(always)]
-    async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
-        Ok(req.memorize(self.0.clone()))
-    }
-}
-
-
-/*
-impl Postgres {
-    pub async fn init() -> impl FangAction {
-        #[derive(Clone)]
-        pub struct UsePostgres(Postgres);
-
-        impl FangAction for UsePostgres {
-            #[inline(always)]
-            async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
-                req.memorize(self.0.clone());
-                Ok(())
-            }
-        }
-
-        macro_rules! load_env {
-            ($($name:ident as $t:ty)*) => {
-                $(
-                    #[allow(non_snake_case)]
-                    let $name = ::std::env::var(stringify!($name))
-                        .expect(concat!(
-                            "Failed to load environment variable ",
-                            "`", stringify!($name), "`"
-                        ))
-                        .parse::<$t>()
-                        .unwrap();
-                )*
-            };
-        } load_env! {
-            MAX_CONNECTIONS as u32
-            MIN_CONNECTIONS as u32
-            DATABASE_URL    as String
-        }
-
-        UsePostgres(Self(
-            sqlx::postgres::PgPoolOptions::new()
-                .max_connections(MAX_CONNECTIONS)
-                .min_connections(MIN_CONNECTIONS)
-                .connect(&DATABASE_URL).await
-                .unwrap()
-        ))
+            
+        UsePostgres(pool.into())
     }
 }
-*/

+ 2 - 3
frameworks/Rust/ohkami/src/main.rs

@@ -1,5 +1,5 @@
 mod fangs;
-use fangs::{SetServer, UsePostgres};
+use fangs::SetServer;
 
 mod models;
 use models::{Fortune, Message, World, WorldsQuery};
@@ -16,7 +16,7 @@ use ohkami::Memory;
 
 #[tokio::main]
 async fn main() {
-    Ohkami::with((SetServer, UsePostgres::init().await), (
+    Ohkami::with((SetServer, Postgres::init().await), (
         "/json"     .GET(json_serialization),
         "/db"       .GET(single_database_query),
         "/queries"  .GET(multiple_database_query),
@@ -48,7 +48,6 @@ async fn fortunes(p: Memory<'_, Postgres>) -> FortunesTemplate {
         id:      0,
         message: String::from("Additional fortune added at request time."),
     });
-
     fortunes.sort_unstable_by(|a, b| str::cmp(&a.message, &b.message));
 
     FortunesTemplate { fortunes }