Using CSV not database.

master
Griffiths Lott 3 years ago
commit 59981e2636
  1. 3
      .env
  2. 1
      .gitignore
  3. 1473
      Cargo.lock
  4. 13
      Cargo.toml
  5. 52
      src/main.rs

@ -0,0 +1,3 @@
BIND_ADDRESS="127.0.0.1"
BIND_PORT="4220"
SAVE_PATH="/home/g/movie_reqs.csv"

1
.gitignore vendored

@ -0,0 +1 @@
/target

1473
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,13 @@
[package]
name = "movie_req_api"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
actix-web = "4"
serde = {version= "1.0.152", features=["derive"]}
chrono = "0.4.23"
csv = "1.2.0"

@ -0,0 +1,52 @@
use actix_web::{App, HttpResponse, HttpServer, Responder, web::{post,Json}};
use dotenv::var;
use serde::{Serialize,Deserialize};
use csv::Writer;
use std::fs::OpenOptions;
use chrono::Utc;
#[derive(Serialize,Deserialize)]
struct MovieRequest {
title: String,
description: String,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let bind_address = var("BIND_ADDRESS").expect("No bind address configured in .env!");
let bind_port = var("BIND_PORT").expect("No bind port configured in .env!")
.parse::<u16>().expect("Failed to convert bind port to u16");
HttpServer::new(move || {
App::new()
.route("/moviereq",post().to(moviereq))
})
.bind((bind_address, bind_port))?
.run()
.await
}
async fn moviereq(item: Json<MovieRequest>) -> impl Responder {
//TODO: implement a real database for movie reqs
let date_time_added = Utc::now();
let formatted_date_time_added = date_time_added.format("%Y-%m-%d:%H:%M").to_string();
let csv_file = var("SAVE_PATH").expect("No save path configured in .env!");
let file = match OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(csv_file) {
Ok(file) => file,
Err(_) => return HttpResponse::InternalServerError().body("Failed to add request to database"),
};
let mut csv_writer = Writer::from_writer(file);
let record = (formatted_date_time_added, &item.title, &item.description);
csv_writer.serialize(record).unwrap();
HttpResponse::Ok().body("Successfully added to the database")
}
Loading…
Cancel
Save