Seperate into multiple files. Add configuration options
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
pub application_port: u16
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
}
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
// Initialise configuration reader
|
||||
let mut settings = config::Config::default();
|
||||
|
||||
// Add config from `configuration` file (yaml, json, etc.)
|
||||
settings.merge(config::File::with_name("configuration"))?;
|
||||
|
||||
// Try convert into Settings type
|
||||
settings.try_into()
|
||||
}
|
||||
+3
-28
@@ -1,28 +1,3 @@
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use actix_web::dev::Server;
|
||||
use std::net::TcpListener;
|
||||
|
||||
async fn health_check() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct FormData {
|
||||
email: String,
|
||||
name: String
|
||||
}
|
||||
|
||||
async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||
let server = HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/health_check", web::get().to(health_check))
|
||||
.route("/subscriptions", web::post().to(subscribe))
|
||||
})
|
||||
.listen(listener)?
|
||||
.run();
|
||||
Ok(server)
|
||||
}
|
||||
pub mod configuration;
|
||||
pub mod routes;
|
||||
pub mod startup;
|
||||
+7
-3
@@ -1,10 +1,14 @@
|
||||
use mail_app::run;
|
||||
use mail_app::startup::run;
|
||||
use mail_app::configuration::get_configuration;
|
||||
use std::net::TcpListener;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// Attempt to read from config
|
||||
let configuration = get_configuration().expect("Failed to read configuration data.");
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to 8000");
|
||||
|
||||
// Take port from settings file
|
||||
let address = format!("127.0.0.1:{}", configuration.application_port);
|
||||
let listener = TcpListener::bind(address)?;
|
||||
run(listener)?.await
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
pub async fn health_check() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
mod health_check;
|
||||
mod subscriptions;
|
||||
|
||||
pub use health_check::*;
|
||||
pub use subscriptions::*;
|
||||
@@ -0,0 +1,11 @@
|
||||
use actix_web::{web, HttpResponse};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct FormData {
|
||||
email: String,
|
||||
name: String
|
||||
}
|
||||
|
||||
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_web::dev::Server;
|
||||
use std::net::TcpListener;
|
||||
|
||||
use crate::routes;
|
||||
|
||||
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||
let server = HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/health_check", web::get().to(routes::health_check))
|
||||
.route("/subscriptions", web::post().to(routes::subscribe))
|
||||
})
|
||||
.listen(listener)?
|
||||
.run();
|
||||
Ok(server)
|
||||
}
|
||||
Reference in New Issue
Block a user