From c6e522f685f00a73742d82d8e676d24719f58c95 Mon Sep 17 00:00:00 2001 From: Nemo Date: Wed, 25 May 2022 20:48:26 +0530 Subject: [PATCH] Fly API deployment --- .dockerignore | 4 ++++ .github/workflows/api.yml | 15 ++++++++++++++ .github/workflows/update.yml | 3 +-- .gitignore | 5 ++++- Dockerfile | 19 ++++++++++++++++++ Makefile | 9 +++++++-- deps.ts | 8 ++++++++ fly.toml | 39 ++++++++++++++++++++++++++++++++++++ main.ts | 27 +++++++++++++++++++++++++ src/setup.sql | 5 +++++ 10 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/api.yml create mode 100644 Dockerfile create mode 100644 deps.ts create mode 100644 fly.toml create mode 100644 main.ts create mode 100644 src/setup.sql diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..10b8ffa --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +* +!ISIN.db +!main.ts +!deps.ts diff --git a/.github/workflows/api.yml b/.github/workflows/api.yml new file mode 100644 index 0000000..16b5b94 --- /dev/null +++ b/.github/workflows/api.yml @@ -0,0 +1,15 @@ +name: API Deploy +on: + release: + types: [published] + push: +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} +jobs: + deploy: + name: Deploy app + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: superfly/flyctl-actions/setup-flyctl@master + - run: make db && flyctl deploy --remote-only diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index 3b6a591..5ec476f 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -1,7 +1,6 @@ name: Update Data on: workflow_dispatch: - push: schedule: # 18:07 UTC every day # 23:37 IST every day @@ -30,4 +29,4 @@ jobs: pip install -r src/requirements.txt make release "version=v${{ steps.update_data.outputs.version }}" env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 78433da..305d24a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ pup release.md pup.zip IN*.csv -notes.md \ No newline at end of file +notes.md +ISIN.db +ISIN.sqbpro +scratchpad.txt \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..34ccc53 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM denoland/deno:1.22.0 + +# The port that your application listens to. +EXPOSE 8080 + +WORKDIR /app + +# Prefer not to run as root. +USER deno + +# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified). +# Ideally cache deps.ts will download and compile _all_ external files used in main.ts. +COPY deps.ts . +RUN deno cache deps.ts + +# These steps will be re-run upon each file change in your working directory: +ADD . . + +CMD ["run", "--cached-only", "--allow-net", "--allow-read", "main.ts"] \ No newline at end of file diff --git a/Makefile b/Makefile index 16607fc..af62d89 100644 --- a/Makefile +++ b/Makefile @@ -33,5 +33,10 @@ old: release-notes: old python3 src/diff.py -release: release-notes - gh release create "$(version)" --notes-file notes.md ISIN.csv release.md +release: release-notes db + gh release create "$(version)" --notes-file notes.md ISIN.csv release.md ISIN.db + +db: + cat ISIN.csv | tail -n +2 > /tmp/ISIN-no-headers.csv + rm -f ISIN.db + cat src/setup.sql | sqlite3 ISIN.db \ No newline at end of file diff --git a/deps.ts b/deps.ts new file mode 100644 index 0000000..41d36d6 --- /dev/null +++ b/deps.ts @@ -0,0 +1,8 @@ +import { DB } from "https://deno.land/x/sqlite/mod.ts"; +import { + app, + get, + post, + redirect, + contentType, +} from "https://denopkg.com/syumai/dinatra@0.15.0/mod.ts"; \ No newline at end of file diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..c4f7227 --- /dev/null +++ b/fly.toml @@ -0,0 +1,39 @@ +app = "isin" + +kill_signal = "SIGINT" +kill_timeout = 5 + +[env] + PORT = "8080" + +[experimental] + allowed_public_ports = [] + auto_rollback = true + + +[[services]] + http_checks = [] + internal_port = 8080 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + hard_limit = 25 + soft_limit = 20 + type = "connections" + + [[services.ports]] + force_https = true + handlers = ["http"] + port = 80 + + [[services.ports]] + handlers = ["tls", "http"] + port = 443 + + [[services.tcp_checks]] + grace_period = "1s" + interval = "15s" + restart_limit = 0 + timeout = "2s" diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..def169e --- /dev/null +++ b/main.ts @@ -0,0 +1,27 @@ +import { DB } from "https://deno.land/x/sqlite/mod.ts"; +import { + app, + get, + post, + redirect, + contentType, +} from "https://denopkg.com/syumai/dinatra@0.15.0/mod.ts"; + +const db = new DB("ISIN.db", { mode: "read" }); + +app( + get("/api/:isin", ({ params }) => { + let query = `SELECT * from ISIN WHERE ISIN='${params.isin}'` + let res = db.query(query); + + if (res.length == 1) { + let [isin,description,issuer,type,status] = res[0] + if (issuer == "null") { + issuer = null + } + return [200, contentType("json"), JSON.stringify({ isin,description,issuer,type,status })] + } else { + return[404, contentType("json"), "Invalid ISIN"] + } + }) +); \ No newline at end of file diff --git a/src/setup.sql b/src/setup.sql new file mode 100644 index 0000000..3d003d8 --- /dev/null +++ b/src/setup.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS "ISIN"("ISIN" TEXT, "Description" TEXT, "Issuer" TEXT, "Type" TEXT COLLATE NOCASE, "Status" TEXT COLLATE NOCASE); +CREATE UNIQUE INDEX isinidx on ISIN (ISIN); +CREATE INDEX statusidx on ISIN (Status); +CREATE INDEX typeidx on ISIN (Type); +.import /tmp/ISIN-no-headers.csv ISIN --csv \ No newline at end of file