summaryrefslogtreecommitdiffstats
path: root/tv/2configs/elm-packages-proxy.nix
diff options
context:
space:
mode:
Diffstat (limited to 'tv/2configs/elm-packages-proxy.nix')
-rw-r--r--tv/2configs/elm-packages-proxy.nix150
1 files changed, 125 insertions, 25 deletions
diff --git a/tv/2configs/elm-packages-proxy.nix b/tv/2configs/elm-packages-proxy.nix
index 17a0d2304..097d706c6 100644
--- a/tv/2configs/elm-packages-proxy.nix
+++ b/tv/2configs/elm-packages-proxy.nix
@@ -4,20 +4,45 @@
cfg.packageDir = "/var/lib/elm-packages";
cfg.port = 7782;
+ # TODO secret files
+ cfg.htpasswd = "/var/lib/certs/package.elm-lang.org/htpasswd";
+ cfg.sslCertificate = "/var/lib/certs/package.elm-lang.org/fullchain.pem";
+ cfg.sslCertificateKey = "/var/lib/certs/package.elm-lang.org/key.pem";
+
+ semverRegex =
+ "(?<major>0|[1-9]\\d*)\\.(?<minor>0|[1-9]\\d*)\\.(?<patch>0|[1-9]\\d*)(?:-(?<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?";
+
in {
services.nginx.virtualHosts."package.elm-lang.org" = {
addSSL = true;
- # TODO secret files
- sslCertificate = "/var/lib/certs/package.elm-lang.org/fullchain.pem";
- sslCertificateKey = "/var/lib/certs/package.elm-lang.org/key.pem";
+ sslCertificate = cfg.sslCertificate;
+ sslCertificateKey = cfg.sslCertificateKey;
+
+ locations."/all-packages".extraConfig = ''
+ proxy_pass http://127.0.0.1:${toString config.krebs.htgen.elm-packages-proxy.port};
+ proxy_pass_header Server;
+ '';
locations."/all-packages/since/".extraConfig = ''
proxy_pass http://127.0.0.1:${toString config.krebs.htgen.elm-packages-proxy.port};
proxy_pass_header Server;
'';
- locations."~ ^/packages/(?<author>[A-Za-z0-9-]+)/(?<pname>[A-Za-z0-9-]+)/(?<version>(?<major>0|[1-9]\\d*)\\.(?<minor>0|[1-9]\\d*)\\.(?<patch>0|[1-9]\\d*)(?:-(?<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?)/(?:zipball|elm.json|endpoint.json)\$".extraConfig = ''
+ locations."~ ^/packages/(?<author>[A-Za-z0-9-]+)/(?<pname>[A-Za-z0-9-]+)/(?<version>${semverRegex})\$".extraConfig = ''
+ auth_basic "Restricted Area";
+ auth_basic_user_file ${cfg.htpasswd};
+
+ proxy_set_header X-User $remote_user;
+ proxy_set_header X-Author $author;
+ proxy_set_header X-Package $pname;
+ proxy_set_header X-Version $version;
+ proxy_pass_header Server;
+
+ proxy_pass http://127.0.0.1:${toString config.krebs.htgen.elm-packages-proxy.port};
+ '';
+
+ locations."~ ^/packages/(?<author>[A-Za-z0-9-]+)/(?<pname>[A-Za-z0-9-]+)/(?<version>${semverRegex})/(?:zipball|elm.json|endpoint.json)\$".extraConfig = ''
set $zipball "${cfg.packageDir}/$author/$pname/$version/zipball";
proxy_set_header X-Author $author;
proxy_set_header X-Package $pname;
@@ -39,14 +64,15 @@ in {
krebs.htgen.elm-packages-proxy = {
port = cfg.port;
- script = /* sh */ ''(. ${pkgs.writeDash "elm-packages-proxy.sh" ''
+ script = /* sh */ ''. ${pkgs.writeDash "elm-packages-proxy.sh" ''
PATH=${lib.makeBinPath [
+ pkgs.attr
pkgs.coreutils
pkgs.curl
pkgs.findutils
pkgs.gnugrep
pkgs.jq
- pkgs.unzip
+ pkgs.p7zip
]}
export PATH
file_response() {(
@@ -73,7 +99,7 @@ in {
printf "HTTP/1.1 $status_code $status_reason\r\n"
printf 'Connection: close\r\n'
- printf 'Content-Length: %d\r\n' ''${#response_body}
+ printf 'Content-Length: %d\r\n' "$(expr ''${#response_body} + 1)"
printf 'Content-Type: %s\r\n' "$content_type"
printf 'Server: %s\r\n' "$Server"
printf '\r\n'
@@ -99,7 +125,7 @@ in {
;;
elm.json)
if ! test -f "$elmjson"; then
- unzip -p "$zipball" \*/elm.json > "$elmjson"
+ 7z x -so "$zipball" \*/elm.json > "$elmjson"
fi
file_response 200 OK "$elmjson" 'application/json; charset=UTF-8'
exit
@@ -119,36 +145,110 @@ in {
;;
esac
;;
- 'POST /all-packages/since/'*)
+ 'POST /packages/'*)
+
+ author=$req_x_author
+ pname=$req_x_package
+ user=$req_x_user
+ version=$req_x_version
+
+ action=uploading
+ force=''${req_x_force-false}
+ zipball=${cfg.packageDir}/$author/$pname/$version/zipball
+ elmjson=$HOME/cache/$author%2F$pname%2F$version%2Felm.json
+ endpointjson=$HOME/cache/$author%2F$pname%2F$version%2Fendpoint.json
+
+ if test -e "$zipball"; then
+ if test "$force" = true; then
+ zipball_owner=$(attr -q -g X-User "$zipball" || :)
+ if test "$zipball_owner" = "$req_x_user"; then
+ action=replacing
+ rm -f "$elmjson"
+ rm -f "$endpointjson"
+ else
+ string_response 403 Forbidden \
+ "package already exists: $author/$pname@$version" \
+ text/plain
+ exit
+ fi
+ else
+ string_response 409 Conflict \
+ "package already exists: $author/$pname@$version" \
+ text/plain
+ exit
+ fi
+ fi
+
+ echo "user $user is $action package $author/$pname@$version" >&2
+ # TODO check package
+ mkdir -p "$(dirname "$zipball")"
+ head -c $req_content_length > "$zipball"
+
+ attr -q -s X-User -V "$user" "$zipball" || :
+
+ string_response 200 OK \
+ "package created: $author/$pname@$version" \
+ text/plain
+
+ exit
+ ;;
+ 'GET /all-packages'|'POST /all-packages')
+
+ response=$(mktemp -t htgen.$$.elm-packages-proxy.all-packages.XXXXXXXX)
+ trap "rm $response >&2" EXIT
+
+ {
+ # upstream packages
+ curl -fsS https://package.elm-lang.org"$Request_URI"
- # TODO only show newest?
- my_packages=$(
- cd ${cfg.packageDir}
- find -mindepth 3 -maxdepth 3 |
+ # private packages
+ (cd ${cfg.packageDir}; find -mindepth 3 -maxdepth 3) |
jq -Rs '
split("\n") |
map(
select(.!="") |
- sub("^\\./(?<author>[^/]+)/(?<pname>[^/]+)/(?<version>[^/]+)$";"\(.author)/\(.pname)@\(.version)")
+ match("^\\./(?<author>[^/]+)/(?<pname>[^/]+)/(?<version>[^/]+)$").captures |
+ map({key:.name,value:.string}) |
+ from_entries
+ ) |
+ reduce .[] as $item ({};
+ ($item|"\(.author)/\(.pname)") as $name |
+ . + { "\($name)": ((.[$name] // []) + [$item.version]) }
)
'
- )
+ } |
+ jq -cs add > $response
+
+ file_response 200 OK "$response" 'application/json; charset=UTF-8'
+ exit
+ ;;
+ 'GET /all-packages/since/'*|'POST /all-packages/since/'*)
+
+ response=$(mktemp -t htgen.$$.elm-packages-proxy.all-packages.XXXXXXXX)
+ trap "rm $response >&2" EXIT
- new_upstream_packages=$(
+ {
+ # upstream packages
curl -fsS https://package.elm-lang.org"$Request_URI"
- )
- response=$(
- jq -n \
- --argjson my_packages "$my_packages" \
- --argjson new_upstream_packages "$new_upstream_packages" \
- '$new_upstream_packages + $my_packages'
- )
+ # private packages
+ (cd ${cfg.packageDir}; find -mindepth 3 -maxdepth 3) |
+ jq -Rs '
+ split("\n") |
+ map(
+ select(.!="") |
+ sub("^\\./(?<author>[^/]+)/(?<pname>[^/]+)/(?<version>[^/]+)$";"\(.author)/\(.pname)@\(.version)")
+ ) |
+ sort_by(split("@") | [.[0]]+(.[1]|split("."))) |
+ reverse
+ '
+ } |
+ jq -cs add > $response
- string_response 200 OK "$response" 'application/json; charset=UTF-8'
+ file_response 200 OK "$response" 'application/json; charset=UTF-8'
exit
;;
esac
- ''})'';
+ ''}'';
};
}