From 60b07ed44602532f85478c4827bb233513782fc4 Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Mon, 22 Jul 2024 21:18:54 -0400 Subject: [PATCH] Outline simple mist project --- gleam.toml | 4 ++++ manifest.toml | 26 +++++++++++++++++++++ src/webtest.gleam | 59 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 manifest.toml diff --git a/gleam.toml b/gleam.toml index 5c49e6a..98aedcb 100644 --- a/gleam.toml +++ b/gleam.toml @@ -14,6 +14,10 @@ version = "1.0.0" [dependencies] gleam_stdlib = ">= 0.34.0 and < 2.0.0" +gleam_http = ">= 3.6.0 and < 4.0.0" +mist = ">= 1.2.0 and < 2.0.0" +gleam_otp = ">= 0.10.0 and < 1.0.0" +gleam_erlang = ">= 0.25.0 and < 1.0.0" [dev-dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..63f8030 --- /dev/null +++ b/manifest.toml @@ -0,0 +1,26 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, + { name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" }, + { name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" }, + { name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" }, + { name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" }, + { name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" }, + { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, + { name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" }, + { name = "gramps", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "3CCAA6E081225180D95C79679D383BBF51C8D1FDC1B84DA1DA444F628C373793" }, + { name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" }, + { name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" }, + { name = "mist", version = "1.2.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "109B4D64E68C104CC23BB3CC5441ECD479DD7444889DA01113B75C6AF0F0E17B" }, + { name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" }, +] + +[requirements] +gleam_erlang = { version = ">= 0.25.0 and < 1.0.0"} +gleam_http = { version = ">= 3.6.0 and < 4.0.0"} +gleam_otp = { version = ">= 0.10.0 and < 1.0.0"} +gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +mist = { version = ">= 1.2.0 and < 2.0.0"} diff --git a/src/webtest.gleam b/src/webtest.gleam index 0657c0a..e2d3528 100644 --- a/src/webtest.gleam +++ b/src/webtest.gleam @@ -1,5 +1,62 @@ +import gleam/bytes_builder +import gleam/erlang/process +import gleam/http +import gleam/http/request.{type Request} +import gleam/http/response.{type Response} import gleam/io +import gleam/iterator +import gleam/option.{None, Some} +import gleam/otp/actor +import gleam/result +import gleam/string +import mist.{type Connection, type ResponseData} pub fn main() { - io.println("Hello from webtest!") + let selector = process.new_selector() + let state = Nil + + let not_found = + response.new(404) + |> response.set_body(mist.Bytes(bytes_builder.new())) + + let assert Ok(_) = + fn(req: Request(Connection)) -> Response(ResponseData) { + case request.path_segments(req) { + ["ws"] -> + mist.websocket( + request: req, + on_init: fn(_conn) { #(state, Some(selector)) }, + on_close: fn(_state) { io.println("Goodbye!") }, + handler: handle_we_message, + ) + _ -> not_found + } + } + |> mist.new + |> mist.port(3000) + |> mist.start_http + + process.sleep_forever() +} + +pub type MyMessage { + Broadcast(String) +} + +fn handle_we_message(state, conn, message) { + case message { + mist.Text("ping") -> { + let assert Ok(_) = mist.send_text_frame(conn, "pong") + actor.continue(state) + } + mist.Text(_) | mist.Binary(_) -> { + actor.continue(state) + } + mist.Custom(Broadcast(text)) -> { + let assert Ok(_) = mist.send_text_frame(conn, text) + actor.continue(state) + } + + mist.Closed | mist.Shutdown -> actor.Stop(process.Normal) + } }