Commit 889c6ad0 authored by 陈冲's avatar 陈冲

rename fc-backend

parent a336c43f
......@@ -152,32 +152,6 @@ dependencies = [
]
[[package]]
name = "backend-ds"
version = "0.1.0"
dependencies = [
"aes",
"async-trait",
"base64",
"bytes",
"cbc",
"chrono",
"dashmap",
"lazy_static",
"md-5",
"rand 0.10.0",
"rmp-serde",
"rust-embed",
"salvo",
"serde",
"serde_json",
"socketioxide",
"sqlx",
"tokio",
"tower",
"tower-http",
]
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
......@@ -783,6 +757,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "fc-backend"
version = "0.1.0"
dependencies = [
"aes",
"async-trait",
"base64",
"bytes",
"cbc",
"chrono",
"dashmap",
"lazy_static",
"md-5",
"rand 0.10.0",
"rmp-serde",
"rust-embed",
"salvo",
"serde",
"serde_json",
"socketioxide",
"sqlx",
"tokio",
"tower",
"tower-http",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
......
[package]
name = "backend-ds"
name = "fc-backend"
version = "0.1.0"
edition = "2024"
......@@ -27,7 +27,7 @@ cbc = { version = "0.1.2", features = ["alloc"] }
md5 = { version = "0.10.6", package = "md-5" }
[[bin]]
name = "backend-ds"
name = "fc-backend"
# [target.aarch64-unknown-linux-gnu]
# linker = "aarch64-linux-gnu-gcc"
......
......@@ -21,9 +21,8 @@ pub struct Config {
#[derive(Clone, Deserialize, Serialize, Debug)]
#[serde(default)]
pub struct DbConfig {
// 数据库类型:mongodb、postgresql、mysql、sqlite。
// 数据库类型:postgresql、mysql、sqlite。(暂只处理了postgresql)
pub kind: String,
// mongodb://admin:123456@127.0.0.1:27017/backend_ds
// postgresql://admin:123456@localhost:5432/backend_ds
pub uri: String,
}
......
......@@ -3,109 +3,8 @@ mod db;
mod protocols;
mod router;
use commons::utils::CONFIG as config;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
pub async fn init_postgresql_schema() -> Result<(), String> {
let pool = PgPoolOptions::new()
.max_connections(4)
.connect(&config.db.uri)
.await
.map_err(|err| err.to_string())?;
sqlx::raw_sql(
"create table if not exists player_profiles (
userid text primary key,
nickname text not null,
level bigint not null default 1,
exp bigint not null default 0,
avatar_id bigint not null default 0,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create table if not exists player_wallets (
userid text primary key references player_profiles(userid) on delete cascade,
gold bigint not null default 0,
diamond bigint not null default 0,
stamina bigint not null default 100,
updated_at timestamptz not null default now()
);
create table if not exists player_items (
id bigserial primary key,
userid text not null references player_profiles(userid) on delete cascade,
item_id text not null,
item_count bigint not null default 0,
updated_at timestamptz not null default now(),
unique(userid, item_id)
);
create table if not exists player_mails (
id bigserial primary key,
userid text not null references player_profiles(userid) on delete cascade,
title text not null,
content text not null default '',
attachments jsonb not null default '[]'::jsonb,
status text not null default 'unread',
created_at timestamptz not null default now()
);
create table if not exists task_idempotency (
task_id text primary key,
task_type text not null,
status text not null default 'running',
attempt bigint not null default 1,
result jsonb,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
alter table player_profiles alter column level type bigint;
alter table player_profiles alter column avatar_id type bigint;
alter table player_wallets alter column stamina type bigint;",
)
.execute(&pool)
.await
.map_err(|err| err.to_string())?;
migrate_legacy_players(&pool).await
}
async fn migrate_legacy_players(pool: &PgPool) -> Result<(), String> {
let row: (bool,) = sqlx::query_as("select to_regclass('public.players') is not null")
.fetch_one(pool)
.await
.map_err(|err| err.to_string())?;
let has_legacy_table = row.0;
if !has_legacy_table {
return Ok(());
}
sqlx::raw_sql(
"insert into player_profiles (userid, nickname)
select userid, nickname
from players
on conflict (userid)
do update set nickname = excluded.nickname;
insert into player_wallets (userid)
select userid
from players
on conflict (userid) do nothing;
drop table if exists players;",
)
.execute(pool)
.await
.map_err(|err| err.to_string())?;
Ok(())
}
#[cfg(test)]
mod tests {
use serde_json::json;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::commons::utils;
......@@ -153,7 +52,7 @@ mod tests {
.unwrap_or(0);
format!("test_userid_{ts}")
}
#[tokio::test]
async fn test_current_timestamp() {
let ts = utils::get_current_timestamp();
......
......@@ -13,19 +13,18 @@ pub fn config_router() -> Router {
.layer(CorsLayer::permissive())
.layer(layer);
let layer = layer.compat();
let mut r = Router::new().push(
Router::with_path(config.socket_path.as_str())
.hoop(layer)
.goal(hello),
);
io.ns(
config.client_ns.as_str(),
crate::protocols::socket_io::on_connect,
);
r = r.push(Router::with_path("/manager/login").post(crate::protocols::socket_io::login));
r
let layer = layer.compat();
Router::new()
.push(
Router::with_path(config.socket_path.as_str())
.hoop(layer)
.goal(hello),
)
.push(Router::with_path("/manager/login").post(crate::protocols::socket_io::login))
}
pub fn config_catcher() -> Catcher {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment