Skip to content

Instantly share code, notes, and snippets.

@karlseguin
karlseguin / db.ex
Created Feb 5, 2022
Elixir without Ecto, A thin Postgrex wrapper
View db.ex
# see https://www.openmymind.net/Elixir-Without-Ecto/
defmodule A.DB do
defmacro __using__(_) do
quote location: :keep do
@name __MODULE__
def child_spec(opts) do
%{
id: __MODULE__,
View basic-tcp-chat.zig
const std = @import("std");
const net = std.net;
const Queue = std.atomic.Queue;
const ArenaAllocator = std.heap.ArenaAllocator;
// THIS _MUST_ be placed in your main.zig file
pub const io_mode = .evented;
pub fn main() anyerror!void {
@karlseguin
karlseguin / waf.conf
Created Oct 22, 2021
basic nginx waf
View waf.conf
# WAFs are a waste of time. False positives negatively impact actual users.
# And for what? Protection against vulnerabilities that, if you still have
# in this day in age, you're in more trouble than you think.
# Still, to keep my logs clean, I like to run something like the follwoing:
location ~ \.php$ { access_log off; return 444; }
location ~ \.asp$ { access_log off; return 444; }
location ~ \.aspx$ { access_log off; return 444; }
location ~ /Admincenter/ { access_log off; return 444; }
location /console/ { access_log off; return 444; }
@karlseguin
karlseguin / util.go
Created Aug 13, 2020
Dummy modules in go
View util.go
var Util util
type util struct{}
func (_ util) PadLeft(s string, n int) string {
//...
}
// ...
@karlseguin
karlseguin / no_return.md
Created Feb 25, 2020
elixir needs a return statement
View no_return.md

This works most of the time

def handle(data) do
	with {:ok, product} <- Jason.decode!(data),
	     {:ok, category} <- Category.load(data.category_id)
	do
		# todo
	end
end
@karlseguin
karlseguin / logs.csv
Created Oct 18, 2019
1 months worth of probes to an open 80 and 443
View logs.csv
timestamp method uri ip
Oct 18, 2019 @ 09:41:02.000 - - 61.219.11.153
Oct 18, 2019 @ 09:34:37.000 GET / 164.52.24.162
Oct 18, 2019 @ 09:33:12.000 - - 61.219.11.153
Oct 18, 2019 @ 09:32:28.000 - - 61.219.11.153
Oct 18, 2019 @ 09:26:41.000 GET /otsmobile/app/mgs/mgw.htm 110.105.80.110
Oct 18, 2019 @ 09:23:42.000 GET /ftpsync.settings 185.234.216.20
Oct 18, 2019 @ 09:23:41.000 GET /deployment-config.json 185.234.216.20
Oct 18, 2019 @ 09:23:40.000 GET /.vscode/ftp-sync.json 185.234.216.20
Oct 18, 2019 @ 09:23:40.000 GET /.vscode/sftp.json 185.234.216.20
View gist:c264863573a6bfbf7605
loadArticle (res, req) ->
article = yield findArticlebyId(...)
return res.notFound() unless artilce?
res.ok(article)
findArticleById (id) ->
conn = yield conn.Open().
row = yield conn.QueryAsync("....", id)
new Article(row)
View gist:3fbe1aa749b1e58c485e
func LoadArticle(res http.ResponseWriter, req *http.Request) {
if article, err := FindArticleById(...)
if err != nil {
handleError(res, err)
return
}
if article == nil {
handleNotFound(res, err)
}
}
@karlseguin
karlseguin / gist:0ba24030fb12b10b686b
Created Mar 4, 2015
bolt vs redis (appendonly on) vs postgresql
View gist:0ba24030fb12b10b686b
/*
bolt 5000 277963 ns/op
redis 30000 48081 ns/op
pg 10000 149691 ns/op
Yes, the Bolt transactions could be batched. But so too could the PG transactions,
and the Redis work could be pipelined. And that isn't always a workable solution.
*/
import (
@karlseguin
karlseguin / init.coffee
Created Feb 13, 2015
Atom editor multiple wrap guides
View init.coffee
# place this in your init.coffee
atom.workspace.observeTextEditors (e) ->
editor = atom.views.getView(e)
clone = editor.querySelector('.wrap-guide').cloneNode()
clone.style.left = (editor.getDefaultCharacterWidth() * 120) + "px"
editor.querySelector('.underlayer').appendChild(clone)