First import
This commit is contained in:
48
template/api/src/configuration.rs
Normal file
48
template/api/src/configuration.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use config::{Config, ConfigError, Environment, File};
|
||||
use serde::Deserialize;
|
||||
use std::env;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Settings {
|
||||
pub application: ApplicationSettings,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ApplicationSettings {
|
||||
pub port: u16,
|
||||
pub log_directive: String,
|
||||
pub front_base_url: String,
|
||||
pub api_path: String,
|
||||
pub static_path: Option<String>,
|
||||
pub static_dir: Option<String>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new_from_file(file: Option<String>) -> Result<Self, ConfigError> {
|
||||
let config_file_required = file.is_some();
|
||||
let config_path = env::var("CONFIG_PATH").unwrap_or_else(|_| "config".into());
|
||||
let config_file = file.unwrap_or_else(|| {
|
||||
env::var("CONFIG_FILE").unwrap_or_else(|_| format!("{}/dev", &config_path))
|
||||
});
|
||||
|
||||
let default_config_file = format!("{}/default", config_path);
|
||||
let local_config_file = format!("{}/local", config_path);
|
||||
println!(
|
||||
"Trying to load {:?} config files",
|
||||
vec![&default_config_file, &local_config_file, &config_file]
|
||||
);
|
||||
|
||||
let config = Config::builder()
|
||||
.add_source(File::with_name(&default_config_file))
|
||||
.add_source(File::with_name(&local_config_file).required(false))
|
||||
.add_source(File::with_name(&config_file).required(config_file_required))
|
||||
.add_source(Environment::with_prefix("{{crate_name}}"))
|
||||
.build()?;
|
||||
|
||||
config.try_deserialize()
|
||||
}
|
||||
|
||||
pub fn new() -> Result<Self, ConfigError> {
|
||||
Settings::new_from_file(None)
|
||||
}
|
||||
}
|
||||
67
template/api/src/lib.rs
Normal file
67
template/api/src/lib.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use actix_files as fs;
|
||||
use actix_web::{dev::Server, http, middleware, web, App, HttpServer};
|
||||
use configuration::Settings;
|
||||
use core::time::Duration;
|
||||
use std::net::TcpListener;
|
||||
use tracing::info;
|
||||
use tracing_actix_web::TracingLogger;
|
||||
|
||||
pub mod configuration;
|
||||
pub mod {{crate_name}};
|
||||
pub mod observability;
|
||||
pub mod routes;
|
||||
|
||||
pub fn run(listener: TcpListener, settings: &Settings) -> Result<Server, std::io::Error> {
|
||||
let api_path = settings.application.api_path.clone();
|
||||
let front_base_url = settings.application.front_base_url.clone();
|
||||
let static_path = settings.application.static_path.clone();
|
||||
let static_dir = settings
|
||||
.application
|
||||
.static_dir
|
||||
.clone()
|
||||
.unwrap_or_else(|| ".".to_string());
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
info!(
|
||||
"Mounting API on {}",
|
||||
if api_path.is_empty() { "/" } else { &api_path }
|
||||
);
|
||||
let api_scope = web::scope(&api_path)
|
||||
.wrap(
|
||||
middleware::DefaultHeaders::new()
|
||||
.add(("Access-Control-Allow-Origin", front_base_url.as_bytes()))
|
||||
.add((
|
||||
"Access-Control-Allow-Methods",
|
||||
"POST, GET, OPTIONS".as_bytes(),
|
||||
))
|
||||
.add(("Access-Control-Allow-Headers", "content-type".as_bytes())),
|
||||
)
|
||||
.route("/hello", web::get().to(routes::hello))
|
||||
.route(
|
||||
"/TODO*",
|
||||
web::method(http::Method::OPTIONS).to(routes::option_wildcard),
|
||||
);
|
||||
|
||||
let mut app = App::new()
|
||||
.wrap(TracingLogger::default())
|
||||
.wrap(middleware::Compress::default())
|
||||
.route("/ping", web::get().to(routes::ping))
|
||||
.service(api_scope);
|
||||
if let Some(path) = &static_path {
|
||||
info!(
|
||||
"Mounting static files on {}",
|
||||
if path.is_empty() { "/" } else { &path }
|
||||
);
|
||||
let static_scope = fs::Files::new(path, &static_dir)
|
||||
.use_last_modified(true)
|
||||
.index_file("index.html");
|
||||
app = app.service(static_scope);
|
||||
}
|
||||
app
|
||||
})
|
||||
.keep_alive(http::KeepAlive::Timeout(Duration::from_secs(60)))
|
||||
.shutdown_timeout(60)
|
||||
.listen(listener)?;
|
||||
|
||||
Ok(server.run())
|
||||
}
|
||||
15
template/api/src/main.rs
Normal file
15
template/api/src/main.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use {{crate_name}}_api::configuration::Settings;
|
||||
use {{crate_name}}_api::observability::{get_subscriber, init_subscriber};
|
||||
use {{crate_name}}_api::run;
|
||||
use std::net::TcpListener;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let settings = Settings::new().expect("Cannot load {{project-name | capitalize}} configuration");
|
||||
let subscriber = get_subscriber(&settings.application.log_directive);
|
||||
init_subscriber(subscriber);
|
||||
|
||||
let listener = TcpListener::bind(format!("0.0.0.0:{}", settings.application.port))
|
||||
.expect("Failed to bind port");
|
||||
run(listener, &settings)?.await
|
||||
}
|
||||
23
template/api/src/observability.rs
Normal file
23
template/api/src/observability.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use tracing::{subscriber::set_global_default, Subscriber};
|
||||
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
||||
use tracing_log::LogTracer;
|
||||
use tracing_subscriber::fmt::TestWriter;
|
||||
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
|
||||
|
||||
pub fn get_subscriber(env_filter_str: &str) -> impl Subscriber + Send + Sync {
|
||||
let formatting_layer =
|
||||
BunyanFormattingLayer::new("{{project-name}}-api".into(), TestWriter::new);
|
||||
|
||||
let env_filter =
|
||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter_str));
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(env_filter)
|
||||
.with(JsonStorageLayer)
|
||||
.with(formatting_layer)
|
||||
}
|
||||
|
||||
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
|
||||
LogTracer::init().expect("Failed to set logger");
|
||||
set_global_default(subscriber).expect("Failed to set subscriber");
|
||||
}
|
||||
28
template/api/src/routes/default.rs
Normal file
28
template/api/src/routes/default.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::{{crate_name}};
|
||||
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
||||
use anyhow::Context;
|
||||
use ::{{crate_name}}::Object;
|
||||
|
||||
impl ResponseError for {{crate_name}}::{{project-name | capitalize}}Error {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
{{crate_name}}::{{project-name | capitalize}}Error::InvalidDataError { .. } => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
{{crate_name}}::{{project-name | capitalize}}Error::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug")]
|
||||
pub async fn hello() -> Result<HttpResponse, {{crate_name}}::{{project-name | capitalize}}Error> {
|
||||
let object: Object = {{crate_name}}::build_object()?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(serde_json::to_string(&object).context("Cannot serialize object")?))
|
||||
}
|
||||
|
||||
|
||||
#[tracing::instrument(level = "debug")]
|
||||
pub async fn option_wildcard() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
5
template/api/src/routes/health_check.rs
Normal file
5
template/api/src/routes/health_check.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
pub async fn ping() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
5
template/api/src/routes/mod.rs
Normal file
5
template/api/src/routes/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod default;
|
||||
mod health_check;
|
||||
|
||||
pub use default::*;
|
||||
pub use health_check::*;
|
||||
40
template/api/src/{{project-name}}/api.rs
Normal file
40
template/api/src/{{project-name}}/api.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use chrono::Utc;
|
||||
use serde_json;
|
||||
use {{crate_name}}::Object;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn error_chain_fmt(
|
||||
e: &impl std::error::Error,
|
||||
f: &mut std::fmt::Formatter<'_>,
|
||||
) -> std::fmt::Result {
|
||||
writeln!(f, "{}\n", e)?;
|
||||
let mut current = e.source();
|
||||
while let Some(cause) = current {
|
||||
writeln!(f, "Caused by:\n\t{}", cause)?;
|
||||
current = cause.source();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for {{project-name | capitalize}}Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
error_chain_fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error)]
|
||||
pub enum {{project-name | capitalize}}Error {
|
||||
#[error("Invalid {{project-name | capitalize}} data")]
|
||||
InvalidDataError(#[from] serde_json::Error),
|
||||
#[error(transparent)]
|
||||
UnexpectedError(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug")]
|
||||
pub fn build_object() -> Result<Object, {{project-name | capitalize}}Error> {
|
||||
Ok(Object {
|
||||
name: "test".to_string(),
|
||||
id: Uuid::new_v4(),
|
||||
modified: Utc::now(),
|
||||
})
|
||||
}
|
||||
3
template/api/src/{{project-name}}/mod.rs
Normal file
3
template/api/src/{{project-name}}/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod api;
|
||||
|
||||
pub use api::*;
|
||||
Reference in New Issue
Block a user