Skip to content

Instantly share code, notes, and snippets.

@rmoorman
rmoorman / README.md
Created June 3, 2022 11:25 — forked from tcbyrd/README.md
Route53 CNAME Update

AWS CLI command to update CNAME

When you have a set of application servers running in EC2 in an active/passive configuration, the easiest way to failover is to simply update the DNS to point to the second server as soon as it's available to serve requests. If you are using Route 53 to manage your DNS configuration, with the AWS CLI you can make this change in a single command.

Initial Setup

The CLI expects the change to be submitted via a JSON-formatted configuration file. I've inclu

@rmoorman
rmoorman / email.exs
Created November 20, 2021 12:42 — forked from daemonfire300/email.exs
Simple shot at implementing an email validator for use with `Ecto.Changeset`
defmodule YourApp.Validators.Email do
use Ecto.Changeset
@mail_regex ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/
# ensure that the email looks valid
def validate_email(changeset, field) do
changeset
|> validate_format(field, @mail_regex)
end
end
@rmoorman
rmoorman / registration_controller.ex
Created November 20, 2021 12:29 — forked from abitdodgy/registration_controller.ex
Medium Article: Building Many-To-Many Associations with Embedded Schemas in Ecto and Phoenix
defmodule App.RegistrationController do
use App.Web, :controller
alias App.{Registration, Repo}
def new(conn, _params) do
changeset = Registration.changeset(%Registration{})
render conn, :new, changeset: changeset
end
def create(conn, %{"registration" => registration_params}) do
@rmoorman
rmoorman / instructions.md
Created September 12, 2021 22:21 — forked from wosephjeber/instructions.md
Ecto migration for renaming table with indexes and constraints

Renaming table in Ecto migration

I recently wanted to rename a model and its postgres table in a Phoenix app. Renaming the table was simple and documented, but the table also had constraints, sequences, and indexes that needed to be updated in order for the Ecto model to be able to rely on default naming conventions. I couldn't find any examples of what this would look like but was eventually able to figure it out. For anyone else in the same situation, hopefully this example helps.

In the example below, I'm renaming the Permission model to Membership. This model belongs to a User and an Account, so it has foreign key constraints that need to be renamed.

defmodule MyApp.Repo.Migrations.RenamePermissionsToMemberships do
  use Ecto.Migration
@rmoorman
rmoorman / registration.ex
Created August 23, 2021 15:37 — forked from abitdodgy/registration.ex
Medium Article: Building Many-To-Many Associations with Embedded Schemas in Ecto and Phoenix
defmodule App.Registration do
use App.Web, :model
alias App.{Account, User, Membership, Repo}
embedded_schema do
field :email
field :org_name
end
@required_fields ~w(email org_name)a
@rmoorman
rmoorman / xpath_soup.py
Created August 4, 2021 09:57 — forked from ergoithz/xpath_soup.py
Generate unique XPATH for BeautifulSoup element
#!/usr/bin/python
# -*- coding: utf-8 -*-
def xpath_soup(element):
# type: (typing.Union[bs4.element.Tag, bs4.element.NavigableString]) -> str
"""
Generate xpath from BeautifulSoup4 element.
:param element: BeautifulSoup4 element.
:type element: bs4.element.Tag or bs4.element.NavigableString
@rmoorman
rmoorman / mock_websocket_server.ex
Created June 23, 2021 22:43 — forked from pulkit110/mock_websocket_server.ex
Supplementary Code for Testing WebSocket Clients in Elixir with a Mock Server
defmodule Commerce.Orders.MockWebsocketServer do
use Plug.Router
plug(:match)
plug(:dispatch)
match _ do
send_resp(conn, 200, "Hello from plug")
end
@rmoorman
rmoorman / runtime_types.exs
Created May 29, 2021 18:29 — forked from JEG2/runtime_types.exs
How to get typespec information from Elixir at runtime.
defmodule TypeSpecs do
def for_module(module) do
{:ok, {^module, [{:abstract_code, {:raw_abstract_v1, attributes}}]}} =
module |> :code.which |> :beam_lib.chunks([:abstract_code])
attributes
|> Enum.filter_map(fn
{:attribute, _, :spec, _function_and_types} -> true
_attribute -> false
end, fn {:attribute, _, :spec, function_and_types} ->
function_and_types
@rmoorman
rmoorman / exaramel.yml
Created February 17, 2021 11:42 — forked from humpalum/exaramel.yml
Ansible playbook to check for files related to Exaramel
---
# Checks if files exists that related to Exaramel Malware
# Ref:https://www.cert.ssi.gouv.fr/uploads/CERTFR-2021-CTI-005.pdf
- name: Setting files to check
set_fact:
maliciousFiles:
- /tmp/.applocktx
- /tmp/.applock
- /usr/local/centreon/www/search.php
@rmoorman
rmoorman / axios-catch-error.js
Created September 10, 2020 09:56 — forked from fgilio/axios-catch-error.js
Catch request errors with Axios
/*
* Handling Errors using async/await
* Has to be used inside an async function
*/
try {
const response = await axios.get('https://your.site/api/v1/bla/ble/bli');
// Success 🎉
console.log(response);
} catch (error) {
// Error 😨