diff options
author | tv <tv@krebsco.de> | 2021-08-31 19:48:19 +0200 |
---|---|---|
committer | tv <tv@krebsco.de> | 2021-08-31 19:48:19 +0200 |
commit | f35e7c588467a6e0f3589857ecf163a88fb5858c (patch) | |
tree | e478a5b7a9286edb36a8753a82b80e91394ccbca /tv/2configs | |
parent | 7b618e1e0883b9ea05c73b5e88d2adc6d144cae6 (diff) |
tv elm-package-proxy: add search.json
Diffstat (limited to 'tv/2configs')
-rw-r--r-- | tv/2configs/elm-packages-proxy.nix | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tv/2configs/elm-packages-proxy.nix b/tv/2configs/elm-packages-proxy.nix index 097d706..265289b 100644 --- a/tv/2configs/elm-packages-proxy.nix +++ b/tv/2configs/elm-packages-proxy.nix @@ -60,6 +60,11 @@ in { proxy_pass $new_uri; ''; + + locations."/search.json".extraConfig = '' + proxy_pass http://127.0.0.1:${toString config.krebs.htgen.elm-packages-proxy.port}; + proxy_pass_header Server; + ''; }; krebs.htgen.elm-packages-proxy = { @@ -248,6 +253,76 @@ in { file_response 200 OK "$response" 'application/json; charset=UTF-8' exit ;; + 'GET /search.json') + + searchjson=$HOME/cache/search.json + mkdir -p "$HOME/cache" + + # update cached search.json + ( + last_modified=$( + if test -f "$searchjson"; then + date -Rr "$searchjson" + else + date -R -d @0 + fi + ) + tempsearchjson=$(mktemp "$searchjson.XXXXXXXX") + trap 'rm "$tempsearchjson" >&2' EXIT + curl -fsS --compressed https://package.elm-lang.org/search.json \ + -H "If-Modified-Since: $last_modified" \ + -o "$tempsearchjson" + if test -s "$tempsearchjson"; then + mv "$tempsearchjson" "$searchjson" + trap - EXIT + fi + ) + + response=$(mktemp -t htgen.$$.elm-packages-proxy.search.XXXXXXXX) + trap 'rm "$response" >&2' EXIT + + { + printf '{"upstream":'; cat "$searchjson" + printf ',"private":'; (cd ${cfg.packageDir}; find -mindepth 3 -maxdepth 3) | + jq -Rs ' + split("\n") | + map( + select(.!="") | + match("^\\./(?<author>[^/]+)/(?<pname>[^/]+)/(?<version>[^/]+)$").captures | + map({key:.name,value:.string}) | + from_entries + ) | + map({ + key: "\(.author)/\(.pname)", + value: .version, + }) | + from_entries + ' + printf '}' + } | + jq -c ' + reduce .upstream[] as $upstreamItem ({ private, output: [] }; + .private[$upstreamItem.name] as $privateItem | + if $privateItem then + .output += [$upstreamItem * { version: $privateItem.version }] | + .private |= del(.[$upstreamItem.name]) + else + .output += [$upstreamItem] + end + ) | + + .output + (.private | to_entries | sort_by(.key) | map({ + name: .key, + version: .value, + summary: "dummy summary", + license: "dummy license", + })) + ' \ + > $response + + file_response 200 OK "$response" 'application/json; charset=UTF-8' + exit + ;; esac ''}''; }; |