Skip to content

Instantly share code, notes, and snippets.

View emmiep's full-sized avatar

Emmie Päivärinta emmiep

View GitHub Profile
@emmiep
emmiep / readkey.c
Created November 4, 2019 18:31
Read private RSA key from PEM file
#include <stdio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
int main(int argc, char *argv[]) {
FILE *f = fopen(argv[1], "r");
RSA *key = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
RSA_print_fp(stdout, key, 0);
fclose(f);
@emmiep
emmiep / Dockerfile
Created September 12, 2019 19:04
Minimal Caddy dockerfile
FROM golang:1.13 AS build
ENV GO111MODULE on
RUN go get github.com/caddyserver/caddy/caddy
FROM busybox:1-glibc
COPY --from=build /go/bin/caddy /usr/local/bin/caddy
@emmiep
emmiep / session.cr
Created May 23, 2019 14:42
Crystal session counter
get "/" do |env|
count = env.session.int?("count")
count = 0 unless count
env.session.int("count", count+1)
"Count = #{count}"
end
@emmiep
emmiep / randstring.go
Created April 6, 2019 18:56
Random strings in Go
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
)
func randomString(n int) (str string, err error) {
@emmiep
emmiep / whilewait.go
Last active March 16, 2019 14:59
Periodically do background task while waiting for result
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan string)
t := time.NewTicker(200 * time.Millisecond)
@emmiep
emmiep / cache.js
Created August 24, 2018 13:40
Async caching with WeakMap and promises
export function createAsyncCache(fn) {
const cache = new WeakMap();
return {
get(key) {
if (cache.has(key)) {
return cache.get(key);
}
const promise = Promise.resolve(fn(key));
@emmiep
emmiep / mode.js
Created August 12, 2018 13:27
Object rest spread with falsy value
view({showProfile}={}) {
const {name, id, profile} = this;
return {
name,
id,
...(showProfile && {profile})
};
}
@emmiep
emmiep / noreturn.js
Created August 9, 2018 19:38
Good coding style?
function getUser(id) {
if (users.has(id)) {
return users.get(id);
}
}
@emmiep
emmiep / nodemon.json
Created August 8, 2018 11:13
Nodemon for dart
{
"watch": [
"bin/",
"lib/"
],
"execMap": {
"dart": "dart"
},
"ext": "dart"
}
@emmiep
emmiep / nginx.conf
Last active July 3, 2018 12:18
Nginx basic SPA config
http {
server {
listen 80;
root /usr/share/nginx/html;
location / {
internal;
}
location ~ ^/(js/|img/|css/|favicon\.ico$) {