diff --git a/compiled_starters/scala/.codecrafters/compile.sh b/compiled_starters/scala/.codecrafters/compile.sh new file mode 100755 index 0000000..7d1ab4f --- /dev/null +++ b/compiled_starters/scala/.codecrafters/compile.sh @@ -0,0 +1,12 @@ +#!/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 + +# The option is needed for Java 17+ to allow native memory access operations. +SBT_OPTS="--enable-native-access=ALL-UNNAMED" sbt assembly diff --git a/compiled_starters/scala/.codecrafters/run.sh b/compiled_starters/scala/.codecrafters/run.sh new file mode 100755 index 0000000..b36382c --- /dev/null +++ b/compiled_starters/scala/.codecrafters/run.sh @@ -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 java -jar "$(dirname "$0")/target/scala-3.7.4/http-server.jar" "$@" diff --git a/compiled_starters/scala/.gitattributes b/compiled_starters/scala/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/compiled_starters/scala/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/compiled_starters/scala/.gitignore b/compiled_starters/scala/.gitignore new file mode 100644 index 0000000..808a587 --- /dev/null +++ b/compiled_starters/scala/.gitignore @@ -0,0 +1,28 @@ +# macOS +.DS_Store + +# sbt specific +dist/* +debug/ +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ +project/local-plugins.sbt +.history +.ensime +.ensime_cache/ +.sbt-scripted/ +local.sbt + +# Bloop +.bsp + +# VS Code +.vscode/ + +# Metals +.bloop/ +.metals/ +metals.sbt \ No newline at end of file diff --git a/compiled_starters/scala/README.md b/compiled_starters/scala/README.md new file mode 100644 index 0000000..19d89f9 --- /dev/null +++ b/compiled_starters/scala/README.md @@ -0,0 +1,38 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png) + +This is a starting point for Scala solutions to the +["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview). + +[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the +protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server +that is capable of serving multiple clients. + +Along the way you'll learn about TCP servers, +[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html), +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 HTTP server implementation is in +`src/main/scala/codecrafters_http_server/App.scala`. 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 +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `sbt (1.11.7)` installed locally +1. Run `./your_program.sh` to run your program, which is implemented in + `src/main/scala/codecrafters_http_server/App.scala`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/compiled_starters/scala/build.sbt b/compiled_starters/scala/build.sbt new file mode 100644 index 0000000..2fc2851 --- /dev/null +++ b/compiled_starters/scala/build.sbt @@ -0,0 +1,14 @@ +ThisBuild / scalaVersion := "3.7.4" +ThisBuild / version := "0.1.0-SNAPSHOT" +ThisBuild / organization := "com.codecrafters" +ThisBuild / organizationName := "CodeCrafters" + +assembly / assemblyJarName := "http-server.jar" + +lazy val root = (project in file(".")) + .settings( + name := "codecrafter-http-server", + // List your dependencies here + libraryDependencies ++= Seq( + ) + ) diff --git a/compiled_starters/scala/codecrafters.yml b/compiled_starters/scala/codecrafters.yml new file mode 100644 index 0000000..a3644bc --- /dev/null +++ b/compiled_starters/scala/codecrafters.yml @@ -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 Scala version used to run your code +# on Codecrafters. +# +# Available versions: scala-3.7 +buildpack: scala-3.7 diff --git a/compiled_starters/scala/project/assembly.sbt b/compiled_starters/scala/project/assembly.sbt new file mode 100644 index 0000000..f8ea5d0 --- /dev/null +++ b/compiled_starters/scala/project/assembly.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") diff --git a/compiled_starters/scala/project/build.properties b/compiled_starters/scala/project/build.properties new file mode 100644 index 0000000..01a16ed --- /dev/null +++ b/compiled_starters/scala/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.11.7 diff --git a/compiled_starters/scala/src/main/scala/codecrafters_http_server/App.scala b/compiled_starters/scala/src/main/scala/codecrafters_http_server/App.scala new file mode 100644 index 0000000..37b058b --- /dev/null +++ b/compiled_starters/scala/src/main/scala/codecrafters_http_server/App.scala @@ -0,0 +1,25 @@ +package codecrafters_http_server + +import java.io.IOException +import java.net.ServerSocket + +object Main extends App { + // You can use print statements as follows for debugging, they'll be visible when running tests. + System.err.println("Logs from your program will appear here!") + + // TODO: Uncomment the code below to pass the first stage + // + // try { + // val serverSocket = new ServerSocket(4221) + // + // // Since the tester restarts your program quite often, setting SO_REUSEADDR + // // ensures that we don't run into 'Address already in use' errors + // serverSocket.setReuseAddress(true) + // + // serverSocket.accept() // Wait for connection from client. + // println("accepted new connection") + // } catch { + // case e: IOException => + // println(s"IOException: ${e.getMessage}") + // } +} diff --git a/compiled_starters/scala/your_program.sh b/compiled_starters/scala/your_program.sh new file mode 100755 index 0000000..de4e138 --- /dev/null +++ b/compiled_starters/scala/your_program.sh @@ -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 + SBT_OPTS="--enable-native-access=ALL-UNNAMED" sbt assembly +) + +# 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 java -jar "$(dirname "$0")/target/scala-3.7.4/http-server.jar" "$@" diff --git a/course-definition.yml b/course-definition.yml index cc0f870..4b22a95 100644 --- a/course-definition.yml +++ b/course-definition.yml @@ -79,6 +79,7 @@ languages: - slug: "python" - slug: "ruby" - slug: "rust" + - slug: "scala" - slug: "typescript" - slug: "zig" diff --git a/dockerfiles/scala-3.7.Dockerfile b/dockerfiles/scala-3.7.Dockerfile new file mode 100644 index 0000000..c153173 --- /dev/null +++ b/dockerfiles/scala-3.7.Dockerfile @@ -0,0 +1,27 @@ +# syntax=docker/dockerfile:1.7-labs +FROM maven:3.9.9-eclipse-temurin-24-noble + +ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="build.sbt,project/assembly.sbt,project/build.properties" + +SHELL ["/bin/bash", "-o", "pipefail", "-c"] + +RUN echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | tee /etc/apt/sources.list.d/sbt.list + +RUN echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | tee /etc/apt/sources.list.d/sbt_old.list + +RUN curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/scalasbt-release.gpg --import && \ + chmod 644 /etc/apt/trusted.gpg.d/scalasbt-release.gpg + +RUN apt-get update && \ + apt-get install --no-install-recommends sbt=1.11.7 -yqq && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +ENV PATH=/usr/local/sbt/bin:$PATH + +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app + +WORKDIR /app + +RUN .codecrafters/compile.sh \ No newline at end of file diff --git a/solutions/scala/01-at4/code/.codecrafters/compile.sh b/solutions/scala/01-at4/code/.codecrafters/compile.sh new file mode 100755 index 0000000..7d1ab4f --- /dev/null +++ b/solutions/scala/01-at4/code/.codecrafters/compile.sh @@ -0,0 +1,12 @@ +#!/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 + +# The option is needed for Java 17+ to allow native memory access operations. +SBT_OPTS="--enable-native-access=ALL-UNNAMED" sbt assembly diff --git a/solutions/scala/01-at4/code/.codecrafters/run.sh b/solutions/scala/01-at4/code/.codecrafters/run.sh new file mode 100755 index 0000000..b36382c --- /dev/null +++ b/solutions/scala/01-at4/code/.codecrafters/run.sh @@ -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 java -jar "$(dirname "$0")/target/scala-3.7.4/http-server.jar" "$@" diff --git a/solutions/scala/01-at4/code/.gitattributes b/solutions/scala/01-at4/code/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/solutions/scala/01-at4/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/scala/01-at4/code/.gitignore b/solutions/scala/01-at4/code/.gitignore new file mode 100644 index 0000000..808a587 --- /dev/null +++ b/solutions/scala/01-at4/code/.gitignore @@ -0,0 +1,28 @@ +# macOS +.DS_Store + +# sbt specific +dist/* +debug/ +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ +project/local-plugins.sbt +.history +.ensime +.ensime_cache/ +.sbt-scripted/ +local.sbt + +# Bloop +.bsp + +# VS Code +.vscode/ + +# Metals +.bloop/ +.metals/ +metals.sbt \ No newline at end of file diff --git a/solutions/scala/01-at4/code/README.md b/solutions/scala/01-at4/code/README.md new file mode 100644 index 0000000..19d89f9 --- /dev/null +++ b/solutions/scala/01-at4/code/README.md @@ -0,0 +1,38 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png) + +This is a starting point for Scala solutions to the +["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview). + +[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the +protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server +that is capable of serving multiple clients. + +Along the way you'll learn about TCP servers, +[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html), +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 HTTP server implementation is in +`src/main/scala/codecrafters_http_server/App.scala`. 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 +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `sbt (1.11.7)` installed locally +1. Run `./your_program.sh` to run your program, which is implemented in + `src/main/scala/codecrafters_http_server/App.scala`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/solutions/scala/01-at4/code/build.sbt b/solutions/scala/01-at4/code/build.sbt new file mode 100644 index 0000000..2fc2851 --- /dev/null +++ b/solutions/scala/01-at4/code/build.sbt @@ -0,0 +1,14 @@ +ThisBuild / scalaVersion := "3.7.4" +ThisBuild / version := "0.1.0-SNAPSHOT" +ThisBuild / organization := "com.codecrafters" +ThisBuild / organizationName := "CodeCrafters" + +assembly / assemblyJarName := "http-server.jar" + +lazy val root = (project in file(".")) + .settings( + name := "codecrafter-http-server", + // List your dependencies here + libraryDependencies ++= Seq( + ) + ) diff --git a/solutions/scala/01-at4/code/codecrafters.yml b/solutions/scala/01-at4/code/codecrafters.yml new file mode 100644 index 0000000..a3644bc --- /dev/null +++ b/solutions/scala/01-at4/code/codecrafters.yml @@ -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 Scala version used to run your code +# on Codecrafters. +# +# Available versions: scala-3.7 +buildpack: scala-3.7 diff --git a/solutions/scala/01-at4/code/project/assembly.sbt b/solutions/scala/01-at4/code/project/assembly.sbt new file mode 100644 index 0000000..f8ea5d0 --- /dev/null +++ b/solutions/scala/01-at4/code/project/assembly.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") diff --git a/solutions/scala/01-at4/code/project/build.properties b/solutions/scala/01-at4/code/project/build.properties new file mode 100644 index 0000000..01a16ed --- /dev/null +++ b/solutions/scala/01-at4/code/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.11.7 diff --git a/solutions/scala/01-at4/code/src/main/scala/codecrafters_http_server/App.scala b/solutions/scala/01-at4/code/src/main/scala/codecrafters_http_server/App.scala new file mode 100644 index 0000000..a86dba0 --- /dev/null +++ b/solutions/scala/01-at4/code/src/main/scala/codecrafters_http_server/App.scala @@ -0,0 +1,20 @@ +package codecrafters_http_server + +import java.io.IOException +import java.net.ServerSocket + +object Main extends App { + try { + val serverSocket = new ServerSocket(4221) + + // Since the tester restarts your program quite often, setting SO_REUSEADDR + // ensures that we don't run into 'Address already in use' errors + serverSocket.setReuseAddress(true) + + serverSocket.accept() // Wait for connection from client. + println("accepted new connection") + } catch { + case e: IOException => + println(s"IOException: ${e.getMessage}") + } +} diff --git a/solutions/scala/01-at4/code/your_program.sh b/solutions/scala/01-at4/code/your_program.sh new file mode 100755 index 0000000..de4e138 --- /dev/null +++ b/solutions/scala/01-at4/code/your_program.sh @@ -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 + SBT_OPTS="--enable-native-access=ALL-UNNAMED" sbt assembly +) + +# 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 java -jar "$(dirname "$0")/target/scala-3.7.4/http-server.jar" "$@" diff --git a/solutions/scala/01-at4/diff/src/main/scala/codecrafters_http_server/App.scala.diff b/solutions/scala/01-at4/diff/src/main/scala/codecrafters_http_server/App.scala.diff new file mode 100644 index 0000000..83ce361 --- /dev/null +++ b/solutions/scala/01-at4/diff/src/main/scala/codecrafters_http_server/App.scala.diff @@ -0,0 +1,38 @@ +@@ -1,25 +1,20 @@ + package codecrafters_http_server + + import java.io.IOException + import java.net.ServerSocket + + object Main extends App { +- // You can use print statements as follows for debugging, they'll be visible when running tests. +- System.err.println("Logs from your program will appear here!") ++ try { ++ val serverSocket = new ServerSocket(4221) + +- // TODO: Uncomment the code below to pass the first stage +- // +- // try { +- // val serverSocket = new ServerSocket(4221) +- // +- // // Since the tester restarts your program quite often, setting SO_REUSEADDR +- // // ensures that we don't run into 'Address already in use' errors +- // serverSocket.setReuseAddress(true) +- // +- // serverSocket.accept() // Wait for connection from client. +- // println("accepted new connection") +- // } catch { +- // case e: IOException => +- // println(s"IOException: ${e.getMessage}") +- // } ++ // Since the tester restarts your program quite often, setting SO_REUSEADDR ++ // ensures that we don't run into 'Address already in use' errors ++ serverSocket.setReuseAddress(true) ++ ++ serverSocket.accept() // Wait for connection from client. ++ println("accepted new connection") ++ } catch { ++ case e: IOException => ++ println(s"IOException: ${e.getMessage}") ++ } + } diff --git a/starter_templates/scala/code/.codecrafters/compile.sh b/starter_templates/scala/code/.codecrafters/compile.sh new file mode 100755 index 0000000..7d1ab4f --- /dev/null +++ b/starter_templates/scala/code/.codecrafters/compile.sh @@ -0,0 +1,12 @@ +#!/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 + +# The option is needed for Java 17+ to allow native memory access operations. +SBT_OPTS="--enable-native-access=ALL-UNNAMED" sbt assembly diff --git a/starter_templates/scala/code/.codecrafters/run.sh b/starter_templates/scala/code/.codecrafters/run.sh new file mode 100755 index 0000000..b36382c --- /dev/null +++ b/starter_templates/scala/code/.codecrafters/run.sh @@ -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 java -jar "$(dirname "$0")/target/scala-3.7.4/http-server.jar" "$@" diff --git a/starter_templates/scala/code/.gitignore b/starter_templates/scala/code/.gitignore new file mode 100644 index 0000000..808a587 --- /dev/null +++ b/starter_templates/scala/code/.gitignore @@ -0,0 +1,28 @@ +# macOS +.DS_Store + +# sbt specific +dist/* +debug/ +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ +project/local-plugins.sbt +.history +.ensime +.ensime_cache/ +.sbt-scripted/ +local.sbt + +# Bloop +.bsp + +# VS Code +.vscode/ + +# Metals +.bloop/ +.metals/ +metals.sbt \ No newline at end of file diff --git a/starter_templates/scala/code/build.sbt b/starter_templates/scala/code/build.sbt new file mode 100644 index 0000000..2fc2851 --- /dev/null +++ b/starter_templates/scala/code/build.sbt @@ -0,0 +1,14 @@ +ThisBuild / scalaVersion := "3.7.4" +ThisBuild / version := "0.1.0-SNAPSHOT" +ThisBuild / organization := "com.codecrafters" +ThisBuild / organizationName := "CodeCrafters" + +assembly / assemblyJarName := "http-server.jar" + +lazy val root = (project in file(".")) + .settings( + name := "codecrafter-http-server", + // List your dependencies here + libraryDependencies ++= Seq( + ) + ) diff --git a/starter_templates/scala/code/project/assembly.sbt b/starter_templates/scala/code/project/assembly.sbt new file mode 100644 index 0000000..f8ea5d0 --- /dev/null +++ b/starter_templates/scala/code/project/assembly.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.1") diff --git a/starter_templates/scala/code/project/build.properties b/starter_templates/scala/code/project/build.properties new file mode 100644 index 0000000..01a16ed --- /dev/null +++ b/starter_templates/scala/code/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.11.7 diff --git a/starter_templates/scala/code/src/main/scala/codecrafters_http_server/App.scala b/starter_templates/scala/code/src/main/scala/codecrafters_http_server/App.scala new file mode 100644 index 0000000..37b058b --- /dev/null +++ b/starter_templates/scala/code/src/main/scala/codecrafters_http_server/App.scala @@ -0,0 +1,25 @@ +package codecrafters_http_server + +import java.io.IOException +import java.net.ServerSocket + +object Main extends App { + // You can use print statements as follows for debugging, they'll be visible when running tests. + System.err.println("Logs from your program will appear here!") + + // TODO: Uncomment the code below to pass the first stage + // + // try { + // val serverSocket = new ServerSocket(4221) + // + // // Since the tester restarts your program quite often, setting SO_REUSEADDR + // // ensures that we don't run into 'Address already in use' errors + // serverSocket.setReuseAddress(true) + // + // serverSocket.accept() // Wait for connection from client. + // println("accepted new connection") + // } catch { + // case e: IOException => + // println(s"IOException: ${e.getMessage}") + // } +} diff --git a/starter_templates/scala/config.yml b/starter_templates/scala/config.yml new file mode 100644 index 0000000..224bfb5 --- /dev/null +++ b/starter_templates/scala/config.yml @@ -0,0 +1,3 @@ +attributes: + required_executable: sbt (1.11.7) + user_editable_file: src/main/scala/codecrafters_http_server/App.scala