diff --git a/src/main.rs b/src/main.rs index 85e5e06..71aca2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() diff --git a/src/routes/languages.rs b/src/routes/languages.rs new file mode 100644 index 0000000..0f0e694 --- /dev/null +++ b/src/routes/languages.rs @@ -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!\"}")) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..fd1392f --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1,31 @@ +pub mod languages; + +use std::fmt::{self, Display}; + +use actix_web::{http::StatusCode, HttpResponse}; + +pub type Result = std::result::Result; + +#[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!\"") + } +}