Skip to content

Commit cedf956

Browse files
authored
Merge pull request #127 from bolivier/bolivier/add-clojure
Add Clojure code
2 parents 93588f6 + 46c63a4 commit cedf956

File tree

31 files changed

+497
-0
lines changed

31 files changed

+497
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
clj -T:build
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec java -jar /tmp/codecrafters-build-http-server-clojure/target.jar "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/clojure/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pom.xml
2+
pom.xml.asc
3+
*.jar
4+
*.class
5+
/lib/
6+
/classes/
7+
/target/
8+
/checkouts/
9+
.lein-deps-sum
10+
.lein-repl-history
11+
.lein-plugins/
12+
.lein-failures
13+
.nrepl-port
14+
.cpcache/
15+
deps/

compiled_starters/clojure/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png)
2+
3+
This is a starting point for Clojure solutions to the
4+
["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview).
5+
6+
[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the
7+
protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server
8+
that is capable of serving multiple clients.
9+
10+
Along the way you'll learn about TCP servers,
11+
[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html),
12+
and more.
13+
14+
**Note**: If you're viewing this repo on GitHub, head over to
15+
[codecrafters.io](https://codecrafters.io) to try the challenge.
16+
17+
# Passing the first stage
18+
19+
The entry point for your HTTP server implementation is in
20+
`src/http_server/core.clj`. Study and uncomment the relevant code, and push your
21+
changes to pass the first stage:
22+
23+
```sh
24+
git commit -am "pass 1st stage" # any msg
25+
git push origin master
26+
```
27+
28+
Time to move on to the next stage!
29+
30+
# Stage 2 & beyond
31+
32+
Note: This section is for stages 2 and beyond.
33+
34+
1. Ensure you have `clj` installed locally
35+
1. Run `./your_program.sh` to run your program, which is implemented in
36+
`src/http_server/core.clj`.
37+
1. Commit your changes and run `git push origin master` to submit your solution
38+
to CodeCrafters. Test output will be streamed to your terminal.

compiled_starters/clojure/build.clj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(ns build
2+
(:gen-class)
3+
(:require [clojure.tools.build.api :as b]))
4+
5+
(def lib 'io.codecrafters.http-server)
6+
(def class-dir "/tmp/codecrafters-build-http-server-clojure/classes")
7+
(def basis (b/create-basis {:project "deps.edn"}))
8+
(def uber-file "/tmp/codecrafters-build-http-server-clojure/target.jar")
9+
10+
(defn clean [_]
11+
(b/delete {:path "/tmp/codecrafters-build-http-server-clojure"}))
12+
13+
(defn uber [_]
14+
(clean nil)
15+
(b/copy-dir {:src-dirs ["src"] :target-dir class-dir})
16+
(b/compile-clj {:basis basis
17+
:ns-compile '[http-server.core]
18+
:class-dir class-dir})
19+
(b/uber {:class-dir class-dir
20+
:uber-file uber-file
21+
:basis basis
22+
:main 'http-server.core}))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the Clojure version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: clojure-1.12.0
11+
language_pack: clojure-1.12.0

compiled_starters/clojure/deps.edn

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
:paths ["src"]
3+
4+
:deps {
5+
org.clojure/clojure {:mvn/version "1.12.0"}
6+
org.clojure/tools.cli {:mvn/version "1.1.230"}
7+
}
8+
9+
:aliases {
10+
:build {
11+
:extra-deps {io.github.clojure/tools.build {:mvn/version "0.10.9"}}
12+
:exec-fn build/uber
13+
}
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns http-server.core
2+
(:require
3+
[clojure.java.io :as io])
4+
(:import
5+
[java.net ServerSocket])
6+
(:gen-class))
7+
8+
(def port 4221)
9+
10+
(defn serve []
11+
(let [server-sock (ServerSocket. port)]
12+
(.setReuseAddress server-sock true)
13+
(.accept server-sock)
14+
(binding [*out* (java.io.PrintWriter. System/out true)]
15+
(println "accepted new connection"))))
16+
17+
(defn -main [& args]
18+
;; You can use print statements as follows for debugging, they'll be visible when running tests.
19+
(println "Logs from your program will appear here!")
20+
21+
;; Uncomment this block to pass the first stage
22+
;;(serve)
23+
24+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# Use this script to run your program LOCALLY.
4+
#
5+
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit early if any commands fail
10+
11+
# Copied from .codecrafters/compile.sh
12+
#
13+
# - Edit this to change how your program compiles locally
14+
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
15+
(
16+
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
17+
clj -T:build
18+
)
19+
20+
# Copied from .codecrafters/run.sh
21+
#
22+
# - Edit this to change how your program runs locally
23+
# - Edit .codecrafters/run.sh to change how your program runs remotely
24+
exec java -jar /tmp/codecrafters-build-http-server-clojure/target.jar "$@"

course-definition.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ concept_slugs: ["network-protocols", "tcp-overview"]
6464
# The languages that your course supports.
6565
languages:
6666
- slug: "c"
67+
- slug: "clojure"
6768
- slug: "cpp"
6869
- slug: "csharp"
6970
- slug: "dart"

dockerfiles/clojure-1.12.0.Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# syntax=docker/dockerfile:1.7-labs
2+
FROM clojure:tools-deps-bookworm
3+
4+
# Ensures the container is re-built if dependency files change
5+
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="deps.edn"
6+
7+
WORKDIR /app
8+
9+
# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
10+
COPY --exclude=.git --exclude=README.md . /app
11+
12+
# Install language-specific dependencies
13+
RUN .codecrafters/compile.sh
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
clj -T:build
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec java -jar /tmp/codecrafters-build-http-server-clojure/target.jar "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pom.xml
2+
pom.xml.asc
3+
*.jar
4+
*.class
5+
/lib/
6+
/classes/
7+
/target/
8+
/checkouts/
9+
.lein-deps-sum
10+
.lein-repl-history
11+
.lein-plugins/
12+
.lein-failures
13+
.nrepl-port
14+
.cpcache/
15+
deps/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png)
2+
3+
This is a starting point for Clojure solutions to the
4+
["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview).
5+
6+
[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the
7+
protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server
8+
that is capable of serving multiple clients.
9+
10+
Along the way you'll learn about TCP servers,
11+
[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html),
12+
and more.
13+
14+
**Note**: If you're viewing this repo on GitHub, head over to
15+
[codecrafters.io](https://codecrafters.io) to try the challenge.
16+
17+
# Passing the first stage
18+
19+
The entry point for your HTTP server implementation is in
20+
`src/http_server/core.clj`. Study and uncomment the relevant code, and push your
21+
changes to pass the first stage:
22+
23+
```sh
24+
git commit -am "pass 1st stage" # any msg
25+
git push origin master
26+
```
27+
28+
Time to move on to the next stage!
29+
30+
# Stage 2 & beyond
31+
32+
Note: This section is for stages 2 and beyond.
33+
34+
1. Ensure you have `clj` installed locally
35+
1. Run `./your_program.sh` to run your program, which is implemented in
36+
`src/http_server/core.clj`.
37+
1. Commit your changes and run `git push origin master` to submit your solution
38+
to CodeCrafters. Test output will be streamed to your terminal.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(ns build
2+
(:gen-class)
3+
(:require [clojure.tools.build.api :as b]))
4+
5+
(def lib 'io.codecrafters.http-server)
6+
(def class-dir "/tmp/codecrafters-build-http-server-clojure/classes")
7+
(def basis (b/create-basis {:project "deps.edn"}))
8+
(def uber-file "/tmp/codecrafters-build-http-server-clojure/target.jar")
9+
10+
(defn clean [_]
11+
(b/delete {:path "/tmp/codecrafters-build-http-server-clojure"}))
12+
13+
(defn uber [_]
14+
(clean nil)
15+
(b/copy-dir {:src-dirs ["src"] :target-dir class-dir})
16+
(b/compile-clj {:basis basis
17+
:ns-compile '[http-server.core]
18+
:class-dir class-dir})
19+
(b/uber {:class-dir class-dir
20+
:uber-file uber-file
21+
:basis basis
22+
:main 'http-server.core}))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the Clojure version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: clojure-1.12.0
11+
language_pack: clojure-1.12.0
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
:paths ["src"]
3+
4+
:deps {
5+
org.clojure/clojure {:mvn/version "1.12.0"}
6+
org.clojure/tools.cli {:mvn/version "1.1.230"}
7+
}
8+
9+
:aliases {
10+
:build {
11+
:extra-deps {io.github.clojure/tools.build {:mvn/version "0.10.9"}}
12+
:exec-fn build/uber
13+
}
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(ns http-server.core
2+
(:require
3+
[clojure.java.io :as io])
4+
(:import
5+
[java.net ServerSocket])
6+
(:gen-class))
7+
8+
(def port 4221)
9+
10+
(defn serve []
11+
(let [server-sock (ServerSocket. port)]
12+
(.setReuseAddress server-sock true)
13+
(.accept server-sock)
14+
(binding [*out* (java.io.PrintWriter. System/out true)]
15+
(println "accepted new connection"))))
16+
17+
(defn -main [& args]
18+
(serve)
19+
20+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# Use this script to run your program LOCALLY.
4+
#
5+
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit early if any commands fail
10+
11+
# Copied from .codecrafters/compile.sh
12+
#
13+
# - Edit this to change how your program compiles locally
14+
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
15+
(
16+
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
17+
clj -T:build
18+
)
19+
20+
# Copied from .codecrafters/run.sh
21+
#
22+
# - Edit this to change how your program runs locally
23+
# - Edit .codecrafters/run.sh to change how your program runs remotely
24+
exec java -jar /tmp/codecrafters-build-http-server-clojure/target.jar "$@"

0 commit comments

Comments
 (0)