refactor(config)!: Setup for expanding lua config return
Moves the application config out of automation_lib and sets up the config return type for further expansion.
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
use std::net::{Ipv4Addr, SocketAddr};
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use lua_typed::Typed;
|
use lua_typed::Typed;
|
||||||
@@ -30,29 +29,6 @@ impl From<MqttConfig> for MqttOptions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct FulfillmentConfig {
|
|
||||||
pub openid_url: String,
|
|
||||||
#[serde(default = "default_fulfillment_ip")]
|
|
||||||
pub ip: Ipv4Addr,
|
|
||||||
#[serde(default = "default_fulfillment_port")]
|
|
||||||
pub port: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<FulfillmentConfig> for SocketAddr {
|
|
||||||
fn from(fulfillment: FulfillmentConfig) -> Self {
|
|
||||||
(fulfillment.ip, fulfillment.port).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_fulfillment_ip() -> Ipv4Addr {
|
|
||||||
[0, 0, 0, 0].into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_fulfillment_port() -> u16 {
|
|
||||||
7878
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Typed)]
|
#[derive(Debug, Clone, Deserialize, Typed)]
|
||||||
pub struct InfoConfig {
|
pub struct InfoConfig {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|||||||
10
config.lua
10
config.lua
@@ -21,10 +21,6 @@ local function mqtt_automation(topic)
|
|||||||
return "automation/" .. topic
|
return "automation/" .. topic
|
||||||
end
|
end
|
||||||
|
|
||||||
local fulfillment = {
|
|
||||||
openid_url = "https://login.huizinga.dev/api/oidc",
|
|
||||||
}
|
|
||||||
|
|
||||||
local mqtt_client = require("automation:mqtt").new(device_manager, {
|
local mqtt_client = require("automation:mqtt").new(device_manager, {
|
||||||
host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto",
|
host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto",
|
||||||
port = 8883,
|
port = 8883,
|
||||||
@@ -748,4 +744,8 @@ device_manager:schedule("0 0 20 * * *", function()
|
|||||||
bedroom_air_filter:set_on(false)
|
bedroom_air_filter:set_on(false)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return fulfillment
|
return {
|
||||||
|
fulfillment = {
|
||||||
|
openid_url = "https://login.huizinga.dev/api/oidc",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::net::{Ipv4Addr, SocketAddr};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Setup {
|
||||||
#[serde(default = "default_entrypoint")]
|
#[serde(default = "default_entrypoint")]
|
||||||
pub entrypoint: String,
|
pub entrypoint: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@@ -15,3 +16,30 @@ pub struct Config {
|
|||||||
fn default_entrypoint() -> String {
|
fn default_entrypoint() -> String {
|
||||||
"./config.lua".into()
|
"./config.lua".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct FulfillmentConfig {
|
||||||
|
pub openid_url: String,
|
||||||
|
#[serde(default = "default_fulfillment_ip")]
|
||||||
|
pub ip: Ipv4Addr,
|
||||||
|
#[serde(default = "default_fulfillment_port")]
|
||||||
|
pub port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Config {
|
||||||
|
pub fulfillment: FulfillmentConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FulfillmentConfig> for SocketAddr {
|
||||||
|
fn from(fulfillment: FulfillmentConfig) -> Self {
|
||||||
|
(fulfillment.ip, fulfillment.port).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn default_fulfillment_ip() -> Ipv4Addr {
|
||||||
|
[0, 0, 0, 0].into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_fulfillment_port() -> u16 {
|
||||||
|
7878
|
||||||
|
}
|
||||||
|
|||||||
19
src/main.rs
19
src/main.rs
@@ -9,19 +9,18 @@ use std::path::Path;
|
|||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
use ::config::{Environment, File};
|
use ::config::{Environment, File};
|
||||||
use automation_lib::config::FulfillmentConfig;
|
|
||||||
use automation_lib::device_manager::DeviceManager;
|
use automation_lib::device_manager::DeviceManager;
|
||||||
use axum::extract::{FromRef, State};
|
use axum::extract::{FromRef, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::routing::post;
|
use axum::routing::post;
|
||||||
use axum::{Json, Router};
|
use axum::{Json, Router};
|
||||||
use config::Config;
|
|
||||||
use google_home::{GoogleHome, Request, Response};
|
use google_home::{GoogleHome, Request, Response};
|
||||||
use mlua::LuaSerdeExt;
|
use mlua::LuaSerdeExt;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
use web::{ApiError, User};
|
use web::{ApiError, User};
|
||||||
|
|
||||||
|
use crate::config::{Config, Setup};
|
||||||
use crate::secret::EnvironmentSecretFile;
|
use crate::secret::EnvironmentSecretFile;
|
||||||
use crate::version::VERSION;
|
use crate::version::VERSION;
|
||||||
|
|
||||||
@@ -76,7 +75,7 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
info!(version = VERSION, "automation_rs");
|
info!(version = VERSION, "automation_rs");
|
||||||
|
|
||||||
let config: Config = ::config::Config::builder()
|
let setup: Setup = ::config::Config::builder()
|
||||||
.add_source(
|
.add_source(
|
||||||
File::with_name(&format!("{}.toml", std::env!("CARGO_PKG_NAME"))).required(false),
|
File::with_name(&format!("{}.toml", std::env!("CARGO_PKG_NAME"))).required(false),
|
||||||
)
|
)
|
||||||
@@ -138,12 +137,12 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
lua.register_module("automation:device_manager", device_manager.clone())?;
|
lua.register_module("automation:device_manager", device_manager.clone())?;
|
||||||
|
|
||||||
lua.register_module("automation:variables", lua.to_value(&config.variables)?)?;
|
lua.register_module("automation:variables", lua.to_value(&setup.variables)?)?;
|
||||||
lua.register_module("automation:secrets", lua.to_value(&config.secrets)?)?;
|
lua.register_module("automation:secrets", lua.to_value(&setup.secrets)?)?;
|
||||||
|
|
||||||
let entrypoint = Path::new(&config.entrypoint);
|
let entrypoint = Path::new(&setup.entrypoint);
|
||||||
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
let config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
||||||
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
let config: Config = lua.from_value(config)?;
|
||||||
|
|
||||||
// Create google home fulfillment route
|
// Create google home fulfillment route
|
||||||
let fulfillment = Router::new().route("/google_home", post(fulfillment));
|
let fulfillment = Router::new().route("/google_home", post(fulfillment));
|
||||||
@@ -152,12 +151,12 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.nest("/fulfillment", fulfillment)
|
.nest("/fulfillment", fulfillment)
|
||||||
.with_state(AppState {
|
.with_state(AppState {
|
||||||
openid_url: fulfillment_config.openid_url.clone(),
|
openid_url: config.fulfillment.openid_url.clone(),
|
||||||
device_manager,
|
device_manager,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the web server
|
// Start the web server
|
||||||
let addr: SocketAddr = fulfillment_config.into();
|
let addr: SocketAddr = config.fulfillment.into();
|
||||||
info!("Server started on http://{addr}");
|
info!("Server started on http://{addr}");
|
||||||
let listener = TcpListener::bind(addr).await?;
|
let listener = TcpListener::bind(addr).await?;
|
||||||
axum::serve(listener, app).await?;
|
axum::serve(listener, app).await?;
|
||||||
|
|||||||
Reference in New Issue
Block a user