summaryrefslogtreecommitdiffstats
path: root/tv
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2021-08-31 19:48:19 +0200
committertv <tv@krebsco.de>2021-08-31 19:48:19 +0200
commite97a29678c097a9ea43bac340bec3fd3226001b5 (patch)
tree7cd4dc4949cd060e232a4355a639eaa1f67ebc5b /tv
parent1ac0608fc53214897703e4a163186a4586c73bb6 (diff)
tv elm-package-proxy: add search.json
Diffstat (limited to 'tv')
-rw-r--r--tv/2configs/elm-packages-proxy.nix75
1 files changed, 75 insertions, 0 deletions
diff --git a/tv/2configs/elm-packages-proxy.nix b/tv/2configs/elm-packages-proxy.nix
index 097d706c6..265289bbf 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
''}'';
};