Skip to content

Instantly share code, notes, and snippets.

@MrTin
MrTin / nextjs-basic-http-auth.ts
Created October 20, 2022 17:28
NextJS Basic HTTP Authentication
// src/middleware.ts
import { NextRequest, NextResponse } from 'next/server';
export const config = {
matcher: '/:path*',
};
export function middleware(req: NextRequest) {
const basicAuth = req.headers.get('authorization');
const url = req.nextUrl;
@MrTin
MrTin / pm2.js
Last active October 18, 2022 20:16
NestJS PM2
// package.json
{ "pm2": "^5.2.0" }
{ "start:prod:cluster": "pm2-runtime start ecosystem.config.js --env production" }
// ecosystem.config.js
module.exports = {
apps: [
{
name: 'api',
script: 'dist/src/main.js',
// https://www.apollographql.com/docs/apollo-server/data/errors/#unauthenticated
// Using types and unions
// https://blog.logrocket.com/handling-graphql-errors-like-a-champ-with-unions-and-interfaces/
// Wrap mutation with result{user, errors}
// https://stackoverflow.com/a/55532706
// UserInputError
// ValidationError
import { existsSync, createReadStream } from 'fs';
export const streamAgentLogToStdout = async (
filePath = '/tmp/appsignal.log',
) => {
let totalLookups = 0;
const lookupLimit = 10;
const intervalObj = setInterval(function () {
const fileExists = existsSync(filePath);
totalLookups += 1;
@MrTin
MrTin / default-order-rails-uuid.rb
Last active March 5, 2020 15:59
Set default order when using Rails ActiveRecord and UUID
class ApplicationController < ActiveRecord::Base
self.abstract_class = true
# Default order on :created_at instead of primary key
self.implicit_order_column = :created_at
end
@MrTin
MrTin / wsl.conf
Created January 28, 2020 21:21
WSL mount C to root instead of /mnt
# Enable extra metadata options by default
[automount]
enabled = true
root = /
options = "metadata,umask=22,fmask=11"
mountFsTab = false
@MrTin
MrTin / database_url-ecto3-phoenix-distiller.ex
Created February 28, 2019 11:19
DATABASE_URL with Phoenix, Ecto 3+ and Distillery
defmodule MyApp.Repo do
require Logger
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
def init(_type, config) do
database_url = System.get_env("DATABASE_URL")
if database_url == nil do
Logger.debug "$DATABASE_URL not set, using config"
@MrTin
MrTin / fix-libv8-mac.txt
Last active February 19, 2019 12:57 — forked from fernandoaleman/fix-libv8-mac.txt
Fixing libv8 and therubyracer on Mac
brew tap homebrew/versions
brew install v8-315
gem install libv8 -v '3.16.14.15' -- --with-system-v8
gem install therubyracer -v '0.12.2' -- --with-v8-dir=/usr/local/opt/v8@3.15
bundle

Keybase proof

I hereby claim:

  • I am mrtin on github.
  • I am d6nomad (https://keybase.io/d6nomad) on keybase.
  • I have a public key ASAF_eeHK6aYmUtu8db-4UX5A4P3fFOzxxy_rKSBuYqUDQo

To claim this, I am signing this object:

@MrTin
MrTin / setup_local-dns.sh
Last active October 31, 2018 18:01
Setup dnsmasq and resolvers on Mac OS X to enable *.test and wildcard subdomain *.local.test for local development.
#!/bin/bash
# Install directly using console:
# bash <(curl -s https://gist.githubusercontent.com/MrTin/fa72909939e2752615caa4d272e944ef/raw)
# Install dnsmasq
brew install dnsmasq
# Create config directory
mkdir -pv $(brew --prefix)/etc/