01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use io;
use json;
use maps;
use task;
use convert;
import "web.j" as web;
import "log.j" as log;
def const REGISTRY as string init "registry.jennifer-lang.dev";
def const PAGE_SIZE as int init 25;
def struct Deck {
name as string,
version as string,
downloads as int
};
def enum Status {
Ready{since as string},
Building{percent as int},
Failed{reason as string}
};
func describe(s as Status) {
match ($s) {
when Ready(r) { return "ready since " + $r.since; }
when Building(b) { return "building " + convert.toString($b.percent) + "%"; }
when Failed(f) { return "failed: " + $f.reason; }
}
}
func encode(d as Deck) {
def out as json.Value init json.map();
$out = json.set($out, "/name", $d.name);
$out = json.set($out, "/version", $d.version);
$out = json.set($out, "/downloads", $d.downloads);
return $out;
}
func popular(d as Deck) {
return $d.downloads >= 1000;
}
func showDeck(ctx as web.Context) {
def name as string init web.param($ctx, "name");
if (not maps.has($catalog, $name)) {
web.sendJson($ctx, 404, json.map());
return;
}
web.sendJson($ctx, 200, encode($catalog[$name]));
}
def catalog as map of string to Deck init {};
$catalog["scheduler"] = Deck{name: "@jennifer/scheduler", version: "1.4.0", downloads: 4210};
$catalog["ledger"] = Deck{name: "@jennifer/ledger", version: "0.9.2", downloads: 1904};
def logger as log.Logger init log.new("info", "logfmt");
def fields as map of string to string init {"registry": REGISTRY};
log.info($logger, "catalog loaded", $fields);
def jobs as list of task of int init [];
for (def name in $catalog) {
def job as task of int init spawn {
return $catalog[$name].downloads;
};
$jobs[] = $job;
}
def total as int init 0;
for (def t in $jobs) {
$total = $total + task.wait($t);
}
def decks as int init len($catalog);
io.printf("{$total} downloads across {$decks} decks\n");
def app as web.App init web.new();
$app = web.get($app, "/decks/:name", showDeck);
web.run($app, "127.0.0.1:8080");