generated from calcit-lang/calcit-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact.cirru
92 lines (91 loc) · 3.99 KB
/
compact.cirru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{} (:package |app)
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.0.1)
:modules $ [] |skir/ |lilac/
:entries $ {}
:test $ {} (:init-fn |app.test/main!) (:reload-fn |app.test/reload!)
:modules $ [] |calcit-test/
:files $ {}
|app.http $ %{} :FileEntry
:defs $ {}
|extract-body $ %{} :CodeEntry (:doc |)
:code $ quote
defn extract-body (req cb)
let
*buf $ atom "\""
.!on req "\"data" $ fn (d) (swap! *buf str d)
.!on req "\"end" $ fn ()
cb $ parse-cirru-edn (deref *buf)
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.http)
|app.main $ %{} :FileEntry
:defs $ {}
|handle-token $ %{} :CodeEntry (:doc |)
:code $ quote
defn handle-token (bucket app-id secret file-key cb)
&let
cos-obj $ new COS
js-object (:SecretId app-id) (:SecretKey secret)
w-js-log $ .!getObjectUrl cos-obj
js-object (:Bucket bucket) (:Region "\"ap-shanghai")
:Key $ str "\"cos-up/" (or file-key "\"demo0")
:Method "\"PUT"
:Sign true
fn (err data) (js/console.log "\"COS DATA:" err data)
if (some? err)
cb $ {} (:code 400)
:body $ {}
:message $ str err
cb $ {} (:code 200)
:headers $ {} ("\"Access-Control-Allow-Origin" "\"*")
:body $ {}
:url $ .-Url data
|main! $ %{} :CodeEntry (:doc |)
:code $ quote
defn main! ()
println "\"COS get" $ .-getAuthorization COS
skir/create-server! on-request! $ {}
:port $ js/parseInt (get-env "\"PORT" "\"4000")
|on-request! $ %{} :CodeEntry (:doc |)
:code $ quote
defn on-request! (req-data res)
println $ format-cirru-edn
-> req-data (dissoc :original-request) (dissoc :headers)
if
and
= :post $ :method req-data
= "\"/token" $ :path req-data
let
secrets $ parse-cirru-edn
fs/readFileSync
path/join (fileURLToPath js/import.meta.url) "\"../../secrets.cirru"
, "\"utf8"
data $ {}
fn (cb)
extract-body (:original-request req-data)
fn (body) (println "\"BODY" body)
if
and
= (:user body) (:user secrets)
= (:pass body) (:pass-md5 secrets)
handle-token (:bucket secrets) (:app-id secrets) (:secret secrets) (:file-key body) cb
cb $ {} (:code 403) (:message "\"Not user")
:headers $ {}
:body $ {} (:message "\"not open for all users...")
if
= :options $ :method req-data
{} (:code 200) (:message "\"OK")
:headers $ {} ("\"Access-Control-Allow-Origin" "\"*")
{} (:code 404) (:message "\"Non hit")
:headers $ {}
:body $ {} (:message "\"only token is impelemented")
|reload! $ %{} :CodeEntry (:doc |)
:code $ quote
defn reload! () (skir/reset-req-handler! on-request!) (println "\"Reloaded.")
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.main $ :require ("\"cos-nodejs-sdk-v5" :default COS) (skir.core :as skir)
app.$meta :refer $ calcit-dirname
"\"node:path" :as path
"\"node:url" :refer $ fileURLToPath
"\"node:fs" :as fs
app.http :refer $ extract-body