Skip to content

Commit

Permalink
feat(#9): clean
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 1, 2024
1 parent ee64f37 commit 7775a99
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
45 changes: 31 additions & 14 deletions src/home.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use std::collections::HashMap;
use std::error::Error;

use axum::http::StatusCode;
use axum::Json;
use axum::response::IntoResponse;
use axum::Json;
use tokio::fs;

use crate::rs_err::RsErr;
Expand All @@ -14,24 +35,20 @@ pub async fn home() -> impl IntoResponse {
let objs = serde_json::from_str(&content).unwrap();
Json(objs)
}
Err(e) => {
Json(
serde_json::to_value(
RsErr {
status: StatusCode::INTERNAL_SERVER_ERROR.to_string(),
message: "Failed to fetch home (/) endpoints".to_owned(),
trace: e.to_string(),
}
).unwrap()
)
}
Err(e) => Json(
serde_json::to_value(RsErr {
status: StatusCode::INTERNAL_SERVER_ERROR.to_string(),
message: "Failed to fetch home (/) endpoints".to_owned(),
trace: e.to_string(),
})
.unwrap(),
),
}
}

async fn json(path: &str) -> Result<String, Box<dyn Error>> {
let content = fs::read_to_string(path).await.unwrap();
let mut data: HashMap<String, String> = serde_json::from_str(&content)
.unwrap();
let mut data: HashMap<String, String> = serde_json::from_str(&content).unwrap();
for (_, value) in &mut data {
*value = value.replace("{port}", "3000");
}
Expand Down
11 changes: 3 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use axum::Router;
use axum::routing::get;
use axum::Router;

mod home;
mod rs_err;

#[tokio::main]
async fn main() {
let app = Router::new()
.route(
"/",
get(home::home),
);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await
.unwrap();
let app = Router::new().route("/", get(home::home));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}

0 comments on commit 7775a99

Please sign in to comment.