Skip to content

Commit

Permalink
a little backend refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
StigNygaard committed Oct 13, 2024
1 parent ed752a0 commit 1b1bbac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ content. And see https://lastfm-widgets.deno.dev/ for more about widget *modes*

Frontend-code for the demo page seen on https://lastfm-widgets.deno.dev/

#### /proxy-api/ folder
#### /services/ folder

Contains *audioscrobbler.ts*, an example backend proxy-api made with Deno. The proxy-api is used on demo page when
- *proxy-api.ts* - An example backend proxy-api made with Deno. The proxy-api is used on demo page when
widget is in *Backend-supported* mode, but also used by widget on [rockland.dk](https://www.rockland.dk/).
- *log.ts* - A simple service used by demo page to log changes in widget state

#### /main.ts file

Expand Down
9 changes: 7 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {serveDir} from 'jsr:@std/http/file-server';
import 'jsr:@std/dotenv/load';
import {audioscrobbler} from './proxy-api/audioscrobbler.ts';
import {proxyApi} from './services/proxy-api.ts';
import {log} from './services/log.ts';

/**
* @run --allow-net --allow-env --allow-read=./demo,./widgets,./.env main.ts
Expand All @@ -14,8 +15,12 @@ Deno.serve(async (req: Request, info: Deno.ServeHandlerInfo) => {
// The "Router"...
if (/^\/proxy-api\/?$/.test(pathname)) {
// The "proxy API" - https://lastfm-widgets.deno.dev/proxy-api
const result = await audioscrobbler(url.searchParams, req.headers, info);
const result = await proxyApi(url.searchParams, req.headers, info);
return new Response(result.body, result.options);
} else if (/^\/log\/?$/.test(pathname)) {
// Simple "post object" log-endpoint - https://lastfm-widgets.deno.dev/log
log(url.searchParams, req, info);
return new Response('', {status: 200, statusText: 'OK'});
} else if (pathname.startsWith('/widgets/')) {
// The statically served widgets code - https://lastfm-widgets.deno.dev/widgets/*
return serveDir(req, {
Expand Down
6 changes: 3 additions & 3 deletions proxy-api/audioscrobbler.ts → services/proxy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let fetchErrorCount = 0;

let hibernate = false; // In case of error 26 or 29, enter Hibernate mode

export async function audioscrobbler(searchParams: URLSearchParams, reqHeaders: Headers, info: Deno.ServeHandlerInfo) : Promise<{body: string, options: object}> {
export async function proxyApi(searchParams: URLSearchParams, reqHeaders: Headers, info: Deno.ServeHandlerInfo) : Promise<{body: string, options: object}> {

const origin = reqHeaders.get('Origin');
const respHeaders = new Headers({'content-type': 'application/json'});
Expand All @@ -45,11 +45,11 @@ export async function audioscrobbler(searchParams: URLSearchParams, reqHeaders:
method: method,
date: new Date().toISOString(),
userAgent: reqHeaders.get('User-Agent') ?? '',
origin: reqHeaders.get('Origin') ?? '',
origin: origin ?? '',
referer: reqHeaders.get('Referer') ?? '',
...remoteAddr(info)
};
console.log(`[${fetchSuccessCount}/${fetchErrorCount}] logData: `, logData);
console.log(`[${fetchSuccessCount}/${fetchErrorCount}] proxy: `, logData);
}
const nextTime = parseInt(cache.get(`${method}-NextTime`) || '0', 10);
if (Date.now() <= nextTime) {
Expand Down

0 comments on commit 1b1bbac

Please sign in to comment.