Added configuration file
This commit is contained in:
@@ -7,5 +7,6 @@ edition = "2024"
|
|||||||
actix-web = {version = "4.11.0"}
|
actix-web = {version = "4.11.0"}
|
||||||
serde = "1.0.228"
|
serde = "1.0.228"
|
||||||
serde_json = "1.0.145"
|
serde_json = "1.0.145"
|
||||||
|
toml = "0.9.7"
|
||||||
sqlx = {version = "0.8.6", features = ["postgres"]}
|
sqlx = {version = "0.8.6", features = ["postgres"]}
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
5
config.toml
Normal file
5
config.toml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
[application]
|
||||||
|
# Change with actual location of assets
|
||||||
|
assets=""
|
||||||
|
# Set bind address
|
||||||
|
bind="127.0.0.1:8080"
|
||||||
56
src/main.rs
56
src/main.rs
@@ -1,25 +1,57 @@
|
|||||||
mod html;
|
mod html;
|
||||||
mod pages;
|
|
||||||
mod middleware;
|
mod middleware;
|
||||||
|
mod pages;
|
||||||
|
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{App, HttpServer, web};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use toml::from_slice;
|
||||||
|
|
||||||
use pages::{files::file, index, projects::{projects, project}};
|
|
||||||
use middleware::MimeType;
|
use middleware::MimeType;
|
||||||
|
use pages::{
|
||||||
|
files::file,
|
||||||
|
index,
|
||||||
|
projects::{project, projects},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Config {
|
||||||
|
server: ServerConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ServerConfig {
|
||||||
|
assets: String,
|
||||||
|
bind: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AppState {
|
||||||
|
assets: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let bind_address = "0.0.0.0:8080";
|
let mut config_file =
|
||||||
if let Ok(server) =
|
File::open("/etc/aindustries-be/config.toml").expect("Could not open config file");
|
||||||
HttpServer::new(|| App::new()
|
let mut buf = vec![];
|
||||||
.service(file)
|
config_file
|
||||||
.service(web::scope("")
|
.read_to_end(&mut buf)
|
||||||
.wrap(MimeType::new("text/html".to_string()))
|
.expect("Could not read config file");
|
||||||
|
let config: Config = from_slice(&buf).expect("Could not parse config");
|
||||||
|
let bind_address = config.server.bind;
|
||||||
|
let assets = config.server.assets;
|
||||||
|
let data = web::Data::new(AppState { assets });
|
||||||
|
if let Ok(server) = HttpServer::new(move || {
|
||||||
|
App::new().app_data(data.clone()).service(file).service(
|
||||||
|
web::scope("")
|
||||||
|
.wrap(MimeType::new("text/html"))
|
||||||
.service(index)
|
.service(index)
|
||||||
.service(projects)
|
.service(projects)
|
||||||
.service(project)
|
.service(project),
|
||||||
)
|
)
|
||||||
).bind(bind_address)
|
})
|
||||||
|
.bind(bind_address)
|
||||||
{
|
{
|
||||||
match server.run().await {
|
match server.run().await {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
|
|||||||
Reference in New Issue
Block a user