Skip to content

Instantly share code, notes, and snippets.

View maartenvanvliet's full-sized avatar
🍊

Maarten van Vliet maartenvanvliet

🍊
View GitHub Profile
@filipecabaco
filipecabaco / form_from_schema.ex
Created March 31, 2023 10:41
Simple component to build forms from a given schema
defmodule AppWeb.Component.FormFromSchema do
use AppWeb, :live_component
attr :changeset, :any, required: true
attr :event_suffix, :string, default: ""
attr :schema, :any, required: true
attr :uploads, :any, default: nil
attr :visibility_rules, :any, default: %{}
attr :field_type_rules, :any, default: %{}

Using :httpc securely

Introduction

There are many HTTP clients in the Erlang and Elixir ecosystem, in fact OTP itself comes with one included - :httpc.

This raises the question which client to use and whether :httpc is good enough if all you need is a simple request here and there. This notebook explores relevant security concerns and highlights how to address them when using :httpc.

@wpiekutowski
wpiekutowski / some_subscription_test.exs
Created August 7, 2020 13:34
Testing subscriptions: Elixir + Absinthe + Phoenix WebSocket
defmodule App.Schema.SomeSubscriptionTest do
use App.SubscriptionCase
test "subscription: someSubscription" do
subscription_query = """
subscription {
someSubscription {
someData
}
}
@ishikawa
ishikawa / parameterized_test.exs
Last active December 10, 2019 12:18
My preferred way to write parameterized tests in Elixir.
# The helper functions for the test module. To make it possible to import
# this helper module in the test module, define this module outside the context that uses it.
defmodule MyTest.Helpers do
@spec fake_params(Enumrable.t()) :: map
def fake_params(override \\ %{}) do
%{
country: "jp",
phone_number: Faker.phone_number(),
locale: "ja",
company: "My Company",
@rhnonose
rhnonose / absinthe_collector.ex
Created October 18, 2019 17:57
Absinthe Collector
defmodule AbsintheCollector do
@moduledoc """
Module to collect absinthe telemetry events
"""
use Prometheus.Metric
require Prometheus.Contrib.HTTP
@events [
[:absinthe, :resolve, :field, :start],
[:absinthe, :resolve, :field],

Using Elixir releases and multi-stage Docker files to simplify Phoenix deployment

This repo is my experiment in deploying a basic Phoenix app using the release feature from elixir 1.9 (https://elixir-lang.org/blog/2019/06/24/elixir-v1-9-0-released/) and docker, via a multi-stage Dockerfile (https://docs.docker.com/develop/develop-images/multistage-build/) leveraging bitwalker's docker images for Elixir and Phoenix.

Step 1: Install Elixir 1.9.1 (and Erlang)

The simplest way to manage Elixir versions is to use asdf.

@bruce
bruce / absinthe.md
Last active November 2, 2019 16:03
Very brief description of how Absinthe processes GraphQL
@ibarchenkov
ibarchenkov / stream_data_ecto_factory.ex
Created January 31, 2019 21:09
Elixir Ecto testing factories combined with StreamData generators.
defmodule MyApp.Factory do
use ExUnitProperties
alias MyApp.{Repo, User, Comment}
### Generators
def generator(:user) do
gen all name <- string(:alphanumeric, min_length: 2),
email <- generator(:email),
age <- integer(10..130) do
@opsb
opsb / ecto_utils.ex
Last active March 1, 2018 16:23
Export elixir Ecto models as plain maps
defmodule Util.Ecto do
def export(model = %{__meta__: _meta, __struct__: _struct}) when is_map(model) do
stripped =
model
|> Map.from_struct()
|> Map.delete(:__meta__)
associations = model.__struct__.__schema__(:associations)
Enum.reduce(associations, stripped, fn assoc, model ->
@almoraes
almoraes / 0x-ebrotate.config
Created January 2, 2018 17:34
EB AWS rotate docker logs
files:
"/etc/logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.dockerlogs.conf":
mode: "000644"
owner: root
group: root
content: |
/var/lib/docker/containers/*/*.log {
size 100M
rotate 14
missingok