Skip to content

Commit

Permalink
Merge pull request #1 from Termina/token
Browse files Browse the repository at this point in the history
Token API
  • Loading branch information
NoEgAm authored Sep 19, 2023
2 parents 7a9f8c4 + 3d799ab commit 61fedd9
Show file tree
Hide file tree
Showing 9 changed files with 1,121 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ jobs:
version: '0.8.1'

- name: "test"
run: caps --ci && cr -1 --entry test
run: caps --ci

- run: cr --emit-js -1 --entry test && yarn && node ./main.mjs
- run: cr --emit-ir -1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ js-out/
node_modules

example/

secrets.cirru
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@
COS upload
----

> script.
> dirty script providing a server.
Format for `secrets.cirru`:

```cirru
{}
:bucket |TODO
:app-id |TODO
:secret |TODO
:user |TODO
:pass-md5 |TODO
```

Dev request:

```bash
curl -X POST http://localhost:4000/token -d "{} (:user |me) (:pass |aaaa) (:file-key |demo)"
```

### Workflow

Expand Down
508 changes: 506 additions & 2 deletions calcit.cirru

Large diffs are not rendered by default.

80 changes: 76 additions & 4 deletions compact.cirru
Original file line number Diff line number Diff line change
@@ -1,19 +1,91 @@

{} (:package |app)
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.0.1)
:modules $ []
: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-hongkong")
: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 "\"DO"
defn main! ()
println "\"COS get" $ .-getAuthorization COS
skir/create-server! on-request! $ {}
|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
{} (: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! () $ println "\"Reloaded."
defn reload! () (skir/reset-req-handler! on-request!) (println "\"Reloaded.")
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.main $ :require
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
2 changes: 1 addition & 1 deletion main.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { main_$x_, reload_$x_ } from "./js-out/app.test.mjs"
import { main_$x_, reload_$x_ } from "./js-out/app.main.mjs"

main_$x_()
2 changes: 2 additions & 0 deletions package.cirru
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

{}
:dependencies $ {}
|calcit-lang/skir |main
|calcit-lang/lilac |main
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version": "0.0.7",
"dependencies": {
"@calcit/procs": "^0.8.1"
"@calcit/procs": "^0.8.1",
"cos-nodejs-sdk-v5": "^2.12.4"
},
"scripts": {},
"devDependencies": {}
Expand Down
Loading

0 comments on commit 61fedd9

Please sign in to comment.