Skip to content

Commit

Permalink
documented code
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkrishnads committed Jul 28, 2024
1 parent c887a04 commit b3191e0
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 9 deletions.
6 changes: 6 additions & 0 deletions scripts/builder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
This is a script that takes in the html, css & js to generate a single html string.
It adds the CSS to a <style></style> tag, the JS to a <script type="module"></script> tag.
It then adds the links to the specified elements, hides the unselected ones, and returns the final string.
*/

const fs = require('fs');
const { JSDOM } = require('jsdom');

Expand Down
4 changes: 2 additions & 2 deletions scripts/screenshot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
During initial setup, run bun run puppeteer browsers install chrome,
and copy the executable path to line 8
This script takes in as input a file that contains a HTML string, renders it in a browser instance, and takes two screenshots in different sizes.
During initial setup, run bun run puppeteer browsers install chrome, and copy the executable path to line 8
*/
const puppeteer = require('puppeteer');

Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ impl Config {
}
}

// Right now, this seems like the best way to manage the upload of templates from github
// This function is very expensive, so a better method would be to write a dedicated endpoint for uploading templates from the site,
// and adding it to the database along with it. But that's for another day.
pub async fn monitor_templates_directory(app_state: Arc<AppState>) {
let templates_dir = format!("{}/.zitefy/templates", env::var("HOME").unwrap());

Expand Down
5 changes: 5 additions & 0 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// In this file, the logic should have existed in models/user.rs
// in the heat of the moment, I've just gotten this to work.

use actix_files::NamedFile;
use actix_multipart::Multipart;
use actix_web::http::Error;
Expand Down Expand Up @@ -236,6 +239,8 @@ async fn edit(
) -> impl Responder {
let users: Collection<User> = app_state.db.collection("users");

// there might be a way to refactor this better. this line repeats everywhere
// get_user_id_from_token() was refactored, but i still think there's more room.
let user_id = match get_user_id_from_token(&req, &app_state).await {
Ok(id) => id,
Err(response) => return response,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use handlers::{user::LoginResponse, proxy_anthropic};
use models::user::{EditData, LoginData, SignupData, UserDataResponse};
use services::tempfiles::TempFileService;

// this is very cumbersome, has to be changed.
// right now, we don't have time to clean this up, but there should be a way.
#[derive(OpenApi)]
#[openapi(
paths(
Expand Down
2 changes: 2 additions & 0 deletions src/models/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct Site {
pub user: ObjectId,
}

// most of the names & code are self-explanatory. nothing much to document per se
impl Site {
pub async fn new(
template_id: ObjectId,
Expand Down Expand Up @@ -351,6 +352,7 @@ pub fn read_dir_to_string(dir: &Path) -> io::Result<String> {
Ok(contents)
}

// recursively reads the contents of a directory to filenames and returns them
pub fn read_dir_to_names(dir: &Path) -> io::Result<Vec<String>> {
let mut urls = Vec::new();
for entry in fs::read_dir(dir)? {
Expand Down
5 changes: 5 additions & 0 deletions src/models/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Template {
}

impl Template {
// add a template from metadata.json in the directory
pub async fn from_metadata(path: &Path) -> Result<Template, Box<dyn std::error::Error>> {
let metadata_path = path.join("metadata.json");
let metadata_file = fs::File::open(metadata_path)?;
Expand All @@ -55,6 +56,7 @@ impl Template {
})).await?)
}

// build a site from a specified template
pub async fn build_site(site_id: ObjectId, user_id: ObjectId, template_id: ObjectId, app_state: &Arc<AppState>) -> Result<PathBuf, Box<dyn std::error::Error>> {
let home_dir = std::env::var("HOME")?;
let site_dir = Path::new(&home_dir).join(".zitefy").join("sites").join(site_id.to_hex());
Expand Down Expand Up @@ -98,6 +100,7 @@ impl Template {

}

// copy everything from one dir to another
fn copy_dir_all(src: &Path, dst: &Path, exclude: Option<&str>) -> io::Result<()> {
if !dst.exists() {
fs::create_dir_all(dst)?;
Expand Down Expand Up @@ -125,6 +128,7 @@ fn copy_dir_all(src: &Path, dst: &Path, exclude: Option<&str>) -> io::Result<()>
Ok(())
}

// invoked by the background task to add a template to the db
pub async fn update_template_in_db(path: &Path, app_state: &Arc<AppState>) -> Result<(), Box<dyn std::error::Error>> {
let mut template = Template::from_metadata(path).await?;
template.dir_path = path.to_string_lossy().to_string();
Expand All @@ -139,6 +143,7 @@ pub async fn update_template_in_db(path: &Path, app_state: &Arc<AppState>) -> Re
Ok(())
}

// if ever we wanna implement deleting templates. code is redundant rn, so commenting to reduce build size.
// pub async fn delete_template_from_db(path: &Path, app_state: &Arc<AppState>) -> Result<(), Box<dyn std::error::Error>> {
// let metadata_path = path.join("metadata.json");
// if metadata_path.exists() {
Expand Down
3 changes: 3 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use std::{sync::Arc, env, path::PathBuf};

use crate::models::user::User;

// the server at zitefy.com
// checks if a username matches one in the db & if it has an active site.
// if so, serves the site, otherwise redirects to the portal
pub async fn domain_server(
app_state: web::Data<Arc<Database>>,
req: HttpRequest,
Expand Down
3 changes: 3 additions & 0 deletions src/services/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Preview {
pub desktop: PathBuf,
}

// if a path is provided, the preview images will be stored to that path, otherwise generates a temporarry file
// useful for generating permenant and temporarry images (for template previews)
pub async fn generate_preview(
html: &str,
paths: Option<Preview>,
Expand Down Expand Up @@ -55,6 +57,7 @@ pub async fn generate_preview(
})
}

// just a wrapper around scripts/builder.js
pub fn build_html_string(
html: PathBuf,
css: PathBuf,
Expand Down
1 change: 1 addition & 0 deletions src/services/tempfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use uuid::Uuid;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

// basically just a setup to manage temporarry files, like preview images in the editor.
pub struct TempFile {
path: PathBuf,
expiry: u64,
Expand Down
7 changes: 0 additions & 7 deletions src/utils.rs

This file was deleted.

0 comments on commit b3191e0

Please sign in to comment.