Skip to content

Commit

Permalink
[Gleam]: first attempt to add Gleam. (#8016)
Browse files Browse the repository at this point in the history
* [Gleam]: first attempt to add Gleam.

* Update gleam/tmist/gleam.toml

Co-authored-by: Marwan Rabbâa <[email protected]>

---------

Co-authored-by: whiplash <[email protected]>
Co-authored-by: Marwan Rabbâa <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2024
1 parent 53d148e commit e68e791
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gleam/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions gleam/tmist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
3 changes: 3 additions & 0 deletions gleam/tmist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```sh
gleam run # Run the project
```
7 changes: 7 additions & 0 deletions gleam/tmist/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
framework:
website: https://github.com/rawhat/mist
version: "3.0.0"
files:
- "**/*.gleam"
- "gleam.toml"
- "manifest.toml"
23 changes: 23 additions & 0 deletions gleam/tmist/gleam.toml
Original file line number Diff line number Diff line change
@@ -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"
27 changes: 27 additions & 0 deletions gleam/tmist/manifest.toml
Original file line number Diff line number Diff line change
@@ -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" }
72 changes: 72 additions & 0 deletions gleam/tmist/src/tmist.gleam
Original file line number Diff line number Diff line change
@@ -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()))
}
}

0 comments on commit e68e791

Please sign in to comment.