1
0
Fork 0

Add routes

This commit is contained in:
Alex Kotov 2023-01-29 00:34:55 +04:00
parent d1b3fe3885
commit 086c970d0a
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 45 additions and 1 deletions

View File

@ -1,4 +1,5 @@
mod config;
mod routes;
use config::Config;
@ -13,7 +14,9 @@ async fn main() -> std::io::Result<()> {
.init();
actix_web::HttpServer::new(|| {
actix_web::App::new().wrap(actix_web::middleware::Logger::default())
actix_web::App::new()
.wrap(actix_web::middleware::Logger::default())
.service(routes::languages::index)
})
.bind(config.bind())?
.run()

10
src/routes/languages.rs Normal file
View File

@ -0,0 +1,10 @@
use super::Result;
use actix_web::{get, HttpResponse};
#[get("/languages")]
async fn index() -> Result {
Ok(HttpResponse::Ok()
.content_type("application/json; charset=utf-8")
.body("{\"hello\":\"Hello, World!\"}"))
}

31
src/routes/mod.rs Normal file
View File

@ -0,0 +1,31 @@
pub mod languages;
use std::fmt::{self, Display};
use actix_web::{http::StatusCode, HttpResponse};
pub type Result = std::result::Result<HttpResponse, Error>;
#[derive(Debug)]
pub enum Error {}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Error.")
}
}
impl actix_web::error::ResponseError for Error {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn error_response(&self) -> HttpResponse {
actix_web::HttpResponseBuilder::new(self.status_code())
.insert_header((
actix_web::http::header::CONTENT_TYPE,
"application/json; charset=utf-8",
))
.body("{\"error\":\"Error!\"")
}
}