Skip to content

Instantly share code, notes, and snippets.

View drowzy's full-sized avatar
🤠
Focusing

Simon Thörnqvist drowzy

🤠
Focusing
View GitHub Profile
@drowzy
drowzy / dynamic.ex
Created July 19, 2018 13:58
Dynamically load module in elixir
# Example:
#
# {:ok, mod} = Dynamic.load(Dynamic, "mod")
# "world" == apply(mod, :hello, [])
# {:error, _} = Dynamic.Load(Dynamic, "does_not_exist")
#
defmodule Dynamic do
def load(namespace, name) do
mod_name = String.capitalize(name)
full_name = Module.concat(namespace, mod_name)
@drowzy
drowzy / without.js
Created April 11, 2018 11:13
es6 dynamic key without
const obj = { foo: 'foo', bar: 'bar' };
const key = 'foo';
const { [key]: _, ...newObj } = obj;
defmodule SSHt.Tunnel do
@type to :: {:tcpip, tuple()} | {:local, String.t}
@spec start_link(pid(), to) :: {:ok, pid()} | {:error, term()}
def start_link(ref, to) do
DynamicSupervisor.start_child(
SSHt.TunnelSupervisor,
worker_spec(worker_opts(ref, to))
)
end
defmodule SSHt.Tunnel.TCPHandler do
use GenServer
require Logger
def start_link(ref, socket, transport, opts) do
pid = :proc_lib.spawn_link(__MODULE__, :init, [{ref, socket, transport, opts}])
{:ok, pid}
end
def init({ref, socket, transport, opts}) do
defmodule SSHt.Tunnel do
def start_link(ref, to) do
DynamicSupervisor.start_child(
SSHt.TunnelSupervisor,
{SSHt.Tunnel.TCPServer, worker_opts(ref, to)}
)
end
defp worker_opts(ref, {:tcpip, {port, _}} = to),
do: basic_opts(ref, base_name(port), to)
defmodule SSHt.Application do
@moduledoc """
Application module
"""
use Application
def start(_type, _args) do
children = [
{DynamicSupervisor, name: SSHt.TunnelSupervisor, strategy: :one_for_one}
]
defmodule SSHt do
@ini_window_size 1024 * 1024
@max_packet_size 32 * 1024
@direct_tcpip String.to_charlist("direct-tcpip")
def connect(opts \\ []) do
host = Keyword.get(opts, :host, "127.0.0.1")
port = Keyword.get(opts, :port, 22)
ssh_config = defaults(opts)
@drowzy
drowzy / SSHt.ex
Last active March 21, 2018 22:45
ssh connect
defmodule SSHt do
def connect(opts \\ []) do
host = Keyword.get(opts, :host, "127.0.0.1")
port = Keyword.get(opts, :port, 22)
ssh_config = defaults(opts)
:ssh.connect(String.to_charlist(host), port, ssh_config)
end
defp defaults(opts) do
@drowzy
drowzy / DispatchBeforeRoute.jsx
Last active February 25, 2018 12:07
React router execute action before routing
import React, { Component } from 'react';
import { Route } from 'react-router-dom';
export class DispatchWrapper extends Component {
componentDidMount() {
this.props.action();
}
render() {
let { component: Component, action, ...rest } = this.props;
return <Component {...rest} />;