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]
|
||||
[clojure.java.io :as io]
|
||||
[reitit.ring :as ring]
|
||||
[ring.logger :refer [wrap-log-response]]
|
||||
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
|
||||
[heyarne.all-my-friends.env :refer [env]])
|
||||
(: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
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
[["/"
|
||||
{:get (fn [_]
|
||||
{:status 200
|
||||
:body (io/file (io/resource "public/index.html"))})}]
|
||||
["/upload"
|
||||
[["/upload"
|
||||
;; TODO: Verify params
|
||||
{:post (fn [req]
|
||||
{:status 400
|
||||
:body "To be done"})}]])))
|
||||
(let [{:keys [file friend]} (-> req :params)]
|
||||
(upload! file friend)
|
||||
{:status 201
|
||||
:body (str "Thanks, " friend)}))}]]
|
||||
["/*" (ring/create-resource-handler)])))
|
||||
|
||||
(defonce server (atom nil))
|
||||
|
||||
|
|
@ -29,7 +39,11 @@
|
|||
(defn -main [& args]
|
||||
(println "Starting server on port" (env :port))
|
||||
(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)}))))
|
||||
|
||||
#_(-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