Skip to content

Commit

Permalink
use proxy + cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mciparelli committed Nov 11, 2023
1 parent 09fd513 commit 4195445
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 27 deletions.
39 changes: 13 additions & 26 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@
"lock": false,
"tasks": {
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
"start": "deno run -A --watch=static/,routes/ dev.ts",
"start": "deno run -A --watch=static/,routes/ --unstable dev.ts",
"build": "deno run -A dev.ts build",
"preview": "deno run -A main.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
},
"lint": {
"rules": {
"tags": [
"recommended"
],
"exclude": ["prefer-const"]
},
"exclude": [
"_fresh"
]
"rules": { "tags": ["recommended", "fresh"], "exclude": ["prefer-const"] }
},
"imports": {
"$fresh/": "https://deno.land/x/fresh@1.4.3/",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*[email protected].1",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3",
"$fresh/": "https://deno.land/x/fresh@1.5.4/",
"preact": "https://esm.sh/preact@10.18.1",
"preact/": "https://esm.sh/preact@10.18.1/",
"preact-render-to-string": "https://esm.sh/*[email protected].2",
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.1",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.0",
"twind": "https://esm.sh/[email protected]",
"twind/": "https://esm.sh/[email protected]/",
"$std/": "https://deno.land/[email protected]/",
Expand All @@ -38,15 +30,10 @@
"utils/": "./utils/",
"api": "./utils/api.js",
"islands/": "./islands/",
"juani/": "https://esm.sh/gh/juanidambrosio/smileshelper/"
"juani/": "https://esm.sh/gh/juanidambrosio/smileshelper/",
"cachified": "https://esm.sh/[email protected]",
"@libsql/client": "https://esm.sh/@libsql/[email protected]"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
},
"fmt": {
"exclude": [
"_fresh"
]
}
"compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
"exclude": ["**/_fresh/*"]
}
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as $0 from "./routes/_404.tsx";
import * as $1 from "./routes/_app.tsx";
import * as $2 from "./routes/index.tsx";
import * as $3 from "./routes/search.js";
import * as $$0 from "./islands/filters.jsx";
import * as $$1 from "./islands/footer.jsx";
import * as $$2 from "./islands/form-and-results.jsx";
Expand All @@ -15,6 +16,7 @@ const manifest = {
"./routes/_404.tsx": $0,
"./routes/_app.tsx": $1,
"./routes/index.tsx": $2,
"./routes/search.js": $3,
},
islands: {
"./islands/filters.jsx": $$0,
Expand Down
43 changes: 43 additions & 0 deletions routes/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { cachified, verboseReporter } from 'cachified';
import makeCache from 'utils/cache.js';

const client = Deno.createHttpClient({
proxy: {
basicAuth: {username: Deno.env.get('PROXY_USER'), password: Deno.env.get('PROXY_PW')},
url: `socks5h://pr.oxylabs.io:7777` }
});

const headers = {
authorization:
"Bearer Ghlpz2Fv1P5k9zGSUz2Z3l5jdVmy0aNECen0CV5v1sevBwTX9cA9kc",
"x-api-key": "aJqPU7xNHl9qN3NVZnPaJ208aPo2Bh2p2ZV844tw",
"Content-Type": "application/json",
Accept: "application/json",
region: "ARGENTINA",
};

function searchCached(params) {
return cachified({
reporter: verboseReporter(),
key: `${params.get('originAirportCode')}:${params.get('destinationAirportCode')}:${params.get('departureDate')}`,
cache: makeCache('smiles'),
getFreshValue() {
return fetch('https://api-air-flightsearch-prd.smiles.com.br/v1/airlines/search?' + params.toString(), {
client,
headers
}).then(res => res.json());
},
// good for 12 hours
ttl: 1000 * 60 * 60 * 12,
// one year
swr: 1000 * 60 * 60 * 24 * 365
});
}

export async function handler(req) {
const url = new URL(req.url);
const value = await searchCached(url.searchParams);
return new Response(JSON.stringify(value), {
headers: { "Content-Type": "application/json" },
});
}
31 changes: 31 additions & 0 deletions utils/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createClient } from '@libsql/client';

const client = createClient({
url: Deno.env.get('DB_URL'),
authToken: Deno.env.get('DB_AUTH_TOKEN')
});

export default function makeCache(prefix) {
return {
async set(key, value) {
await client.execute({
sql: "insert into cache values (:key, :value)",
args: { key: prefix + ':' + key, value: JSON.stringify(value) }
});
},
async get(key) {
const { rows: [cacheRow]} = await client.execute({
sql: 'select value from cache where key = :key',
args: { key: prefix + ':' + key }
});
if (!cacheRow) return null;
return JSON.parse(cacheRow.value)
},
async delete(key) {
await client.execute({
sql: 'delete from cache where key = :key',
args: { key: prefix + ':' + key }
});
}
};
}
2 changes: 1 addition & 1 deletion utils/smiles-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async function searchFlights(paramsObject) {
abortControllersSignal.value = [...abortControllersSignal.value, controller];
const params = new URLSearchParams({ ...defaultParams, ...paramsObject });
const response = await fetch(
"https://api-air-flightsearch-prd.smiles.com.br/v1/airlines/search?" +
"/search?" +
params.toString(),
{
signal: controller.signal,
Expand Down

0 comments on commit 4195445

Please sign in to comment.