Skip to content

Instantly share code, notes, and snippets.

View khaledosman's full-sized avatar
🤘

Khaled Osman khaledosman

🤘
View GitHub Profile
@khaledosman
khaledosman / mongo-backup.sh
Last active October 15, 2018 09:00
bash script for taking daily mongo backups inside docker image
#!/bin/bash
export CONTAINER_NAME=""
export DATABASE_NAME=""
export BACKUP_LOCATION="/home/ec2-user/backups"
export MONGO_USER=""
export MONGO_PASSWORD=""
export TIMESTAMP=$(date +'%Y%m%d%H%M%S')
export DUMP_PATH=/data/${DATABASE_NAME}-backup-${TIMESTAMP}
export ZIP_PATH=${DATABASE_NAME}-backup-${TIMESTAMP}.zip
const getNextDate = (dateObj, recurringType) => {
let pointerTime = dateObj.getTime()
const generatedTimes = []
while (pointerTime <= Date.now()) {
let pointerDate = new Date(pointerTime)
if (recurringType === 'daily') {
pointerTime = pointerDate.setHours(pointerDate.getHours() + 24)
} else if (recurringType === 'monthly') {
pointerTime = pointerDate.setMonth(pointerDate.getMonth() + 1)
@khaledosman
khaledosman / dynamodb-helpers.ts
Last active February 21, 2022 00:02
Simple CRUD operations for DynamoDB using NodeJS & Typescript
import * as AWS from 'aws-sdk'
import { DeleteItemOutput, DocumentClient, GetItemOutput, PutItemOutput, QueryOutput,
ScanOutput, UpdateItemOutput } from 'aws-sdk/clients/dynamodb'
AWS.config.update({ region: 'eu-central-1' })
const { DynamoDB } = AWS
const dynamoDB: DocumentClient = new DynamoDB.DocumentClient()
export function query (attributeName: string, attributeValue: string): Promise<QueryOutput> {
const params = {
@khaledosman
khaledosman / s3-helpers.ts
Last active August 23, 2018 11:26
Wrappers around AWS S3 SDK for NodeJS & Typescript
import { S3 } from 'aws-sdk'
import { CreateBucketOutput, GetObjectOutput, HeadObjectOutput, ListObjectsV2Output,
PutObjectOutput } from 'aws-sdk/clients/s3'
const s3: S3 = new S3()
const BUCKET_NAME: string = process.env.BUCKET_NAME
export function appendToFile (params): Promise<PutObjectOutput> {
const { Body, ...rest } = params
return getFileContent(rest)
@khaledosman
khaledosman / generate-zip-from-files.js
Last active April 17, 2023 05:26
recursively add specific files within a directory to a zipfile using NodeJS & JsZip
const fs = require('fs')
const path = require('path')
const JSZip = require('jszip')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
const readDir = promisify(fs.readdir)
const lstat = promisify(fs.lstat)
function addFilesToZip (jsZip, directoryPath, filesToInclude) {
const promiseArr = filesToInclude.map(async file => {
@khaledosman
khaledosman / calculate-great-circle-distance.ts
Created August 22, 2018 08:44
returns shortest distance between two geolocations
interface Location {
lat: number,
long: number
}
// Converts from degrees to radians.
function toRadians (degrees: number): number {
return degrees * Math.PI / 180
}
@khaledosman
khaledosman / lambda-response.ts
Created August 23, 2018 11:28
aws lambda response w/ cors enabled for serverless
export function lambdaResponse (body: any, statusCode = 200): any {
return {
body: JSON.stringify(body),
headers: {
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*'
},
statusCode,
isBase64Encoded: false
@khaledosman
khaledosman / es6 modules in node.json
Last active February 11, 2019 14:15
package.json quick setup to work with es6 modules in node without using --experimental-modules and .mjs files or setting up a webpack.config
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"nodemon": "^1.18.3"
},
"babel": {
"presets": [
"env"
]
},
@khaledosman
khaledosman / oath2-authorization-code.js
Created August 30, 2018 10:13
PubNub functions implementation of Google OAUTH2 w/ Authorization code flow
// https://developers.google.com/actions/identity/oauth2?oauth=code
const kvstore = require('kvstore')
const base64 = require('codec/base64')
const GOOGLE_CLIENT_ID = 'google-client-id'
export default (request, response) => {
let headersObject = request.headers
let bodyString = request.body
let paramsObject = request.params
@khaledosman
khaledosman / convert-to-pem.sh
Created September 13, 2018 08:47
Converts *.key and *.crt files to pem format
#Convert private key to em format
openssl rsa -in domain.key -text > domain.pem
#Convert .crt file to .pem format
openssl x509 -in cert.crt -out cert.pem -outform PEM
openssl x509 -in ca.crt -out ca.pem -outform PEM