mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-07 10:14:35 +00:00
a78b759741
This patch implement a restful server without any auth.
usage:
```bash
# run easytier-web, which acts as an gateway and registry for all easytier-core
$> easytier-web
# run easytier-core and connect to easytier-web with a token
$> easytier-core --config-server udp://127.0.0.1:22020/fdsafdsa
# use restful api to list session
$> curl -H "Content-Type: application/json" -X GET 127.0.0.1:11211/api/v1/sessions
[{"token":"fdsafdsa","client_url":"udp://127.0.0.1:48915","machine_id":"de3f5b8f-0f2f-d9d0-fb30-a2ac8951d92f"}]%
# use restful api to run a network instance
$> curl -H "Content-Type: application/json" -X POST 127.0.0.1:11211/api/v1/network/de3f5b8f-0f2f-d9d0-fb30-a2ac8951d92f -d '{"config": "listeners = [\"udp://0.0.0.0:12344\"]"}'
# use restful api to get network instance info
$> curl -H "Content-Type: application/json" -X GET 127.0.0.1:11211/api/v1/network/de3f5b8f-0f2f-d9d0-fb30-a2ac8951d92f/65437e50-b286-4098-a624-74429f2cb839
```
85 lines
3.4 KiB
Rust
85 lines
3.4 KiB
Rust
use prost::DecodeError;
|
|
|
|
use super::rpc_types;
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/error.rs"));
|
|
|
|
impl From<&rpc_types::error::Error> for Error {
|
|
fn from(e: &rpc_types::error::Error) -> Self {
|
|
use super::error::error::ErrorKind as ProtoError;
|
|
match e {
|
|
rpc_types::error::Error::ExecutionError(e) => Self {
|
|
error_kind: Some(ProtoError::ExecuteError(ExecuteError {
|
|
error_message: format!("{:?}", e),
|
|
})),
|
|
},
|
|
rpc_types::error::Error::DecodeError(_) => Self {
|
|
error_kind: Some(ProtoError::ProstDecodeError(ProstDecodeError {})),
|
|
},
|
|
rpc_types::error::Error::EncodeError(_) => Self {
|
|
error_kind: Some(ProtoError::ProstEncodeError(ProstEncodeError {})),
|
|
},
|
|
rpc_types::error::Error::InvalidMethodIndex(m, s) => Self {
|
|
error_kind: Some(ProtoError::InvalidMethodIndex(InvalidMethodIndex {
|
|
method_index: *m as u32,
|
|
service_name: format!("{:?}", s),
|
|
})),
|
|
},
|
|
rpc_types::error::Error::InvalidServiceKey(s, _) => Self {
|
|
error_kind: Some(ProtoError::InvalidService(InvalidService {
|
|
service_name: format!("{:?}", s),
|
|
})),
|
|
},
|
|
rpc_types::error::Error::MalformatRpcPacket(e) => Self {
|
|
error_kind: Some(ProtoError::MalformatRpcPacket(MalformatRpcPacket {
|
|
error_message: format!("{:?}", e),
|
|
})),
|
|
},
|
|
rpc_types::error::Error::Timeout(e) => Self {
|
|
error_kind: Some(ProtoError::Timeout(Timeout {
|
|
error_message: format!("{:?}", e),
|
|
})),
|
|
},
|
|
#[allow(unreachable_patterns)]
|
|
e => Self {
|
|
error_kind: Some(ProtoError::OtherError(OtherError {
|
|
error_message: format!("{:?}", e),
|
|
})),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&Error> for rpc_types::error::Error {
|
|
fn from(e: &Error) -> Self {
|
|
use super::error::error::ErrorKind as ProtoError;
|
|
match &e.error_kind {
|
|
Some(ProtoError::ExecuteError(e)) => {
|
|
Self::ExecutionError(anyhow::anyhow!(e.error_message.clone()))
|
|
}
|
|
Some(ProtoError::ProstDecodeError(_)) => {
|
|
Self::DecodeError(DecodeError::new("decode error"))
|
|
}
|
|
Some(ProtoError::ProstEncodeError(_)) => {
|
|
Self::DecodeError(DecodeError::new("encode error"))
|
|
}
|
|
Some(ProtoError::InvalidMethodIndex(e)) => {
|
|
Self::InvalidMethodIndex(e.method_index as u8, e.service_name.clone())
|
|
}
|
|
Some(ProtoError::InvalidService(e)) => {
|
|
Self::InvalidServiceKey(e.service_name.clone(), "".to_string())
|
|
}
|
|
Some(ProtoError::MalformatRpcPacket(e)) => {
|
|
Self::MalformatRpcPacket(e.error_message.clone())
|
|
}
|
|
Some(ProtoError::Timeout(e)) => {
|
|
Self::ExecutionError(anyhow::anyhow!(e.error_message.clone()))
|
|
}
|
|
Some(ProtoError::OtherError(e)) => {
|
|
Self::ExecutionError(anyhow::anyhow!(e.error_message.clone()))
|
|
}
|
|
None => Self::ExecutionError(anyhow::anyhow!("unknown error {:?}", e)),
|
|
}
|
|
}
|
|
}
|