Add routes
This commit is contained in:
parent
d1b3fe3885
commit
086c970d0a
|
@ -1,4 +1,5 @@
|
||||||
mod config;
|
mod config;
|
||||||
|
mod routes;
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
|
@ -13,7 +14,9 @@ async fn main() -> std::io::Result<()> {
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
actix_web::HttpServer::new(|| {
|
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())?
|
.bind(config.bind())?
|
||||||
.run()
|
.run()
|
||||||
|
|
|
@ -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!\"}"))
|
||||||
|
}
|
|
@ -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!\"")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue