import gleam/bytes_tree import gleam/crypto import gleam/erlang/process import gleam/http/request import gleam/http/response import gleam/httpc import gleam/uri import midas/effect as e import mist import shellout pub fn run(effect) { case effect { e.Done(value) -> value e.Fetch(request:, resume:) -> { let reply = case httpc.send_bits(request) { Ok(response) -> Ok(response) Error(reason) -> { let reason = case reason { httpc.FailedToConnect(..) -> e.NetworkError("Failed to connect") httpc.ResponseTimeout -> e.NetworkError("Response timeout") httpc.InvalidUtf8Response -> e.UnableToReadBody } Error(reason) } } run(resume(reply)) } e.Follow(uri:, resume:) -> run(resume(do_follow(uri))) e.StrongRandom(length:, resume:) -> { run(resume(Ok(crypto.strong_random_bytes(length)))) } e.Hash(bytes:, algorithm:, resume:) -> { let algorithm = case algorithm { e.Sha256 -> crypto.Sha256 _ -> panic as "unsupported" } run(resume(Ok(crypto.hash(algorithm, bytes)))) } _ -> { panic as "unsupported effect" } } } fn do_follow(uri) { let self = process.new_subject() let assert Ok(_) = mist.new(handle(self, _)) |> mist.port(8080) |> mist.start() let assert Ok(_) = shellout.command(run: "open", with: [uri.to_string(uri)], in: ".", opt: []) let url = process.receive_forever(self) Ok(url) } fn handle(parent, request) { process.send(parent, request.to_uri(request)) response.new(200) |> response.set_body(mist.Bytes(bytes_tree.from_string("hi"))) }