Skip to content

Commit

Permalink
Update for Gleam v1
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Mar 29, 2024
1 parent 9100235 commit 94f29a8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.0.0
- uses: erlef/setup-beam@v1.15.2
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "25.0"
gleam-version: "0.25.3"
otp-version: "26.0"
gleam-version: "1.0.0"
rebar3-version: "3"
- run: gleam format --check src test
- run: gleam test
- run: gleam format --check src test
6 changes: 3 additions & 3 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "echo"
name = "reply"
version = "1.0.0"
licences = ["Apache-2.0"]
description = "A tiny echo server written in Gleam!"
Expand All @@ -10,10 +10,10 @@ links = [
]

[dependencies]
gleam_stdlib = "~> 0.25"
gleam_stdlib = "~> 0.25 or ~> 1.0"
gleam_http = "~> 3.0"
gleam_elli = "~> 2.0"
gleam_erlang = "~> 0.17"

[dev-dependencies]
gleeunit = "~> 0.1"
gleeunit = "~> 1.0"
22 changes: 11 additions & 11 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

packages = [
{ name = "elli", version = "3.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "elli", source = "hex", outer_checksum = "698B13B33D05661DB9FE7EFCBA41B84825A379CCE86E486CF6AFF9285BE0CCF8" },
{ name = "gleam_elli", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_otp", "elli", "gleam_http"], otp_app = "gleam_elli", source = "hex", outer_checksum = "5DB2D8F83DF2A7384C5F381CCC837042EFB00272E9E5A01A071CC7600E2D6978" },
{ name = "gleam_erlang", version = "0.17.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "BAAA84F5BCC4477E809BA3E03BB3009A3894A6544C1511626C44408E39DB2AE6" },
{ name = "gleam_http", version = "3.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "B66B7A1539CCB577119E4DC80DD3484C1A652CB032967954498EEDBAE3355763" },
{ name = "gleam_otp", version = "0.5.2", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "24B88BF1D5B8DEC2525C00ECB65B96D2FD4DC66D8B2BB4D7AD4D12B2CE2A9988" },
{ name = "gleam_stdlib", version = "0.25.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "AD0F89928E0B919C8F8EDF640484633B28DBF88630A9E6AE504617A3E3E5B9A2" },
{ name = "gleeunit", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A1170754BF54F5DD6E9EF392FB1DC612528B007CCBE41B52F0C5453254708490" },
{ name = "gleam_elli", version = "2.4.0", build_tools = ["gleam"], requirements = ["elli", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib"], otp_app = "gleam_elli", source = "hex", outer_checksum = "433F5AF4ED92C55F3EBA8942610E974254EEF90F484AF26E3D775E33338DE832" },
{ 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.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
]

[requirements]
gleam_elli = "~> 2.0"
gleam_erlang = "~> 0.17"
gleam_http = "~> 3.0"
gleam_stdlib = "~> 0.25"
gleeunit = "~> 0.1"
gleam_elli = { version = "~> 2.0" }
gleam_erlang = { version = "~> 0.17" }
gleam_http = { version = "~> 3.0" }
gleam_stdlib = { version = "~> 0.25 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
14 changes: 7 additions & 7 deletions src/echo.gleam → src/reply.gleam
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import echo/web
import gleam/io
import gleam/int
import gleam/string
import gleam/result
import gleam/erlang/process
import gleam/erlang/os
import gleam/erlang/process
import gleam/http/elli
import gleam/int
import gleam/io
import gleam/result
import gleam/string
import reply/web

pub fn main() {
let port =
Expand All @@ -14,7 +14,7 @@ pub fn main() {
|> result.unwrap(3000)

// Start the web server process
assert Ok(_) =
let assert Ok(_) =
web.stack()
|> elli.start(on_port: port)

Expand Down
22 changes: 11 additions & 11 deletions src/echo/web.gleam → src/reply/web.gleam
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import gleam/bit_builder
import gleam/bit_string
import gleam/result
import gleam/string
import gleam/bit_array
import gleam/bytes_builder
import gleam/http.{Get, Post}
import gleam/http/service
import gleam/http/request
import gleam/http/response
import echo/web/logger
import gleam/http/service
import gleam/result
import gleam/string
import reply/web/logger

fn echo(request) {
fn reply(request) {
let content_type =
request
|> request.get_header("content-type")
Expand All @@ -22,7 +22,7 @@ fn echo(request) {
fn not_found() {
let body =
"There's nothing here. Try POSTing to /echo"
|> bit_string.from_string
|> bit_array.from_string

response.new(404)
|> response.set_body(body)
Expand All @@ -36,15 +36,15 @@ fn hello(name) {
}

response.new(200)
|> response.set_body(bit_string.from_string(reply))
|> response.set_body(bit_array.from_string(reply))
|> response.prepend_header("content-type", "text/plain")
}

pub fn service(request) {
let path = request.path_segments(request)

case request.method, path {
Post, ["echo"] -> echo(request)
Post, ["echo"] -> reply(request)
Get, ["hello", name] -> hello(name)
_, _ -> not_found()
}
Expand All @@ -53,6 +53,6 @@ pub fn service(request) {
pub fn stack() {
service
|> service.prepend_response_header("made-with", "Gleam")
|> service.map_response_body(bit_builder.from_bit_string)
|> service.map_response_body(bytes_builder.from_bit_array)
|> logger.middleware
}
6 changes: 3 additions & 3 deletions src/echo/web/logger.gleam → src/reply/web/logger.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/http
import gleam/http/request.{Request}
import gleam/http/response.{Response}
import gleam/http/service.{Service}
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import gleam/http/service.{type Service}
import gleam/int
import gleam/io
import gleam/string
Expand Down
32 changes: 16 additions & 16 deletions test/echo/web_test.gleam → test/reply/web_test.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import echo/web
import gleam/http.{Get, Post}
import gleam/http/request
import gleam/http/response
import reply/web

pub fn not_found_test() {
let resp =
Expand All @@ -11,8 +11,8 @@ pub fn not_found_test() {
|> request.set_body(<<>>)
|> web.service()

assert 404 = resp.status
assert <<"There's nothing here. Try POSTing to /echo":utf8>> = resp.body
let assert 404 = resp.status
let assert <<"There's nothing here. Try POSTing to /echo":utf8>> = resp.body
}

pub fn hello_nubi_test() {
Expand All @@ -23,8 +23,8 @@ pub fn hello_nubi_test() {
|> request.set_body(<<>>)
|> web.service()

assert 200 = resp.status
assert <<"Hello, Nubi!":utf8>> = resp.body
let assert 200 = resp.status
let assert <<"Hello, Nubi!":utf8>> = resp.body
}

pub fn hello_joe_test() {
Expand All @@ -35,8 +35,8 @@ pub fn hello_joe_test() {
|> request.set_body(<<>>)
|> web.service()

assert 200 = resp.status
assert <<"Hello, Joe!":utf8>> = resp.body
let assert 200 = resp.status
let assert <<"Hello, Joe!":utf8>> = resp.body
}

pub fn echo_1_test() {
Expand All @@ -48,9 +48,9 @@ pub fn echo_1_test() {
|> request.prepend_header("content-type", "application/octet-stream")
|> web.service()

assert 200 = resp.status
assert <<1, 2, 3, 4>> = resp.body
assert Ok("application/octet-stream") =
let assert 200 = resp.status
let assert <<1, 2, 3, 4>> = resp.body
let assert Ok("application/octet-stream") =
response.get_header(resp, "content-type")
}

Expand All @@ -63,9 +63,9 @@ pub fn echo_2_test() {
|> request.prepend_header("content-type", "text/plain")
|> web.service()

assert 200 = resp.status
assert <<"Hello, Gleam!":utf8>> = resp.body
assert Ok("text/plain") = response.get_header(resp, "content-type")
let assert 200 = resp.status
let assert <<"Hello, Gleam!":utf8>> = resp.body
let assert Ok("text/plain") = response.get_header(resp, "content-type")
}

pub fn echo_3_test() {
Expand All @@ -76,8 +76,8 @@ pub fn echo_3_test() {
|> request.set_body(<<"Hello, Gleam!":utf8>>)
|> web.service()

assert 200 = resp.status
assert <<"Hello, Gleam!":utf8>> = resp.body
assert Ok("application/octet-stream") =
let assert 200 = resp.status
let assert <<"Hello, Gleam!":utf8>> = resp.body
let assert Ok("application/octet-stream") =
response.get_header(resp, "content-type")
}
File renamed without changes.

0 comments on commit 94f29a8

Please sign in to comment.