Skip to content

Commit

Permalink
Merge pull request #6 from codecrafters-io/add-gleam-support
Browse files Browse the repository at this point in the history
Add Gleam support
  • Loading branch information
rohitpaulk authored Sep 18, 2024
2 parents 0501646 + 68832f0 commit b3bd328
Show file tree
Hide file tree
Showing 31 changed files with 477 additions and 1 deletion.
11 changes: 11 additions & 0 deletions compiled_starters/gleam/.codecrafters/compile.sh
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
11 changes: 11 additions & 0 deletions compiled_starters/gleam/.codecrafters/run.sh
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 -- "$@"
1 change: 1 addition & 0 deletions compiled_starters/gleam/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions compiled_starters/gleam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
34 changes: 34 additions & 0 deletions compiled_starters/gleam/README.md
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.
11 changes: 11 additions & 0 deletions compiled_starters/gleam/codecrafters.yml
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
14 changes: 14 additions & 0 deletions compiled_starters/gleam/gleam.toml
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"
17 changes: 17 additions & 0 deletions compiled_starters/gleam/manifest.toml
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" }
29 changes: 29 additions & 0 deletions compiled_starters/gleam/src/main.gleam
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()
}
24 changes: 24 additions & 0 deletions compiled_starters/gleam/your_program.sh
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 -- "$@"
3 changes: 2 additions & 1 deletion course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ short_description_md: |-
completion_percentage: 15

languages:
- slug: "gleam"
- slug: "go"
- slug: "javascript"
- slug: "python"
- slug: "rust"
- slug: "javascript"

marketing:
difficulty: medium
Expand Down
17 changes: 17 additions & 0 deletions dockerfiles/gleam-1.4.Dockerfile
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
11 changes: 11 additions & 0 deletions solutions/gleam/01-vi6/code/.codecrafters/compile.sh
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
11 changes: 11 additions & 0 deletions solutions/gleam/01-vi6/code/.codecrafters/run.sh
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 -- "$@"
1 change: 1 addition & 0 deletions solutions/gleam/01-vi6/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions solutions/gleam/01-vi6/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
34 changes: 34 additions & 0 deletions solutions/gleam/01-vi6/code/README.md
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.
11 changes: 11 additions & 0 deletions solutions/gleam/01-vi6/code/codecrafters.yml
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
14 changes: 14 additions & 0 deletions solutions/gleam/01-vi6/code/gleam.toml
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"
17 changes: 17 additions & 0 deletions solutions/gleam/01-vi6/code/manifest.toml
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" }
24 changes: 24 additions & 0 deletions solutions/gleam/01-vi6/code/src/main.gleam
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()
}
24 changes: 24 additions & 0 deletions solutions/gleam/01-vi6/code/your_program.sh
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 -- "$@"
38 changes: 38 additions & 0 deletions solutions/gleam/01-vi6/diff/src/main.gleam.diff
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
Loading

0 comments on commit b3bd328

Please sign in to comment.