Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token API #1

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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