mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-07-17 12:48:04 +00:00
Create server
This commit is contained in:
@ -87,6 +87,11 @@ impl Config {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the address this server listen to
|
||||
pub fn server_listen_address(&self) -> String {
|
||||
format!("{}:{}", self.listen_address, self.port)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an instance of the configuration
|
||||
|
30
src/data/http_error.rs
Normal file
30
src/data/http_error.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use serde::Serialize;
|
||||
|
||||
/// HTTP request error
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct HttpError {
|
||||
pub code: u16,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl HttpError {
|
||||
|
||||
/// Generate a 404 error
|
||||
pub fn not_found(message: &str) -> HttpError {
|
||||
HttpError {
|
||||
code: 404,
|
||||
message: message.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a 500 error
|
||||
pub fn internal_error(message: &str) -> HttpError {
|
||||
HttpError {
|
||||
code: 500,
|
||||
message: message.to_string()
|
||||
}
|
||||
}
|
||||
}
|
56
src/data/http_request_handler.rs
Normal file
56
src/data/http_request_handler.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use actix_web::{web, HttpRequest, HttpResponse};
|
||||
use crate::controllers::routes::RequestResult;
|
||||
use crate::data::http_error::HttpError;
|
||||
use std::convert::TryFrom;
|
||||
use std::error::Error;
|
||||
use serde::Serialize;
|
||||
|
||||
/// Http request handler
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct SuccessMessage {
|
||||
success: String
|
||||
}
|
||||
|
||||
pub struct HttpRequestHandler {
|
||||
request: web::HttpRequest,
|
||||
response: Option<web::HttpResponse>
|
||||
}
|
||||
|
||||
impl HttpRequestHandler {
|
||||
|
||||
/// Construct a new request handler
|
||||
pub fn new(req: HttpRequest) -> HttpRequestHandler {
|
||||
HttpRequestHandler {
|
||||
request: req,
|
||||
response: None
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a response has been set for this request
|
||||
pub fn has_response(&self) -> bool {
|
||||
self.response.is_some()
|
||||
}
|
||||
|
||||
/// Take the response from this struct
|
||||
pub fn response(self) -> HttpResponse {
|
||||
self.response.unwrap()
|
||||
}
|
||||
|
||||
/// Success message
|
||||
pub fn success(&mut self, message: &str) -> RequestResult {
|
||||
self.response = Some(HttpResponse::Ok().json(SuccessMessage {
|
||||
success: message.to_string()
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Internal error message
|
||||
pub fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult {
|
||||
self.response = Some(HttpResponse::InternalServerError().json(
|
||||
HttpError::internal_error("Internal server error.")));
|
||||
Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap())
|
||||
}
|
||||
}
|
@ -1 +1,3 @@
|
||||
pub mod config;
|
||||
pub mod config;
|
||||
pub mod http_error;
|
||||
pub mod http_request_handler;
|
Reference in New Issue
Block a user