style: extract error to src/error.rs
This commit is contained in:
34
src/error.rs
Normal file
34
src/error.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use std::fmt::{ self, Display, Formatter };
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum GetStreamError {
|
||||||
|
ReqwestError(reqwest::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for GetStreamError {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
GetStreamError::ReqwestError(e) => write!(f, "{}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for GetStreamError {
|
||||||
|
fn cause(&self) -> Option<&dyn Error> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<reqwest::Error> for GetStreamError {
|
||||||
|
fn from(reqwest_error: reqwest::Error) -> Self {
|
||||||
|
GetStreamError::ReqwestError(reqwest_error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<GetStreamError> for actix_http::error::Error {
|
||||||
|
fn from(get_stream_error: GetStreamError) -> Self {
|
||||||
|
failure!("Get stream error: {}", get_stream_error);
|
||||||
|
actix_http::error::Error::from(())
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/main.rs
38
src/main.rs
@@ -1,42 +1,18 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rust_util;
|
extern crate rust_util;
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::fmt::{ self, Display, Formatter };
|
|
||||||
use std::error::Error;
|
|
||||||
use std::task::{ Context, Poll };
|
use std::task::{ Context, Poll };
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use clap::{ App, Arg };
|
use clap::{ App, Arg };
|
||||||
use actix_web::{ web, HttpServer, HttpRequest, HttpResponse };
|
use actix_web::{ web, HttpServer, HttpRequest, HttpResponse };
|
||||||
|
|
||||||
|
use error::*;
|
||||||
|
|
||||||
const DEFAULT_PORT: u16 = 8888;
|
const DEFAULT_PORT: u16 = 8888;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum GetStreamError {
|
|
||||||
ReqwestError(reqwest::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for GetStreamError {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(f, "Unknown error!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for GetStreamError {
|
|
||||||
fn cause(&self) -> Option<&dyn Error> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<reqwest::Error> for GetStreamError {
|
|
||||||
fn from(reqwest_error: reqwest::Error) -> Self {
|
|
||||||
GetStreamError::ReqwestError(reqwest_error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<GetStreamError> for actix_http::error::Error {
|
|
||||||
fn from(_: GetStreamError) -> Self { todo!() }
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ProxyStream {
|
struct ProxyStream {
|
||||||
inner: Box<dyn futures_core::Stream<Item = reqwest::Result<Bytes>> + Unpin>,
|
inner: Box<dyn futures_core::Stream<Item = reqwest::Result<Bytes>> + Unpin>,
|
||||||
@@ -60,6 +36,9 @@ impl futures_core::Stream for ProxyStream {
|
|||||||
|
|
||||||
async fn get_file(req: HttpRequest) -> HttpResponse {
|
async fn get_file(req: HttpRequest) -> HttpResponse {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
|
let query_string = req.query_string();
|
||||||
|
println!("Q: {}", query_string);
|
||||||
|
// TODO parse url
|
||||||
let resp = reqwest::get("http://example.com/").await.unwrap();
|
let resp = reqwest::get("http://example.com/").await.unwrap();
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
@@ -84,8 +63,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
information!("Listen at: {}:{}", listen_addr, listen_port);
|
information!("Listen at: {}:{}", listen_addr, listen_port);
|
||||||
|
|
||||||
HttpServer::new(||
|
HttpServer::new(||
|
||||||
actix_web::App::new()
|
actix_web::App::new().service(web::resource("/get_file").to(get_file))
|
||||||
.service(web::resource("/get_file").to(get_file))
|
|
||||||
).bind(format!("{}:{}", listen_addr, listen_port))?
|
).bind(format!("{}:{}", listen_addr, listen_port))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
|
|||||||
Reference in New Issue
Block a user