go: store urls in redis
This commit is contained in:
parent
e41239db3c
commit
9ae85f32e3
|
@ -20,61 +20,41 @@ let
|
||||||
};
|
};
|
||||||
|
|
||||||
imp = {
|
imp = {
|
||||||
|
services.redis = {
|
||||||
|
enable = true;
|
||||||
|
};
|
||||||
|
|
||||||
krebs.htgen.go = {
|
krebs.htgen.go = {
|
||||||
port = cfg.port;
|
port = cfg.port;
|
||||||
script = ''. ${pkgs.writeDash "go" ''
|
script = ''. ${pkgs.writeDash "go" ''
|
||||||
find_item() {
|
set -x
|
||||||
if test ''${#1} -ge 7; then
|
|
||||||
set -- "$(find "$STATEDIR/items" -mindepth 1 -maxdepth 1 \
|
|
||||||
-regex "$STATEDIR/items/$1[0-9A-Za-z]*$")"
|
|
||||||
if test -n "$1" && test $(echo "$1" | wc -l) = 1; then
|
|
||||||
echo "$1"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
STATEDIR=$HOME
|
|
||||||
mkdir -p "$STATEDIR/items"
|
|
||||||
|
|
||||||
case "$Method $Request_URI" in
|
case "$Method $Request_URI" in
|
||||||
"GET /"*)
|
"GET /"*)
|
||||||
if item=$(find_item "''${Request_URI#/}"); then
|
if item=$(${pkgs.redis}/bin/redis-cli --raw get "''${Request_URI#/}"); then
|
||||||
uri=$(cat "$item")
|
|
||||||
printf 'HTTP/1.1 302 Found\r\n'
|
printf 'HTTP/1.1 302 Found\r\n'
|
||||||
printf 'Content-Type: text/plain\r\n'
|
printf 'Content-Type: text/plain\r\n'
|
||||||
printf 'Connection: closed\r\n'
|
printf 'Connection: closed\r\n'
|
||||||
printf 'Location: %s\r\n' "$uri"
|
printf 'Location: %s\r\n' "$item"
|
||||||
printf '\r\n'
|
printf '\r\n'
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"POST /")
|
"POST /")
|
||||||
uri=$(mktemp -t htgen.$$.content.XXXXXXXX)
|
uri=$(head -c "$req_content_length" \
|
||||||
trap 'rm $uri >&2' EXIT
|
|
||||||
|
|
||||||
head -c "$req_content_length" \
|
|
||||||
| sed 's/+/ /g;s/%\(..\)/\\x\1/g;' \
|
| sed 's/+/ /g;s/%\(..\)/\\x\1/g;' \
|
||||||
| xargs -0 echo -e \
|
| xargs -0 echo -e \
|
||||||
| tee /tmp/tee.log \
|
| tee /tmp/tee.log \
|
||||||
| ${pkgs.urix}/bin/urix \
|
| ${pkgs.urix}/bin/urix \
|
||||||
| head -1 \
|
| head -1 \
|
||||||
> "$uri"
|
)
|
||||||
sha256=$(sha256sum -b "$uri" | cut -d\ -f1)
|
|
||||||
|
sha256=$(echo "$uri" | sha256sum -b | cut -d\ -f1)
|
||||||
base32=$(${pkgs.nixStable}/bin/nix-hash --to-base32 --type sha256 "$sha256")
|
base32=$(${pkgs.nixStable}/bin/nix-hash --to-base32 --type sha256 "$sha256")
|
||||||
item="$STATEDIR/items/$base32"
|
base32short=$(echo "$base32" | cut -b-5)
|
||||||
ref="http://$req_host/$base32"
|
${pkgs.redis}/bin/redis-cli set "$base32short" "$uri" >/dev/null
|
||||||
|
|
||||||
if ! test -e "$item"; then
|
|
||||||
mkdir -v -p "$STATEDIR/items" >&2
|
|
||||||
cp -v "$uri" "$item" >&2
|
|
||||||
fi
|
|
||||||
|
|
||||||
base32short=$(echo "$base32" | cut -b-7)
|
|
||||||
if item=$(find_item "$base32short"); then
|
|
||||||
ref="http://$req_host/$base32short"
|
ref="http://$req_host/$base32short"
|
||||||
fi
|
|
||||||
|
|
||||||
printf 'HTTP/1.1 200 OK\r\n'
|
printf 'HTTP/1.1 200 OK\r\n'
|
||||||
printf 'Content-Type: text/plain; charset=UTF-8\r\n'
|
printf 'Content-Type: text/plain; charset=UTF-8\r\n'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user