Implement upload endpoint :)
This commit is contained in:
parent
0469f59e80
commit
13fad6756e
2 changed files with 24 additions and 8 deletions
|
|
@ -2,21 +2,31 @@
|
||||||
(:require [org.httpkit.server :as http]
|
(:require [org.httpkit.server :as http]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
|
[ring.logger :refer [wrap-log-response]]
|
||||||
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
|
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
|
||||||
[heyarne.all-my-friends.env :refer [env]])
|
[heyarne.all-my-friends.env :refer [env]])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
|
(def rfc3339 (java.text.SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ssXXX"))
|
||||||
|
|
||||||
|
(defn upload! [file id]
|
||||||
|
(let [now (.format rfc3339 (java.util.Date.))
|
||||||
|
uuid (java.util.UUID/randomUUID)
|
||||||
|
ext (re-find #"\.[\w]+$" (-> file :filename))]
|
||||||
|
(io/copy (-> file :tempfile)
|
||||||
|
(io/file (str "uploads/" id "--" now "--" uuid ext)))))
|
||||||
|
|
||||||
(def app
|
(def app
|
||||||
(ring/ring-handler
|
(ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
[["/"
|
[["/upload"
|
||||||
{:get (fn [_]
|
;; TODO: Verify params
|
||||||
{:status 200
|
|
||||||
:body (io/file (io/resource "public/index.html"))})}]
|
|
||||||
["/upload"
|
|
||||||
{:post (fn [req]
|
{:post (fn [req]
|
||||||
{:status 400
|
(let [{:keys [file friend]} (-> req :params)]
|
||||||
:body "To be done"})}]])))
|
(upload! file friend)
|
||||||
|
{:status 201
|
||||||
|
:body (str "Thanks, " friend)}))}]]
|
||||||
|
["/*" (ring/create-resource-handler)])))
|
||||||
|
|
||||||
(defonce server (atom nil))
|
(defonce server (atom nil))
|
||||||
|
|
||||||
|
|
@ -29,7 +39,11 @@
|
||||||
(defn -main [& args]
|
(defn -main [& args]
|
||||||
(println "Starting server on port" (env :port))
|
(println "Starting server on port" (env :port))
|
||||||
(reset! server (-> #'app
|
(reset! server (-> #'app
|
||||||
(wrap-defaults (assoc site-defaults :static {:resources "public"}))
|
(wrap-defaults (-> (assoc site-defaults
|
||||||
|
:static {:resources "public"}
|
||||||
|
:session false)
|
||||||
|
(assoc-in [:security :anti-forgery] false)))
|
||||||
|
(wrap-log-response)
|
||||||
(http/run-server {:port (env :port)}))))
|
(http/run-server {:port (env :port)}))))
|
||||||
|
|
||||||
#_(-main)
|
#_(-main)
|
||||||
|
|
|
||||||
2
uploads/.gitignore
vendored
Normal file
2
uploads/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
*
|
||||||
|
!.gitignore
|
||||||
Loading…
Add table
Add a link
Reference in a new issue