Skip to content

Commit

Permalink
feat: routing with compojure
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesreprise committed Sep 11, 2023
1 parent 8b5e10e commit 8af3705
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
8 changes: 5 additions & 3 deletions game_server/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
:license {:name "GNU AGPL v3"
:url "https://www.gnu.org/licenses/agpl-3.0.en.html"}
:dependencies [[org.clojure/clojure "1.10.3"]
[cheshire "5.11.0"]
[org.clojure/tools.logging "1.2.4"]
[ring "1.10.0"]
[http-kit "2.3.0"]]
[compojure "1.7.0"]
[http-kit "2.3.0"]
[cheshire "5.11.0"]]
:main game_server.core
:aot [game_server.core]
:profiles {:kaocha {:dependencies [[lambdaisland/kaocha "1.86.1355"]]}}
:aliases {"kaocha" ["with-profile" "+kaocha" "run" "-m" "kaocha.runner"]}
:ring {:handler game_server.core/game}
:ring {:handler game_server.core/server}
:plugins [[lein-ring "0.12.6"]]
:repl-options {:init-ns game_server.core})
34 changes: 28 additions & 6 deletions game_server/src/game_server/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
(ns game_server.core
(:require [cheshire.core :as cheshire]
[org.httpkit.server :as http-server])
(:require
[clojure.tools.logging :as log]
[cheshire.core :as cheshire]
[org.httpkit.server :as http-server]
[compojure.core :refer [GET defroutes]]
[compojure.route :as route]
[compojure.handler :as handler])
(:gen-class))

(def board
Expand Down Expand Up @@ -79,10 +84,27 @@
:current-player 0
:turn 0})

(defn game [_]
{:status 200
(defn response [status map]
{:status status
:headers {"Content-Type" "application/json"}
:body (cheshire/generate-string (new-game {"p1" {:piece :car} "p2" {:piece :hat}}))})
:body (cheshire/generate-string map)})

(def example-2p-game
(new-game {"p1" {:piece :car} "p2" {:piece :hat}}))

(defn game [_]
(response 200 example-2p-game))

(defroutes server-routes
(GET "/" [] game)
(GET "/turn" [] (response 200 (turn example-2p-game)))
(GET "/turn2" [] (response 200 (turn (turn example-2p-game))))
(route/not-found (response 404 {:status 404 :error "Not Found"})))

(def server
(-> (handler/site server-routes)))

(defn -main [& _]
(http-server/run-server game {:port 3000}))
(log/info "Loading...")
(http-server/run-server server {:port 3000})
(log/info "READY"))

0 comments on commit 8af3705

Please sign in to comment.