From e68e791eafe390913cf0d363954c28dc98b52c48 Mon Sep 17 00:00:00 2001 From: whiplash Date: Sun, 8 Dec 2024 20:48:22 +0100 Subject: [PATCH] [Gleam]: first attempt to add Gleam. (#8016) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Gleam]: first attempt to add Gleam. * Update gleam/tmist/gleam.toml Co-authored-by: Marwan Rabbâa --------- Co-authored-by: whiplash Co-authored-by: Marwan Rabbâa --- gleam/Dockerfile | 18 ++++++++++ gleam/tmist/.gitignore | 4 +++ gleam/tmist/README.md | 3 ++ gleam/tmist/config.yaml | 7 ++++ gleam/tmist/gleam.toml | 23 ++++++++++++ gleam/tmist/manifest.toml | 27 ++++++++++++++ gleam/tmist/src/tmist.gleam | 72 +++++++++++++++++++++++++++++++++++++ 7 files changed, 154 insertions(+) create mode 100644 gleam/Dockerfile create mode 100644 gleam/tmist/.gitignore create mode 100644 gleam/tmist/README.md create mode 100644 gleam/tmist/config.yaml create mode 100644 gleam/tmist/gleam.toml create mode 100644 gleam/tmist/manifest.toml create mode 100644 gleam/tmist/src/tmist.gleam diff --git a/gleam/Dockerfile b/gleam/Dockerfile new file mode 100644 index 00000000000..19f34ae0efa --- /dev/null +++ b/gleam/Dockerfile @@ -0,0 +1,18 @@ +{{#language.image}} + FROM gleamlang/gleam +{{/language.image}} +{{^language.image}} + FROM +{{/language.image}} + +WORKDIR /usr/src/app + +{{#files}} +COPY '{{source}}' '{{target}}' +{{/files}} + +RUN gleam build + +ENTRYPOINT {{command}} + +HEALTHCHECK CMD curl --fail http://0.0.0.0:3000 || exit 1 diff --git a/gleam/tmist/.gitignore b/gleam/tmist/.gitignore new file mode 100644 index 00000000000..599be4eb929 --- /dev/null +++ b/gleam/tmist/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/gleam/tmist/README.md b/gleam/tmist/README.md new file mode 100644 index 00000000000..a600a42c04a --- /dev/null +++ b/gleam/tmist/README.md @@ -0,0 +1,3 @@ +```sh +gleam run # Run the project +``` diff --git a/gleam/tmist/config.yaml b/gleam/tmist/config.yaml new file mode 100644 index 00000000000..a1a29945175 --- /dev/null +++ b/gleam/tmist/config.yaml @@ -0,0 +1,7 @@ +framework: + website: https://github.com/rawhat/mist + version: "3.0.0" + files: + - "**/*.gleam" + - "gleam.toml" + - "manifest.toml" diff --git a/gleam/tmist/gleam.toml b/gleam/tmist/gleam.toml new file mode 100644 index 00000000000..f45f1272b83 --- /dev/null +++ b/gleam/tmist/gleam.toml @@ -0,0 +1,23 @@ +name = "tmist" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 0.34.0 and < 2.0.0" +mist = ">= 3.0.0 and < 3.1.0" +gleam_erlang = ">= 0.32.0 and < 1.0.0" +gleam_http = ">= 3.7.1 and < 4.0.0" +gleam_otp = ">= 0.14.1 and < 1.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/gleam/tmist/manifest.toml b/gleam/tmist/manifest.toml new file mode 100644 index 00000000000..7711504aaf7 --- /dev/null +++ b/gleam/tmist/manifest.toml @@ -0,0 +1,27 @@ +# 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.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" }, + { name = "gleam_erlang", version = "0.32.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "B18643083A0117AC5CFD0C1AEEBE5469071895ECFA426DCC26517A07F6AD9948" }, + { name = "gleam_http", version = "3.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "A9EE0722106FCCAB8AD3BF9D0A3EFF92BFE8561D59B83BAE96EB0BE1938D4E0F" }, + { name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" }, + { name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" }, + { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, + { name = "glisten", version = "6.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "912132751031473CB38F454120124FFC96AF6B0EA33D92C9C90DB16327A2A972" }, + { 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 = "3.0.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 = "CDA1A74E768419235E16886463EC4722EFF4AB3F8D820A76EAD45D7C167D7282" }, + { name = "ranger", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "B8F3AFF23A3A5B5D9526B8D18E7C43A7DFD3902B151B97EC65397FE29192B695" }, + { name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" }, +] + +[requirements] +gleam_erlang = { version = ">= 0.32.0 and < 1.0.0" } +gleam_http = { version = ">= 3.7.1 and < 4.0.0" } +gleam_otp = { version = ">= 0.14.1 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 = ">= 3.0.0 and < 4.0.0" } diff --git a/gleam/tmist/src/tmist.gleam b/gleam/tmist/src/tmist.gleam new file mode 100644 index 00000000000..76cb2c4a217 --- /dev/null +++ b/gleam/tmist/src/tmist.gleam @@ -0,0 +1,72 @@ +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() { + 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) { + [] -> index(req) + ["user", name] -> handle_user(req, name) + _ -> not_found + } + } + |> mist.new + |> mist.port(3000) + |> mist.start_http + + process.sleep_forever() +} + +pub type MyMessage { + Broadcast(String) +} + +fn index(request: Request(Connection)) -> Response(ResponseData) { + let content_type = + request + |> request.get_header("content-type") + |> result.unwrap("text/plain") + + response.new(200) + |> response.set_body(mist.Bytes(bytes_builder.new())) +} + +fn handle_user( + request: Request(Connection), + name: String, +) -> Response(ResponseData) { + let content_type = + request + |> request.get_header("content-type") + |> result.unwrap("text/plain") + + case request.method { + http.Post -> + response.new(200) + |> response.set_body(mist.Bytes(bytes_builder.new())) + http.Get -> { + response.new(200) + |> response.set_body(mist.Bytes(bytes_builder.from_string(name))) + } + _other -> + response.new(404) + |> response.set_body(mist.Bytes(bytes_builder.new())) + } +}