Skip to content

Instantly share code, notes, and snippets.

@kaze
Forked from plexus/core.clj
Created October 9, 2021 13:49
Show Gist options
  • Save kaze/e242a4ef3050f5b939af873b83fadf47 to your computer and use it in GitHub Desktop.
Save kaze/e242a4ef3050f5b939af873b83fadf47 to your computer and use it in GitHub Desktop.
(ns my-backend.core
(:gen-class)
(:require [compojure.core :as c]
[compojure.route :as route]
[ring.adapter.jetty :as jetty]
[ring.middleware.defaults :as ring-defaults]
[hiccup2.core :as h]
#_[muuntaja.core :as m]
[muuntaja.middleware :as muuntaja]))
(defonce server (atom nil))
(defn home-view [count]
[:html
[:body
[:h1 "Welcome home!"]
[:ul
(for [i (range count)]
[:li i])]]])
(defn routes []
(c/routes
(c/GET "/" [count]
{:status 200
:headers {"Content-Type" "text/html"}
:body (str (h/html (home-view (Integer. (or count 0)))))})
(c/GET "/:foo" [foo]
{:status 200
:body (str "you asked for " foo)})
(c/POST "/api" [:as req]
(clojure.pprint/pprint (:body-params req))
{:status 200
:body {:hello 123}})))
(defn handler [req]
((routes) req))
(defn start-jetty! []
(reset!
server
(jetty/run-jetty (-> #'handler
muuntaja/wrap-format
(ring-defaults/wrap-defaults ring-defaults/api-defaults))
{:join? false
:port 3428})))
(defn stop-jetty! []
(.stop @server)
(reset! server nil))
(defn -main [& args]
(start-jetty!))
#_(start-jetty!)
{:paths ["src"]
:deps {ring/ring-jetty-adapter {:mvn/version "1.9.4"}
ring/ring-defaults {:mvn/version "0.3.3"}
compojure/compojure {:mvn/version "1.6.2"}
hiccup/hiccup {:mvn/version "2.0.0-alpha2"}
metosin/muuntaja {:mvn/version "0.6.8"}}}
.
├── deps.edn
├── src
│   └── my_backend
│   └── core.clj
└── tree
2 directories, 3 files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment