Skip to content

Instantly share code, notes, and snippets.

View paulswail's full-sized avatar

Paul Swail paulswail

View GitHub Profile
@paulswail
paulswail / template_weekly.md
Created September 1, 2023 17:25 — forked from mediapathic/template_weekly.md
Obsidian weekly template
—-
Status:
Tags: [[<%tp.date.now("YYYY-[M]MM")%>]]
Links: [[Weekly Reviews]]
___
# <% tp.file.title %>
[[<%tp.date.now("YYYY-[W]ww", -9)%>]] <== This Week ==> [[<%tp.date.now("YYYY-[W]ww", 2)%>]]
## Days
### SUN
@paulswail
paulswail / serverless.yml
Last active November 11, 2022 08:05
Configuring a CloudFront distribution in front of API Gateway (Serverless Framework)
resources:
Resources:
APIGatewayFront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
IPV6Enabled: true
HttpVersion: http2
Comment: CDN in front of API Gateway
@paulswail
paulswail / api-client.ts
Last active April 6, 2022 11:29
AppSync GraphQL CodeGenerator Config with Lambda resolver types, Nodejs operation functions and React hooks
// frontend lib module which is injected into the codegen'd hooks
import { getAuthToken } from './auth'
export const API_URL = process.env.REACT_APP_API_URL!
/**
* Custom fetcher used by codegen'd hooks to first load auth token, then make the fetch request.
*/
export const fetcher = <TData, TVariables>(query: string, variables?: TVariables) => {
return async (): Promise<TData> => {
@paulswail
paulswail / prompt.zsh
Created August 10, 2021 11:11
ZSH prompt with Git and AWS profile status
# original script credit: https://github.com/holman/dotfiles
autoload colors && colors
if (( $+commands[git] ))
then
git="$commands[git]"
else
git="/usr/bin/git"
fi
@paulswail
paulswail / cicd-pipelines.ts
Last active December 12, 2022 13:53
Create CICD pipelines for serverless services using AWS CDK
// /stacks/cicd-pipelines.ts
// CDK app which creates a stack using a set of service definitions
import 'source-map-support/register';
import { App } from '@aws-cdk/cdk';
import { ServiceCicdPipelines } from '../lib/cicd/pipelines';
import { deploymentTargetAccounts } from './config';
import services from './services';
const app = new App({
@paulswail
paulswail / invalidate-cloudfront-distribution-by-origin-domain.sh
Last active January 18, 2019 11:31
Invalidate CloudFront Distribution cache using only its origin domain name (AWS CLI)
#!/bin/bash
CLOUDFRONT_DOMAIN="mydomain.com" # change this
# Get Distribution ID (requires node.js to be installed)
DISTRIBUTION_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[].{Id: Id, DomainName: DomainName, OriginDomainName: Origins.Items[0].DomainName }[?contains(OriginDomainName, '$CLOUDFRONT_DOMAIN')] | [0]" | node -pe "JSON.parse(require('fs').readFileSync('/dev/stdin').toString()).Id")
# Invalidate cache
echo "Invalidating CloudFront distribution $DISTRIBUTION_ID ..."
aws cloudfront create-invalidation --distribution-id=$DISTRIBUTION_ID --paths '/*'
@paulswail
paulswail / compound-growth.js
Last active June 7, 2018 09:55
Calculate investment's compound growth
// Calculates the compound growth of an investment over number of periods.
// Assumes fixed regular contributions and average growth rate.
function compoundGrowth(startingBalance = 0, periodContribution = 100, periodGrowthRate=0, nPeriods = 1, currencyLabel = '£') {
const totalContribution = startingBalance + periodContribution * nPeriods;
const finalBalance = [...Array(nPeriods)].reduce((runningBalance) => {
return runningBalance + (runningBalance * periodGrowthRate) + periodContribution;
}, startingBalance);
const absoluteGrowth = finalBalance - totalContribution;
const relativeGrowth = absoluteGrowth / totalContribution;
console.log(`${currencyLabel}${finalBalance.toFixed(2)} will be returned after ${nPeriods} periods with ${currencyLabel}${periodContribution.toFixed(2)} contributed each period and an average period growth rate of ${periodGrowthRate * 100}% and initial lump sum of ${currencyLabel}${startingBalance}.
@paulswail
paulswail / gist:842af71cbc5c42b075a8
Created March 9, 2016 10:50
Promise rethrow inside catch
function rethrowTest() {
Promise.resolve(true).then(function() {
return new Promise(function(resolve, reject) {
reject(new Error('BOOM!'));
}).catch(function(err) {
console.error('First catch: ' + err);
throw err;
});
}).then(function() {
console.log('I should not be printed');