Skip to content

Commit

Permalink
Add initial Gleam project structure and scripts for CodeCrafters chal…
Browse files Browse the repository at this point in the history
…lenge.
  • Loading branch information
rohitpaulk committed Sep 17, 2024
1 parent 0501646 commit d7c2055
Show file tree
Hide file tree
Showing 30 changed files with 448 additions and 0 deletions.
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
11 changes: 11 additions & 0 deletions compiled_starters/gleam/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"

[dev-dependencies]
gleeunit = "~> 1.0"
11 changes: 11 additions & 0 deletions compiled_starters/gleam/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ 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" },
]

[requirements]
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.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 -- "$@"
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
11 changes: 11 additions & 0 deletions solutions/gleam/01-vi6/code/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"

[dev-dependencies]
gleeunit = "~> 1.0"
11 changes: 11 additions & 0 deletions solutions/gleam/01-vi6/code/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ 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" },
]

[requirements]
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.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
24 changes: 24 additions & 0 deletions solutions/gleam/01-vi6/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
The entry point for your Kafka implementation is in `src/main.gleam`.

Study and uncomment the relevant code:

```gleam
// 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()
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
Loading

0 comments on commit d7c2055

Please sign in to comment.