Format
All checks were successful
Build Project / test (push) Successful in 5m38s

This commit is contained in:
2025-01-19 16:14:22 +00:00
parent b336b6b926
commit 92159bc276
3 changed files with 77 additions and 36 deletions

View File

@@ -16,7 +16,7 @@ mod util;
struct AppState {
db: Database,
firmwares_path: PathBuf,
hostname: String
hostname: String,
}
#[post("/telemetry/{device_id}")]
@@ -128,14 +128,20 @@ async fn get_devices(data: web::Data<AppState>) -> impl Responder {
// Upload Firmware file
#[put("/firmware/{device}/{config}/{version}")]
async fn upload_firmware(data: web::Data<AppState>, path: web::Path<(String, String, String)>, body: web::Bytes) -> impl Responder {
async fn upload_firmware(
data: web::Data<AppState>,
path: web::Path<(String, String, String)>,
body: web::Bytes,
) -> impl Responder {
let (device, config, version) = path.into_inner();
let version = version.replace(".", "-");
let firmware_root_path = &data.firmwares_path;
let firmware_folder = firmware_root_path.join(&device);
let firmware_path = firmware_folder.join(format!("firmware_{config}_{version}")).with_extension("bin");
let firmware_path = firmware_folder
.join(format!("firmware_{config}_{version}"))
.with_extension("bin");
if firmware_path.is_file() {
warn!("Firmware with product: {device}, config: {config} and version: {version} at path {firmware_path:?} already exists, cant upload");
@@ -143,12 +149,15 @@ async fn upload_firmware(data: web::Data<AppState>, path: web::Path<(String, Str
}
info!("Uploading firmware with product: {device}, config: {config} and version: {version} to {firmware_path:?}");
fs::create_dir_all(&firmware_folder).unwrap();
let x = tokio::fs::write(&firmware_path, &body).await;
debug!("{x:?}");
HttpResponse::Ok().body(format!("Firmware version {} uploaded successfully", version))
HttpResponse::Ok().body(format!(
"Firmware version {} uploaded successfully",
version
))
}
#[get("/firmware/{device}")]
@@ -157,18 +166,23 @@ async fn get_firmware_json(data: web::Data<AppState>, path: web::Path<String>) -
let fw_path = &data.firmwares_path.join(device);
if fw_path.is_dir() {
match get_files(fw_path, &data.hostname) {
Ok(cfg) => HttpResponse::Ok().json(OTAConfigurationList{configurations: cfg} ),
Err(e) => HttpResponse::InternalServerError().body(e.to_string())
Ok(cfg) => HttpResponse::Ok().json(OTAConfigurationList {
configurations: cfg,
}),
Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
}
} else {
HttpResponse::Ok().json(OTAConfigurationList{configurations: vec![]} )
HttpResponse::Ok().json(OTAConfigurationList {
configurations: vec![],
})
}
}
#[get("/firmware/{product}/{config}/{version}.bin")]
async fn serve_firmware(path: web::Path<(String, String, String)>, data: web::Data<AppState>) -> impl Responder {
async fn serve_firmware(
path: web::Path<(String, String, String)>,
data: web::Data<AppState>,
) -> impl Responder {
let (product, config, version) = path.into_inner();
let version = version.replace(".", "-").replace("_", "-");
let fw_root_path = &data.firmwares_path;
@@ -198,7 +212,6 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
info!("Starting");
let db_url = match env::var("DATABASE_URL") {
Ok(url) => url,
Err(e) => {
@@ -213,7 +226,11 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(AppState { db: db.clone(), firmwares_path: PathBuf::from("./fw"), hostname: "127.0.0.1:8282".to_string() }))
.app_data(web::Data::new(AppState {
db: db.clone(),
firmwares_path: PathBuf::from("./fw"),
hostname: "127.0.0.1:8282".to_string(),
}))
.app_data(web::PayloadConfig::new(256 * 1024 * 1024)) //256MB
.service(receive_telemetry)
.service(get_telemetry)