Skip to content

Instantly share code, notes, and snippets.

View rpn3319's full-sized avatar

Raymond Phu Nguyen rpn3319

  • Ho Chi Minh City, Vietnam
  • 17:55 (UTC +07:00)
  • LinkedIn in/rpn19
View GitHub Profile
@rpn3319
rpn3319 / rust-traverse-directory.rs
Created July 8, 2023 06:45
rust traverse directory
fn traverse_directory(dir_path: &str, parent: &str, files: &mut Vec<String>) {
if let Ok(entries) = fs::read_dir(dir_path) {
for entry in entries {
if let Ok(entry) = entry {
let file_type = entry.file_type().unwrap();
let name = entry.file_name().into_string().unwrap();
if file_type.is_dir() {
traverse_directory(
&entry.path().to_string_lossy(),
@rpn3319
rpn3319 / rust-current-unix-time-stamp.rs
Created July 8, 2023 06:42
rust current unix timestamp
let now = SystemTime::now();
let duration = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
@rpn3319
rpn3319 / async-upload-file-reqwest.rs
Last active April 12, 2024 09:38
rust reqwest upload file async
// reqwest = { version = "0.11", features = ["multipart"] }
use reqwest::multipart::Part;
async fn upload_file() -> Result<(), Box<dyn std::error::Error>> {
let content: Vec<u8> = tokio::fs::read("path/to/file").await?;
let part = Part::bytes(content).file_name("file_name.extension");
let file = reqwest::multipart::Form::new().part("field_name", part);
let response = reqwest::Client::new()
@rpn3319
rpn3319 / aws-update-sts-token.sh
Last active July 8, 2023 06:48
aws-refresh-sts-token.sh
#!/bin/bash
main_profile=myprofile
mfa_profile=myprofile-mfa
mfa_device_arn=arn:aws:iam::999999999999:mfa/example_device
mfa_token_code=$1
echo "Getting temporary credentials for MFA device..."
output=$(aws sts get-session-token --serial-number $mfa_device_arn --token-code $mfa_token_code --profile $main_profile)
@rpn3319
rpn3319 / nodejs-simulate-await-in-loop-block-event-loop.js
Last active June 2, 2023 09:06
nodejs-simulate-await-in-loop-block-event-loop
const express = require('express');
const axios = require('axios');
const Bluebird = require('bluebird');
const { readFile: someOtherFnc } = require('fs');
const app = express();
// This is just a simulation
// real-life functions might not obviously simple for us to realize
async function simulateBlocker() {
@rpn3319
rpn3319 / compare-commit-messages.js
Created May 21, 2023 14:27
Compare 2 branches by commit messages instead of commit hashes
const { execSync } = require('child_process');
// Usage: compareBranches('branchA', 'branchB');
compareBranches(branchA, branchB);
function getCommitMessages(branch) {
const command = `git log --pretty=format:'%s' ${branch}`; // Added %h to include commit hash
const output = execSync(command, { encoding: 'utf-8' });
return output.trim().split('\n');
}
@rpn3319
rpn3319 / join-by-row-order.sql
Created April 30, 2023 11:15
SQL snippet join by row order
select
Doctor.name,
Professor.name,
Singer.name,
Actor.name
from (
select @p:=@p+1 iterator, name
from occupations, (SELECT @p:=0) p
where occupation = "Professor"
order by name
@rpn3319
rpn3319 / Dockerfile
Created April 30, 2023 07:54
NestJS Dockerfile
FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
@rpn3319
rpn3319 / bucket-policy.json
Last active April 13, 2023 14:03
aws s3 static web bucket policy and CORS configuration
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<bucket-name>/*"
}
@rpn3319
rpn3319 / elasticsearch-http-api.http
Created December 10, 2022 13:46
elasticsearch-http-api
GET /_cluster/health
GET /_cat/indices?v
GET /_cat/nodes?v
GET /_cat/shards?v
### Add an index
PUT /products