Skip to content

Instantly share code, notes, and snippets.

View treeform's full-sized avatar

Andre von Houck treeform

View GitHub Profile
proc mimeType*(filePath: string): string =
case filePath.splitFile.ext:
of ".html": "text/html"
of ".js": "text/javascript"
of ".css": "text/css"
of ".ttf": "font/ttf"
of ".png": "image/png"
of ".jpg", ".jpeg": "image/jpeg"
of ".svg": "image/svg+xml"
else: "text/plain"
@treeform
treeform / etag.nim
Last active May 28, 2024 18:39
genWeekEtag
proc genWeekEtag*(filepath: string): string =
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
let
info = getFileInfo(filepath)
ns = info.lastWriteTime.toUnix().uint64 * 1e9.uint64 + info.lastWriteTime.nanosecond.uint64
time = toHex(ns, sizeof(uint64)).toLowerAscii()
size = toHex(info.size, sizeof(uint64)).toLowerAscii()
result = "W/" & '"' & size & '-' & time & '"'
proc serveFile*(req: Request, path: string) {.async.} =
@treeform
treeform / metal.m
Created October 2, 2021 05:54
glfw metal example
#define GLFW_INCLUDE_NONE
#import <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#import <GLFW/glfw3native.h>
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
#import <simd/simd.h>
#include <assert.h>
#include <stdlib.h>
## Based on the work of https://github.com/edin/raytracer
## MIT License
## Copyright (c) 2021 Edin Omeragic
import benchy, chroma, math, times, glm
from pixie import Image, newImage, writeFile, setRgbaUnsafe
type Vec3 = glm.Vec3[float32]
{.push inline, noinit, checks: off.}
@treeform
treeform / parsehttp.nim
Created March 2, 2021 07:50
Some code to do http chunked encoding.
proc fetchStr(req: Request): (string, seq[string]) =
## Fetches a URL and returns header and chunks.
var socket = newSocket()
if req.url.scheme == "https":
var ctx =
try:
let caCertPath = getAppDir() / "cacert.pem"
@treeform
treeform / binomial.nim
Created November 2, 2020 16:38
How to compute Binomial Coefficients in Nim (Useful as a kind of integer version of Gaussian distribution)
import math
# See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
# for details of this function
func binomialCoeff(n, k: int): int =
var res = 1
var k = k
if (k > n - k):
k = n - k
for i in 0 ..< k:
import fidget, vmath
proc drawMain() =
... exported code here ....
windowFrame = vec2(620, 140)
startFidget(drawMain)
@treeform
treeform / build.yaml
Created July 11, 2020 16:19
windows github actions build.yaml for nim
name: Run tests
on: [push, pull_request]
jobs:
build:
runs-on: windows-2019
steps:
- name: "install choosenim"
id: cache-choosenim
run: curl -OL https://github.com/dom96/choosenim/releases/download/v0.6.0/choosenim-0.6.0_windows_amd64_debug.exe
# nim c -r --threads:on --gc:orc
import cpuinfo, os, random, locks, deques
type
WorkReq = ref object
id: int
WorkRes = ref object
id: int
import math
func genCache(): array[100, int] =
for i in 0 .. 99:
let
a = i mod 10
b = (i div 10) mod 10
result[i] = int(a ^ a) + int(b ^ b)
const