-
Notifications
You must be signed in to change notification settings - Fork 32
[Challenge][Http-Server][Language support][Clojure] Add clojure support to http-server-challenge #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Challenge][Http-Server][Language support][Clojure] Add clojure support to http-server-challenge #112
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| clj -T:build uber |
| 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 java -jar /tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure-standalone.jar "$@" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| * text=auto |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| *.jar | ||
| target/ | ||
| .idea/ | ||
| .clj-kondo/ | ||
| .lsp/ | ||
| .nrepl-port | ||
| *.class | ||
| /lib/ | ||
| /classes/ | ||
| .cpcache/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
|  | ||
|
|
||
| This is a starting point for Clojure 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.clj`. 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 `clj (1.12.0)` installed locally | ||
| 1. Run `./your_program.sh` to run your program, which is implemented in | ||
| `src/main.clj`. | ||
| 1. Commit your changes and run `git push origin master` to submit your solution | ||
| to CodeCrafters. Test output will be streamed to your terminal. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| (ns build | ||
| (:require [clojure.tools.build.api :as b] | ||
| [clojure.edn :as edn])) | ||
|
|
||
| (def project (-> (edn/read-string (slurp "deps.edn")) | ||
| :aliases :neil :project)) | ||
| (def lib (or (:name project) 'my/lib1)) | ||
|
|
||
| (def version (or (:version project) | ||
| "0.0.1")) | ||
| (def class-dir "target/classes") | ||
| (def basis (b/create-basis {:project "deps.edn"})) | ||
| (def uber-file "/tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure-standalone.jar") | ||
| (def jar-file "/tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure.jar") | ||
|
|
||
| (defn clean [_] | ||
| (b/delete {:path "target"})) | ||
|
|
||
| (defn jar [_] | ||
| (b/write-pom {:class-dir class-dir | ||
| :lib lib | ||
| :version version | ||
| :basis basis | ||
| :src-dirs ["src"]}) | ||
| (b/copy-dir {:src-dirs ["src" "resources"] | ||
| :target-dir class-dir}) | ||
| (b/jar {:class-dir class-dir | ||
| :jar-file jar-file})) | ||
|
|
||
| (defn install [_] | ||
| (jar {}) | ||
| (b/install {:basis basis | ||
| :lib lib | ||
| :version version | ||
| :jar-file jar-file | ||
| :class-dir class-dir})) | ||
|
|
||
| (defn uber [_] | ||
| (clean nil) | ||
| (b/copy-dir {:src-dirs ["src" "resources"] | ||
| :target-dir class-dir}) | ||
| (b/compile-clj {:basis basis | ||
| :src-dirs ["src"] | ||
| :class-dir class-dir}) | ||
| (b/uber {:class-dir class-dir | ||
| :uber-file uber-file | ||
| :basis basis | ||
| :main 'main})) | ||
|
|
||
| (defn deploy [opts] | ||
| (jar opts) | ||
| ((requiring-resolve 'deps-deploy.deps-deploy/deploy) | ||
| (merge {:installer :remote | ||
| :artifact jar-file | ||
| :pom-file (b/pom-path {:lib lib :class-dir class-dir})} | ||
| opts)) | ||
| opts) | ||
|
|
| 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 Clojure version used to run your code | ||
| # on Codecrafters. | ||
| # | ||
| # Available versions: clojure-1.12 | ||
| language_pack: clojure-1.12 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {:paths ["src"] | ||
| :deps {} | ||
| :aliases {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.10.7" | ||
| :git/sha "573711e"} | ||
| slipset/deps-deploy {:mvn/version "0.2.2"}} | ||
| :ns-default build}}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| (ns main | ||
| (:gen-class)) | ||
|
|
||
| (defn -main [& args] | ||
| ;; You can use print statements as follows for debugging, they'll be visible when running tests. | ||
| (println "Logs from your program will appear here!") | ||
| ;; Uncomment this block to pass the first stage | ||
| ;; (try | ||
| ;; (with-open [server-socket (java.net.ServerSocket. 4221)] | ||
| ;; (doto server-socket | ||
| ;; (.setReuseAddress true) | ||
| ;; (.accept)) | ||
| ;; (println "accepted new connection")) | ||
| ;; (catch java.io.IOException e | ||
| ;; (println (str "IOException: " (.getMessage e))))) | ||
| ) |
| 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 | ||
| clj -T:build uber | ||
| ) | ||
|
|
||
| # 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 /tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure-standalone.jar "$@" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # syntax=docker/dockerfile:1.7-labs | ||
| FROM clojure:tools-deps-1.12.0.1501-bookworm | ||
|
|
||
| ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="deps.edn" | ||
|
|
||
| WORKDIR /app | ||
| # COPY deps.edn ./ | ||
|
|
||
| # .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses | ||
| COPY --exclude=.git --exclude=README.md . /app | ||
|
|
||
| # Install language-specific dependencies | ||
| RUN .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 | ||
|
|
||
| clj -T:build uber |
| 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 java -jar /tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure-standalone.jar "$@" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| * text=auto |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| *.jar | ||
| target/ | ||
| .idea/ | ||
| .clj-kondo/ | ||
| .lsp/ | ||
| .nrepl-port | ||
| *.class | ||
| /lib/ | ||
| /classes/ | ||
| .cpcache/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
|  | ||
|
|
||
| This is a starting point for Clojure 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.clj`. 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 `clj (1.12.0)` installed locally | ||
| 1. Run `./your_program.sh` to run your program, which is implemented in | ||
| `src/main.clj`. | ||
| 1. Commit your changes and run `git push origin master` to submit your solution | ||
| to CodeCrafters. Test output will be streamed to your terminal. |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||
| (ns build | ||||||||||
| (:require [clojure.tools.build.api :as b] | ||||||||||
| [clojure.edn :as edn])) | ||||||||||
|
|
||||||||||
| (def project (-> (edn/read-string (slurp "deps.edn")) | ||||||||||
| :aliases :neil :project)) | ||||||||||
| (def lib (or (:name project) 'my/lib1)) | ||||||||||
|
|
||||||||||
| (def version (or (:version project) | ||||||||||
| "0.0.1")) | ||||||||||
| (def class-dir "target/classes") | ||||||||||
| (def basis (b/create-basis {:project "deps.edn"})) | ||||||||||
| (def uber-file "/tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure-standalone.jar") | ||||||||||
| (def jar-file "/tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure.jar") | ||||||||||
|
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid hardcoding paths in /tmp directory. Using hardcoded paths in
Consider using relative paths in the target directory: -(def uber-file "/tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure-standalone.jar")
-(def jar-file "/tmp/codecrafters-build-http-server-clojure/codecrafters-http-server-clojure.jar")
+(def uber-file "target/codecrafters-http-server-clojure-standalone.jar")
+(def jar-file "target/codecrafters-http-server-clojure.jar")📝 Committable suggestion
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SughiY Understood, I'll retract the suggestion to change the hardcoded paths. ✏️ Learnings added
|
||||||||||
|
|
||||||||||
| (defn clean [_] | ||||||||||
| (b/delete {:path "target"})) | ||||||||||
|
|
||||||||||
| (defn jar [_] | ||||||||||
| (b/write-pom {:class-dir class-dir | ||||||||||
| :lib lib | ||||||||||
| :version version | ||||||||||
| :basis basis | ||||||||||
| :src-dirs ["src"]}) | ||||||||||
| (b/copy-dir {:src-dirs ["src" "resources"] | ||||||||||
| :target-dir class-dir}) | ||||||||||
| (b/jar {:class-dir class-dir | ||||||||||
| :jar-file jar-file})) | ||||||||||
|
|
||||||||||
| (defn install [_] | ||||||||||
| (jar {}) | ||||||||||
| (b/install {:basis basis | ||||||||||
| :lib lib | ||||||||||
| :version version | ||||||||||
| :jar-file jar-file | ||||||||||
| :class-dir class-dir})) | ||||||||||
|
|
||||||||||
| (defn uber [_] | ||||||||||
| (clean nil) | ||||||||||
| (b/copy-dir {:src-dirs ["src" "resources"] | ||||||||||
| :target-dir class-dir}) | ||||||||||
| (b/compile-clj {:basis basis | ||||||||||
| :src-dirs ["src"] | ||||||||||
| :class-dir class-dir}) | ||||||||||
| (b/uber {:class-dir class-dir | ||||||||||
| :uber-file uber-file | ||||||||||
| :basis basis | ||||||||||
| :main 'main})) | ||||||||||
|
|
||||||||||
| (defn deploy [opts] | ||||||||||
| (jar opts) | ||||||||||
| ((requiring-resolve 'deps-deploy.deps-deploy/deploy) | ||||||||||
| (merge {:installer :remote | ||||||||||
| :artifact jar-file | ||||||||||
| :pom-file (b/pom-path {:lib lib :class-dir class-dir})} | ||||||||||
| opts)) | ||||||||||
| opts) | ||||||||||
|
|
||||||||||
| 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 Clojure version used to run your code | ||
| # on Codecrafters. | ||
| # | ||
| # Available versions: clojure-1.12 | ||
| language_pack: clojure-1.12 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {:paths ["src"] | ||
| :deps {} | ||
| :aliases {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.10.7" | ||
| :git/sha "573711e"} | ||
| slipset/deps-deploy {:mvn/version "0.2.2"}} | ||
| :ns-default build}}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| (ns main | ||
| (:gen-class)) | ||
|
|
||
| (defn -main [& args] | ||
| (try | ||
| (with-open [server-socket (java.net.ServerSocket. 4221)] | ||
| (doto server-socket | ||
| (.setReuseAddress true) | ||
| (.accept)) | ||
| (println "accepted new connection")) | ||
| (catch java.io.IOException e | ||
| (println (str "IOException: " (.getMessage e))))) | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unsupported
--excludeflags in COPY command.The
COPYcommand in Docker does not support the--excludeflag, which triggers the static analysis error. Consider using a.dockerignorefile to exclude.gitandREADME.mdfrom the build context instead.Proposed diff:
Additionally, create a
.dockerignorefile at the repository root with:📝 Committable suggestion
🧰 Tools
🪛 Hadolint (2.12.0)
[error] 10-10: invalid flag: --exclude
(DL1000)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SughiY Understood, I'll retract my suggestion about the
--excludeflags.✏️ Learnings added