mirror of
https://github.com/creating2morrow/neveko.git
synced 2025-01-03 17:39:55 +00:00
return json on 402 404 and 500 status
This commit is contained in:
parent
01cbc0181d
commit
a7135a53f7
3 changed files with 39 additions and 2 deletions
|
@ -89,7 +89,7 @@ pub async fn start() {
|
||||||
find_tunnels().await;
|
find_tunnels().await;
|
||||||
{
|
{
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let tick: std::sync::mpsc::Receiver<()> = schedule_recv::periodic_ms(60000);
|
let tick: std::sync::mpsc::Receiver<()> = schedule_recv::periodic_ms(600000);
|
||||||
loop {
|
loop {
|
||||||
tick.recv().unwrap();
|
tick.recv().unwrap();
|
||||||
check_connection().await;
|
check_connection().await;
|
||||||
|
|
|
@ -879,3 +879,18 @@ impl Default for Jwp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For handling 402, 404 and 500 error responses
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(crate = "rocket::serde")]
|
||||||
|
pub struct ErrorResponse {
|
||||||
|
pub error: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ErrorResponse {
|
||||||
|
fn default() -> Self {
|
||||||
|
ErrorResponse {
|
||||||
|
error: utils::empty_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -1,10 +1,31 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
use rocket::response::status::Custom;
|
||||||
|
|
||||||
use nevmes::*;
|
use nevmes::*;
|
||||||
use nevmes_core::*;
|
use nevmes_core::*;
|
||||||
|
|
||||||
// The only changes in here should be mounting new controller methods
|
#[catch(402)]
|
||||||
|
fn payment_required() -> Custom<Json<reqres::ErrorResponse>> {
|
||||||
|
Custom(Status::PaymentRequired,
|
||||||
|
Json(reqres::ErrorResponse { error: String::from("Payment required") }))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[catch(404)]
|
||||||
|
fn not_found() -> Custom<Json<reqres::ErrorResponse>> {
|
||||||
|
Custom(Status::NotFound,
|
||||||
|
Json(reqres::ErrorResponse { error: String::from("Resource does not exist") }))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[catch(500)]
|
||||||
|
fn internal_error() -> Custom<Json<reqres::ErrorResponse>> {
|
||||||
|
Custom(Status::InternalServerError,
|
||||||
|
Json(reqres::ErrorResponse { error: String::from("Internal server error") }))
|
||||||
|
}
|
||||||
|
|
||||||
|
// The only changes below here should be mounting new controller methods
|
||||||
#[launch]
|
#[launch]
|
||||||
async fn rocket() -> _ {
|
async fn rocket() -> _ {
|
||||||
let config = rocket::Config {
|
let config = rocket::Config {
|
||||||
|
@ -16,6 +37,7 @@ async fn rocket() -> _ {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
utils::start_up().await;
|
utils::start_up().await;
|
||||||
rocket::custom(&config)
|
rocket::custom(&config)
|
||||||
|
.register("/", catchers![internal_error, not_found, payment_required])
|
||||||
.mount("/invoice", routes![controller::gen_invoice])
|
.mount("/invoice", routes![controller::gen_invoice])
|
||||||
.mount("/message/rx", routes![controller::rx_message])
|
.mount("/message/rx", routes![controller::rx_message])
|
||||||
.mount("/prove", routes![controller::gen_jwp])
|
.mount("/prove", routes![controller::gen_jwp])
|
||||||
|
|
Loading…
Reference in a new issue