Added configuration file
This commit is contained in:
@@ -7,5 +7,6 @@ edition = "2024"
|
||||
actix-web = {version = "4.11.0"}
|
||||
serde = "1.0.228"
|
||||
serde_json = "1.0.145"
|
||||
toml = "0.9.7"
|
||||
sqlx = {version = "0.8.6", features = ["postgres"]}
|
||||
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 pages;
|
||||
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 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]
|
||||
async fn main() {
|
||||
let bind_address = "0.0.0.0:8080";
|
||||
if let Ok(server) =
|
||||
HttpServer::new(|| App::new()
|
||||
.service(file)
|
||||
.service(web::scope("")
|
||||
.wrap(MimeType::new("text/html".to_string()))
|
||||
let mut config_file =
|
||||
File::open("/etc/aindustries-be/config.toml").expect("Could not open config file");
|
||||
let mut buf = vec![];
|
||||
config_file
|
||||
.read_to_end(&mut buf)
|
||||
.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(projects)
|
||||
.service(project)
|
||||
)
|
||||
).bind(bind_address)
|
||||
.service(project),
|
||||
)
|
||||
})
|
||||
.bind(bind_address)
|
||||
{
|
||||
match server.run().await {
|
||||
Ok(_) => {}
|
||||
|
||||
Reference in New Issue
Block a user