Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

现在可以用环境变量修改端口了 #6

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
62 changes: 58 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@

# phira-mp

## Deployment
`phira-mp` is a project developed with Rust. Below are the steps to deploy and run this project.

```shell
# Install Rust first. See https://www.rust-lang.org/tools/install
[简体中文](README.zh-CN.md) | English Version

## Environment

- Rust 1.70 or later

## Server Installation

### For Linux

#### Dependent
First, install Rust if you haven't already. You can do so by following the instructions at https://www.rust-lang.org/tools/install

For Ubuntu or Debian users, use the following command to install `curl` if it isn't installed yet:

```shell
sudo apt install curl
```
For Fedora or CentOS users, use the following command:
```shell
sudo yum install curl
```
After curl is installed, install Rust with the following command:
```shell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
Then, build the project:
```shell
cargo build --release -p phira-mp-server
```
#### unning the Server
You can run the application with the following command:
```shell
RUST_LOG=info target/release/phira-mp-server
```

#### Troubleshooting
If you encounter issues related to openssl, ensure that you have libssl-dev (for Ubuntu or Debian) or openssl-devel (for Fedora or CentOS) installed. If the issue persists, you can set the OPENSSL_DIR environment variable for the compilation process.

If you're compiling on Linux and targeting Linux and get a message about pkg-config being missing, you may need to install it:

```shell
# For Ubuntu or Debian
sudo apt install pkg-config libssl-dev

# For Fedora or CentOS
sudo dnf install pkg-config openssl-devel
```
For other issues, please refer to the specific error messages and adjust your environment accordingly.

#### Monitoring
You can check the running process and the port it's listening on with:
```shell
ps -aux | grep phira-mp-server
netstat -tuln | grep 12345
```
![image](https://github.com/okatu-loli/phira-mp/assets/53247097/b533aee7-03c2-4920-aae9-a0b9e70ed576)

## For Windows or Android
View: [https://docs.qq.com/doc/DU1dlekx3U096REdD](https://docs.qq.com/doc/DU1dlekx3U096REdD)

61 changes: 61 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# phira-mp

`phira-mp` 是一个用 Rust 开发的项目。 以下是部署和运行该项目服务端的步骤。

简体中文 | [English Version](README.md)
## 环境

- Rust 1.70 或更高版本

## 服务端安装
### 对于 `Linux` 用户
#### 依赖
首先,如果尚未安装 Rust,请安装。 您可以按照 https://www.rust-lang.org/tools/install 中的说明进行操作

对于 Ubuntu 或 Debian 用户,如果尚未安装“curl”,请使用以下命令进行安装:

```shell
sudo apt install curl
```
对于 Fedora 或 CentOS 用户,请使用以下命令:
```shell
sudo yum install curl
```
安装curl后,使用以下命令安装Rust:
```shell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
然后,构建项目:
```shell
cargo build --release -p phira-mp-server
```
#### 运行服务端
您可以使用以下命令运行该应用程序:
```shell
RUST_LOG=info target/release/phira-mp-server
```

#### 故障排除
如果遇到与 openssl 相关的问题,请确保安装了 libssl-dev(适用于 Ubuntu 或 Debian)或 openssl-devel(适用于 Fedora 或 CentOS)。 如果问题仍然存在,您可以为编译过程设置 OPENSSL_DIR 环境变量。

如果您在 Linux 上进行编译并以 Linux 为目标,并收到有关缺少 pkg-config 的消息,则可能需要安装它:

```shell
# 对于 Ubuntu 或 Debian
sudo apt install pkg-config libssl-dev

# 对于 Fedora 或 CentOS
sudo dnf install pkg-config openssl-devel
```
其他问题请参考具体错误信息并相应调整您的环境。

#### 监控
您可以检查正在运行的进程及其正在侦听的端口:
```shell
ps -aux | grep phira-mp-server
netstat -tuln | grep 12345
```
![image](https://github.com/okatu-loli/phira-mp/assets/53247097/b533aee7-03c2-4920-aae9-a0b9e70ed576)

## 对于 Windows 或 Android 用户
查看: [https://docs.qq.com/doc/DU1dlekx3U096REdD](https://docs.qq.com/doc/DU1dlekx3U096REdD)
205 changes: 108 additions & 97 deletions phira-mp-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,97 +1,108 @@
mod l10n;

mod room;
pub use room::*;

mod server;
pub use server::*;

mod session;
pub use session::*;

use anyhow::Result;
use std::{
collections::{
hash_map::{Entry, VacantEntry},
HashMap,
},
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
path::Path,
};
use tokio::{net::TcpListener, sync::RwLock};
use tracing::warn;
use tracing_appender::non_blocking::WorkerGuard;
use uuid::Uuid;

pub type SafeMap<K, V> = RwLock<HashMap<K, V>>;
pub type IdMap<V> = SafeMap<Uuid, V>;

fn vacant_entry<V>(map: &mut HashMap<Uuid, V>) -> VacantEntry<'_, Uuid, V> {
let mut id = Uuid::new_v4();
while map.contains_key(&id) {
id = Uuid::new_v4();
}
match map.entry(id) {
Entry::Vacant(entry) => entry,
_ => unreachable!(),
}
}

pub fn init_log(file: &str) -> Result<WorkerGuard> {
use tracing::{metadata::LevelFilter, Level};
use tracing_log::LogTracer;
use tracing_subscriber::{filter, fmt, prelude::*, EnvFilter};

let log_dir = Path::new("log");
if log_dir.exists() {
if !log_dir.is_dir() {
panic!("log exists and is not a folder");
}
} else {
std::fs::create_dir(log_dir).expect("failed to create log folder");
}

LogTracer::init()?;

let (non_blocking, guard) =
tracing_appender::non_blocking(tracing_appender::rolling::hourly(log_dir, file));

let subscriber = tracing_subscriber::registry()
.with(
fmt::layer()
.with_writer(non_blocking)
.with_filter(LevelFilter::DEBUG),
)
.with(
fmt::layer()
.with_writer(std::io::stdout)
.with_filter(EnvFilter::from_default_env()),
)
.with(
filter::Targets::new()
.with_target("hyper", Level::INFO)
.with_target("rustls", Level::INFO)
.with_target("isahc", Level::INFO)
.with_default(Level::TRACE),
);

tracing::subscriber::set_global_default(subscriber).expect("unable to set global subscriber");
Ok(guard)
}

#[tokio::main]
async fn main() -> Result<()> {
let _guard = init_log("phira-mp")?;

let port = 12346;
let addrs: &[SocketAddr] = &[
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), port),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), port),
];
let listener: Server = TcpListener::bind(addrs).await?.into();
loop {
if let Err(err) = listener.accept().await {
warn!("failed to accept: {err:?}");
}
}
}
mod l10n;

mod room;
pub use room::*;

mod server;
pub use server::*;

mod session;
pub use session::*;

use anyhow::Result;
use std::{
collections::{
hash_map::{Entry, VacantEntry},
HashMap,
},
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
path::Path,
};
use tokio::{net::TcpListener, sync::RwLock};
use tracing::{info, warn};
use tracing_appender::non_blocking::WorkerGuard;
use uuid::Uuid;
use std::env;

pub type SafeMap<K, V> = RwLock<HashMap<K, V>>;
pub type IdMap<V> = SafeMap<Uuid, V>;

fn vacant_entry<V>(map: &mut HashMap<Uuid, V>) -> VacantEntry<'_, Uuid, V> {
let mut id = Uuid::new_v4();
while map.contains_key(&id) {
id = Uuid::new_v4();
}
match map.entry(id) {
Entry::Vacant(entry) => entry,
_ => unreachable!(),
}
}

pub fn init_log(file: &str) -> Result<WorkerGuard> {
use tracing::{metadata::LevelFilter, Level};
use tracing_log::LogTracer;
use tracing_subscriber::{filter, fmt, prelude::*, EnvFilter};

let log_dir = Path::new("log");
if log_dir.exists() {
if !log_dir.is_dir() {
panic!("log exists and is not a folder");
}
} else {
std::fs::create_dir(log_dir).expect("failed to create log folder");
}

LogTracer::init()?;

let (non_blocking, guard) =
tracing_appender::non_blocking(tracing_appender::rolling::hourly(log_dir, file));

let subscriber = tracing_subscriber::registry()
.with(
fmt::layer()
.with_writer(non_blocking)
.with_filter(LevelFilter::DEBUG),
)
.with(
fmt::layer()
.with_writer(std::io::stdout)
.with_filter(EnvFilter::from_default_env()),
)
.with(
filter::Targets::new()
.with_target("hyper", Level::INFO)
.with_target("rustls", Level::INFO)
.with_target("isahc", Level::INFO)
.with_default(Level::TRACE),
);

tracing::subscriber::set_global_default(subscriber).expect("unable to set global subscriber");
Ok(guard)
}

fn get_port() -> u16 {
match env::var_os("PHIRA_PORT") {
Some(v) => v.into_string().unwrap().parse::<u16>().unwrap(),
None => 23333
}
}

#[tokio::main]
async fn main() -> Result<()> {
let _guard = init_log("phira-mp")?;

let port = get_port();

let addrs: &[SocketAddr] = &[
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), port),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), port),
];

let listener: Server = TcpListener::bind(addrs).await?.into();
info!("Server running on port {}", port);
loop {
if let Err(err) = listener.accept().await {
warn!("failed to accept: {err:?}");
}
}
}
Empty file.
7 changes: 7 additions & 0 deletions target/启动服务端.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set RUST_LOG=INFO

set PHIRA_PORT=
set /p PHIRA_PORT=Select the port (default 23333):
echo.

start ./release/phira-mp-server.exe