Skip to content

Instantly share code, notes, and snippets.

View hongphuc5497's full-sized avatar
🐧
Focusing

Hong Phuc hongphuc5497

🐧
Focusing
View GitHub Profile
@hongphuc5497
hongphuc5497 / modular.js
Created February 25, 2024 13:50
Module Design Pattern in JS
// Module using the Module Design Pattern
var Module = (function() {
// Private variable
var privateVariable = "I am a private variable";
// Private function
var privateFunction = function privateFunction() {
console.log("This is a private function");
}
@hongphuc5497
hongphuc5497 / print-ec2-host-with-az.bash
Last active March 24, 2024 17:50
EC2 User Data script
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
EC2_AZ=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone)
@hongphuc5497
hongphuc5497 / questions.md
Last active March 1, 2023 18:01
Websparks Nodejs Test

Q1:

  • This calculator takes values that could be written in a browsers route path as a single string. It then returns the result as a number (or an error message).

  • Route paths use the '/' symbol so this can't be in our calculator. Instead we are using the '$' symbol as our divide operator.

  • You will be passed a string of any length containing numbers and operators:

    • '+' = add
    • '-' = subtract
@hongphuc5497
hongphuc5497 / wrapper.js
Created January 13, 2023 07:37
Build reusable Try Catch Wrapper in ExpressJS
/**
* It takes a function as an argument, and returns a function that will call the original function, and
* if it throws an error, it will call the next function in the middleware chain
* @param req - The request object
* @param res - The response object
* @param next - The next function in the middleware chain.
* @returns A function that takes a function as an argument and returns the result of that function.
*/
const wrapper = (req, res, next) => async (func) => {
try {
@hongphuc5497
hongphuc5497 / README.md
Created November 8, 2022 05:53 — forked from twolfson/README.md
Audit logging via sequelize

We prefer to have audit logging in our services that leverage databases. It gives us clarity into sources of where ACL issues might originate as well as gives us a general timeline of activity in our application.

Audit logging is tedious to set up so this gist contains our latest iteration of audit logging support for a sequelize based service.

@hongphuc5497
hongphuc5497 / buildAssocOpts.js
Created October 4, 2022 19:49
Reduce dot-delimeter string to level object with reduce
const db = EXAMPLE_DB;
const expand = "group.permissions.entities.nestedEntities.slug(searchValue)"
const buildAssocOptions = (str) => {
return str
.split('.')
.reverse()
.reduce((acc, val, index, arr) => {
let currentObj = {
model:
@hongphuc5497
hongphuc5497 / encryption.js
Created August 16, 2022 08:36 — forked from vlucas/encryption.js
Stronger Encryption and Decryption in Node.js
'use strict';
const crypto = require('crypto');
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bits (32 characters)
const IV_LENGTH = 16; // For AES, this is always 16
function encrypt(text) {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
// Examples of Dependency Injection
// Example 1
class LoggerConstructorInjection {
constructor(config) {
this.config = config;
}
getConfig() {
return this.config;
@hongphuc5497
hongphuc5497 / nginx.conf
Last active July 15, 2022 17:32
Nginx Reusable Conf
user www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 65535;
function slowProduct(a, b) {
console.time('Execution time')
for (let i = 0; i <= 10000000; i++) { }
console.timeEnd('Execution time')
return console.log(a * b);
}
function memoizeValue(fnc, context) {
let res = {};