Skip to content

Commit

Permalink
Merge pull request #3 from bmrlab/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kang-oy authored Mar 8, 2024
2 parents 5ea1cc6 + 547eab5 commit 90ae87e
Show file tree
Hide file tree
Showing 41 changed files with 1,675 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
dockerfile
.dockerignore
.git
.gitignore
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/<executable file>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]

[package]
name = "muse_integrator"
name = "credits"
version = "0.1.0"
edition = "2021"

Expand All @@ -25,14 +25,19 @@ sea-orm = { version = "1.0.0-rc.1", features = [
"runtime-tokio-rustls",
"macros",
] }
redis = "0.22.3"

axum = "0.7.1"
include_dir = "0.7"
uuid = { version = "1.6.0", features = ["v4"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }

[dependencies.mongodb]
version = "2.8.0"
features = ["tokio-runtime"]

[[bin]]
name = "muse_integrator-cli"
name = "credits-cli"
path = "src/bin/main.rs"
required-features = []

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# muse-integrator
# credits

积分账户模块中的积分交易处理,账单信息模块

Expand All @@ -16,6 +16,16 @@ cargo install sea-orm-cli

- 项目启动

如果需要本地使用,需要搭建本地 redis,mysql。 并创建数据库。

- 执行表结构生成

```
cargo loco db migrate
```

loco 启动项目:

```
cargo loco start
```
10 changes: 5 additions & 5 deletions config/development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ logger:
# Log level, options: trace, debug, info, warn or error.
level: debug
# Define the logging format. options: compact, pretty or Json
format: compact
format: pretty
# By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
# override_filter: trace

# Web server configuration
server:
# Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
port: 3000
port: 8081
# The UI hostname or IP address that mailers will point to.
host: http://localhost
host: http://127.0.0.1
# Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
middlewares:
# Enable Etag cache header middleware
Expand Down Expand Up @@ -62,7 +62,7 @@ server:
# Database Configuration
database:
# Database connection URI
uri: {{ get_env(name="DATABASE_URL", default="mysql://root:123456@localhost:3306/muse_integrator") }}
uri: {{ get_env(name="DATABASE_URL", default="mysql://root:123456@192.168.20.44:3306/muse_integrator") }}
# When enabled, the sql query will be logged.
enable_logging: false
# Set the timeout duration when acquiring a connection.
Expand All @@ -83,7 +83,7 @@ database:
# Redis Configuration
redis:
# Redis connection URI
uri: {{ get_env(name="REDIS_URL", default="redis://127.0.0.1") }}
uri: {{ get_env(name="REDIS_URL", default="redis://192.168.20.44") }}
# Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_flush: false

Expand Down
90 changes: 90 additions & 0 deletions config/production.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Loco configuration file documentation

# Application logging configuration
logger:
# Enable or disable logging.
enable: true
# Enable pretty backtrace (sets RUST_BACKTRACE=1)
pretty_backtrace: true
# Log level, options: trace, debug, info, warn or error.
level: debug
# Define the logging format. options: compact, pretty or Json
format: pretty
# By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
# override_filter: trace

# Web server configuration
server:
# Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
port: 8081
# The UI hostname or IP address that mailers will point to.
host: http://localhost
# Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
middlewares:
# Enable Etag cache header middleware
etag:
enable: true
# Allows to limit the payload size request. payload that bigger than this file will blocked the request.
limit_payload:
# Enable/Disable the middleware.
enable: true
# the limit size. can be b,kb,kib,mb,mib,gb,gib
body_limit: 5mb
# Generating a unique request ID and enhancing logging with additional information such as the start and completion of request processing, latency, status code, and other request details.
logger:
# Enable/Disable the middleware.
enable: true
# when your code is panicked, the request still returns 500 status code.
catch_panic:
# Enable/Disable the middleware.
enable: true
# Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned.
timeout_request:
# Enable/Disable the middleware.
enable: false
# Duration time in milliseconds.
timeout: 5000
cors:
enable: true
# Set the value of the [`Access-Control-Allow-Origin`][mdn] header
# allow_origins:
# - https://loco.rs
# Set the value of the [`Access-Control-Allow-Headers`][mdn] header
# allow_headers:
# - Content-Type
# Set the value of the [`Access-Control-Allow-Methods`][mdn] header
# allow_methods:
# - POST
# Set the value of the [`Access-Control-Max-Age`][mdn] header in seconds
# max_age: 3600

# Database Configuration
database:
# Database connection URI
uri: {{ get_env(name="DATABASE_URL", default="mysql://root:123456@localhost:3306/muse_integrator") }}
# When enabled, the sql query will be logged.
enable_logging: false
# Set the timeout duration when acquiring a connection.
connect_timeout: 500
# Set the idle duration before closing a connection.
idle_timeout: 500
# Minimum number of connections for a pool.
min_connections: 1
# Maximum number of connections for a pool.
max_connections: 1
# Run migration up when application loaded
auto_migrate: true
# Truncate database when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_truncate: false
# Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_recreate: false

# Redis Configuration
redis:
# Redis connection URI
uri: {{ get_env(name="REDIS_URL", default="redis://127.0.0.1") }}
# Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_flush: false


2 changes: 1 addition & 1 deletion config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mailer:
# Database Configuration
database:
# Database connection URI
uri: {{get_env(name="DATABASE_URL", default="postgres://loco:loco@localhost:5432/muse_integrator_test")}}
uri: {{get_env(name="DATABASE_URL", default="mysql://root:123456@localhost:3306/muse_integrator")}}
# When enabled, the sql query will be logged.
enable_logging: false
# Set the timeout duration when acquiring a connection.
Expand Down
18 changes: 18 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM rust:1.74-slim as builder

WORKDIR /usr/src/

COPY . .

RUN cargo build --release

FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y libc6

WORKDIR /usr/app

COPY --from=builder /usr/src/config /usr/app/config
COPY --from=builder /usr/src/target/release/credits-cli /usr/app/credits-cli

ENTRYPOINT ["/usr/app/credits-cli"]
141 changes: 129 additions & 12 deletions examples/playground.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,143 @@
use chrono::{TimeZone, Utc};
use credits::{
app::App,
models::_entities::transaction_events::{self},
};
use eyre::Context;
#[allow(unused_imports)]
use loco_rs::{cli::playground, prelude::*};
use muse_integrator::{
app::App,
models::_entities::wallets::{self, ActiveModel},
use mongodb::{
bson::{Bson, Document},
Client, Collection,
};
use sea_orm::prelude::Decimal;
use serde_json::Value as Json;
use std::env;

#[tokio::main]
async fn main() -> eyre::Result<()> {
let ctx = playground::<App>().await.context("playground")?;

let active_model = ActiveModel {
addr: Set("0xadsfasdf".to_string()),
balance: Set(Decimal::new(2000, 2)),
status: Set(1),
..Default::default()
};
active_model.insert(&ctx.db).await.unwrap();
// ctx,.

let uri = env::var("url")?;

let client = Client::with_uri_str(uri).await?;
// Get a handle on the movies collection
let database = client.database("damaigc-credits-billing");
let my_coll: Collection<Document> = database.collection("transaction_event");

let mut cursor = my_coll.find(None, None).await?;
while cursor.advance().await? {
let mut tran = transaction_events::ActiveModel {
..Default::default()
};

let doc = &cursor.deserialize_current()?;

println!("{:?}", doc);
let mut id = 0;

let event_exec_id_opt = doc.get("event_exec_id");
if let Some(v) = event_exec_id_opt {
if let Bson::Int32(r) = v {
id = r.clone();
tran.id = Set(r.to_owned());
}
}
let temp = transaction_events::Entity::find_by_id(id)
.one(&ctx.db)
.await
.unwrap();
if let None = temp {
let event_type_opt = doc.get("event_type");
if let Some(event_type) = event_type_opt {
if let Bson::String(v) = event_type {
tran.event_type = Set(v.to_string());
}
}

let from_addr_opt = doc.get("from_addr");
if let Some(from_addr) = from_addr_opt {
if let Bson::String(v) = from_addr {
tran.from_addr = Set(Some(v.to_string()));
}
}

let to_addr_opt = doc.get("to_addr");
if let Some(to_addr) = to_addr_opt {
if let Bson::String(v) = to_addr {
tran.to_addr = Set(Some(v.to_string()));
}
}

let amount_opt = doc.get("amount");
if let Some(amount) = amount_opt {
if let Bson::Double(v) = amount {
tran.amount = Set(Decimal::new((v * 100.0).round() as i64, 2));
}
}

let info_opt = doc.get("info");
if let Some(info) = info_opt {
if let Bson::Document(v) = info {
tran.info = Set(Some(Json::from_iter(v.clone())));
}
}

let account_event_id_opt = doc.get("_id");
if let Some(v) = account_event_id_opt {
if let Bson::String(r) = v {
tran.event_id = Set(r.to_string());
}
}

let trace_id_id_opt: Option<&Bson> = doc.get("trace_id");
if let Some(v) = trace_id_id_opt {
if let Bson::String(r) = v {
tran.trace_id = Set(r.to_string());
}
}

let status_opt = doc.get("status");
if let Some(v) = status_opt {
if let Bson::Int32(r) = v {
tran.state = Set(r.to_owned() as i16);
}
}

let callback_url_opt = doc.get("callback_url");
if let Some(v) = callback_url_opt {
if let Bson::String(r) = v {
tran.callback_url = Set(Some(r.to_string()));
}
}

let status_msg_opt = doc.get("status_msg");
if let Some(v) = status_msg_opt {
if let Bson::String(r) = v {
tran.status_msg = Set(Some(r.to_string()));
}
}

let res = wallets::Entity::find().all(&ctx.db).await.unwrap();
println!("{:?}", res);
let created_at_opt = doc.get("created_at");
if let Some(v) = created_at_opt {
if let Bson::DateTime(r) = v {
tran.created_at = Set(Utc.timestamp_millis_opt(r.timestamp_millis()).unwrap());
}
}
let updated_at_opt = doc.get("updated_at");
if let Some(v) = updated_at_opt {
if let Bson::DateTime(r) = v {
tran.updated_at = Set(Utc.timestamp_millis_opt(r.timestamp_millis()).unwrap());
}
}
println!("{:?}", &tran);
println!("开始转储------");
tran.insert(&ctx.db).await.unwrap();
}
}
println!("转储完成------");

Ok(())
}
Loading

0 comments on commit 90ae87e

Please sign in to comment.