// A download page for map tiles shared over BitTorrent: the .torrent read, a magnet link made, and a torrent made for a new file.
//
//   quillo run bencode.qwe      then open http://localhost:8080/torrent

use "bencode.qwer"

route GET /torrent {
    let t = bencode_torrent_info("bencode_sample.torrent")?
    { name: t.name, info_hash: t.info_hash, size: t.size, pieces: t.pieces, files: t.files, trackers: t.trackers, magnet: t.magnet }
}

// A torrent for a file of our own, read back.
route GET /make {
    write_file("data/notes.txt", join(for i in range(1000) => "hello quillo\n", ""))
    let hash = bencode_torrent_create("data/notes.txt", "https://tracker.example.org/announce", 16, "data/notes.torrent")?
    let back = bencode_torrent_info("data/notes.torrent")?
    { hash: hash, same: hash == back.info_hash, size: back.size, magnet: back.magnet }
}

// Bencode itself: what a tracker answers, encoded and decoded.
route GET /values {
    let b64 = bencode_encode({ interval: 1800, peers: [{ ip: "10.0.0.7", port: 6881 }], "failure reason": "none" })?
    { base64: b64, back: bencode_decode(b64)?, binary: bencode_decode(bencode_encode({ "$base64": "/wA=" })?)? }
}
