generated from codecrafters-io/course-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from codecrafters-io/add-gleam-support
Add Gleam support
- Loading branch information
Showing
31 changed files
with
477 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to compile your program on CodeCrafters | ||
# | ||
# This runs before .codecrafters/run.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
gleam build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to run your program on CodeCrafters | ||
# | ||
# This runs after .codecrafters/compile.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
exec gleam run --module main -- "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) | ||
|
||
This is a starting point for Gleam solutions to the | ||
["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). | ||
|
||
In this challenge, you'll build a toy Kafka clone that's capable of accepting | ||
and responding to APIVersions & Fetch API requests. You'll also learn about | ||
encoding and decoding messages using the Kafka wire protocol. You'll also learn | ||
about handling the network protocol, event loops, TCP sockets and more. | ||
|
||
**Note**: If you're viewing this repo on GitHub, head over to | ||
[codecrafters.io](https://codecrafters.io) to try the challenge. | ||
|
||
# Passing the first stage | ||
|
||
The entry point for your Kafka implementation is in `src/main.gleam`. Study and | ||
uncomment the relevant code, and push your changes to pass the first stage: | ||
|
||
```sh | ||
git commit -am "pass 1st stage" # any msg | ||
git push origin master | ||
``` | ||
|
||
That's all! | ||
|
||
# Stage 2 & beyond | ||
|
||
Note: This section is for stages 2 and beyond. | ||
|
||
1. Ensure you have `gleam (1.0+)` installed locally | ||
1. Run `./your_program.sh` to run your Kafka broker, which is implemented in | ||
`src/main.gleam`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Set this to true if you want debug logs. | ||
# | ||
# These can be VERY verbose, so we suggest turning them off | ||
# unless you really need them. | ||
debug: false | ||
|
||
# Use this to change the Gleam version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: gleam-1.4 | ||
language_pack: gleam-1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name = "codecrafters_kafka" | ||
version = "1.0.0" | ||
|
||
# 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 or ~> 1.0" | ||
gleam_erlang = "~> 0.25" | ||
gleam_otp = "~> 0.10" | ||
glisten = "~> 2.0" | ||
|
||
[dev-dependencies] | ||
gleeunit = "~> 1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" }, | ||
{ name = "gleam_otp", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "CD5FC777E99673BDB390092DF85E34EAA6B8EE1882147496290AB3F45A4960B1" }, | ||
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" }, | ||
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" }, | ||
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" }, | ||
] | ||
|
||
[requirements] | ||
gleam_erlang = { version = "~> 0.25" } | ||
gleam_otp = { version = "~> 0.10" } | ||
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } | ||
gleeunit = { version = "~> 1.0" } | ||
glisten = { version = "~> 2.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import gleam/io | ||
|
||
import gleam/erlang/process | ||
import gleam/option.{None} | ||
import gleam/otp/actor | ||
import glisten | ||
|
||
pub fn main() { | ||
// Ensures gleam doesn't complain about unused imports in stage 1 (feel free to remove this!) | ||
let _ = glisten.handler | ||
let _ = glisten.serve | ||
let _ = process.sleep_forever | ||
let _ = actor.continue | ||
let _ = None | ||
|
||
// You can use print statements as follows for debugging, they'll be visible when running tests. | ||
io.println("Logs from your program will appear here!") | ||
|
||
// Uncomment this block to pass the first stage | ||
// | ||
// let assert Ok(_) = | ||
// glisten.handler(fn(_conn) { #(Nil, None) }, fn(_msg, state, _conn) { | ||
// io.println("Received message!") | ||
// actor.continue(state) | ||
// }) | ||
// |> glisten.serve(9092) | ||
// | ||
// process.sleep_forever() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
# | ||
# Use this script to run your program LOCALLY. | ||
# | ||
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit early if any commands fail | ||
|
||
# Copied from .codecrafters/compile.sh | ||
# | ||
# - Edit this to change how your program compiles locally | ||
# - Edit .codecrafters/compile.sh to change how your program compiles remotely | ||
( | ||
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory | ||
gleam build | ||
) | ||
|
||
# Copied from .codecrafters/run.sh | ||
# | ||
# - Edit this to change how your program runs locally | ||
# - Edit .codecrafters/run.sh to change how your program runs remotely | ||
exec gleam run --module main -- "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# syntax=docker/dockerfile:1.7-labs | ||
FROM ghcr.io/gleam-lang/gleam:v1.4.1-erlang-alpine | ||
|
||
# Rebuild if gleam.toml or manifest.toml change | ||
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="gleam.toml,manifest.toml" | ||
|
||
WORKDIR /app | ||
|
||
# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses | ||
COPY --exclude=.git --exclude=README.md . /app | ||
|
||
# Force deps to be downloaded | ||
RUN gleam build | ||
|
||
# Cache build directory | ||
RUN mkdir -p /app-cached | ||
RUN mv build /app-cached/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to compile your program on CodeCrafters | ||
# | ||
# This runs before .codecrafters/run.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
gleam build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
# | ||
# This script is used to run your program on CodeCrafters | ||
# | ||
# This runs after .codecrafters/compile.sh | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit on failure | ||
|
||
exec gleam run --module main -- "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) | ||
|
||
This is a starting point for Gleam solutions to the | ||
["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). | ||
|
||
In this challenge, you'll build a toy Kafka clone that's capable of accepting | ||
and responding to APIVersions & Fetch API requests. You'll also learn about | ||
encoding and decoding messages using the Kafka wire protocol. You'll also learn | ||
about handling the network protocol, event loops, TCP sockets and more. | ||
|
||
**Note**: If you're viewing this repo on GitHub, head over to | ||
[codecrafters.io](https://codecrafters.io) to try the challenge. | ||
|
||
# Passing the first stage | ||
|
||
The entry point for your Kafka implementation is in `src/main.gleam`. Study and | ||
uncomment the relevant code, and push your changes to pass the first stage: | ||
|
||
```sh | ||
git commit -am "pass 1st stage" # any msg | ||
git push origin master | ||
``` | ||
|
||
That's all! | ||
|
||
# Stage 2 & beyond | ||
|
||
Note: This section is for stages 2 and beyond. | ||
|
||
1. Ensure you have `gleam (1.0+)` installed locally | ||
1. Run `./your_program.sh` to run your Kafka broker, which is implemented in | ||
`src/main.gleam`. | ||
1. Commit your changes and run `git push origin master` to submit your solution | ||
to CodeCrafters. Test output will be streamed to your terminal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Set this to true if you want debug logs. | ||
# | ||
# These can be VERY verbose, so we suggest turning them off | ||
# unless you really need them. | ||
debug: false | ||
|
||
# Use this to change the Gleam version used to run your code | ||
# on Codecrafters. | ||
# | ||
# Available versions: gleam-1.4 | ||
language_pack: gleam-1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name = "codecrafters_kafka" | ||
version = "1.0.0" | ||
|
||
# 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 or ~> 1.0" | ||
gleam_erlang = "~> 0.25" | ||
gleam_otp = "~> 0.10" | ||
glisten = "~> 2.0" | ||
|
||
[dev-dependencies] | ||
gleeunit = "~> 1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" }, | ||
{ name = "gleam_otp", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "CD5FC777E99673BDB390092DF85E34EAA6B8EE1882147496290AB3F45A4960B1" }, | ||
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" }, | ||
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" }, | ||
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" }, | ||
] | ||
|
||
[requirements] | ||
gleam_erlang = { version = "~> 0.25" } | ||
gleam_otp = { version = "~> 0.10" } | ||
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } | ||
gleeunit = { version = "~> 1.0" } | ||
glisten = { version = "~> 2.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import gleam/io | ||
|
||
import gleam/erlang/process | ||
import gleam/option.{None} | ||
import gleam/otp/actor | ||
import glisten | ||
|
||
pub fn main() { | ||
// Ensures gleam doesn't complain about unused imports in stage 1 (feel free to remove this!) | ||
let _ = glisten.handler | ||
let _ = glisten.serve | ||
let _ = process.sleep_forever | ||
let _ = actor.continue | ||
let _ = None | ||
|
||
let assert Ok(_) = | ||
glisten.handler(fn(_conn) { #(Nil, None) }, fn(_msg, state, _conn) { | ||
io.println("Received message!") | ||
actor.continue(state) | ||
}) | ||
|> glisten.serve(9092) | ||
|
||
process.sleep_forever() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
# | ||
# Use this script to run your program LOCALLY. | ||
# | ||
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. | ||
# | ||
# Learn more: https://codecrafters.io/program-interface | ||
|
||
set -e # Exit early if any commands fail | ||
|
||
# Copied from .codecrafters/compile.sh | ||
# | ||
# - Edit this to change how your program compiles locally | ||
# - Edit .codecrafters/compile.sh to change how your program compiles remotely | ||
( | ||
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory | ||
gleam build | ||
) | ||
|
||
# Copied from .codecrafters/run.sh | ||
# | ||
# - Edit this to change how your program runs locally | ||
# - Edit .codecrafters/run.sh to change how your program runs remotely | ||
exec gleam run --module main -- "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@@ -1,29 +1,24 @@ | ||
import gleam/io | ||
|
||
import gleam/erlang/process | ||
import gleam/option.{None} | ||
import gleam/otp/actor | ||
import glisten | ||
|
||
pub fn main() { | ||
// Ensures gleam doesn't complain about unused imports in stage 1 (feel free to remove this!) | ||
let _ = glisten.handler | ||
let _ = glisten.serve | ||
let _ = process.sleep_forever | ||
let _ = actor.continue | ||
let _ = None | ||
|
||
- // You can use print statements as follows for debugging, they'll be visible when running tests. | ||
- io.println("Logs from your program will appear here!") | ||
+ let assert Ok(_) = | ||
+ glisten.handler(fn(_conn) { #(Nil, None) }, fn(_msg, state, _conn) { | ||
+ io.println("Received message!") | ||
+ actor.continue(state) | ||
+ }) | ||
+ |> glisten.serve(9092) | ||
|
||
- // Uncomment this block to pass the first stage | ||
- // | ||
- // let assert Ok(_) = | ||
- // glisten.handler(fn(_conn) { #(Nil, None) }, fn(_msg, state, _conn) { | ||
- // io.println("Received message!") | ||
- // actor.continue(state) | ||
- // }) | ||
- // |> glisten.serve(9092) | ||
- // | ||
- // process.sleep_forever() | ||
+ process.sleep_forever() | ||
} | ||
\ No newline at end of file |
Oops, something went wrong.