Add Docker support and implement Bun for dependency management
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 1m25s
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 1m25s
- Introduced .dockerignore to exclude unnecessary files from Docker context. - Updated Dockerfile to use Bun for building and running the application. - Removed entrypoint.sh as it is no longer needed. - Modified build-and-dockerise.yml to set up Bun and install dependencies. - Enhanced index.html with improved structure and hover effects. - Updated client.ts to include tooltips for music display. - Implemented caching in server.ts for Last.fm data. - Added custom styles for hover effects and tooltips in style.css.
This commit is contained in:
parent
be5a61185b
commit
214982649f
9 changed files with 272 additions and 107 deletions
|
@ -1,6 +1,35 @@
|
|||
import { Elysia, t } from 'elysia';
|
||||
import { staticPlugin } from '@elysiajs/static';
|
||||
|
||||
// Simple cache implementation
|
||||
interface CacheEntry {
|
||||
data: any;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
const cache = new Map<string, CacheEntry>();
|
||||
const CACHE_TTL = 30 * 1000; // 30 seconds in milliseconds
|
||||
|
||||
function getCachedData(key: string): any | null {
|
||||
const entry = cache.get(key);
|
||||
if (!entry) return null;
|
||||
|
||||
const now = Date.now();
|
||||
if (now - entry.timestamp > CACHE_TTL) {
|
||||
cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
function setCachedData(key: string, data: any): void {
|
||||
cache.set(key, {
|
||||
data,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
const app = new Elysia()
|
||||
.use(staticPlugin({
|
||||
assets: 'dist',
|
||||
|
@ -17,13 +46,22 @@ const app = new Elysia()
|
|||
);
|
||||
}
|
||||
|
||||
// Check cache first
|
||||
const cacheKey = `lastfm:${username}`;
|
||||
const cachedData = getCachedData(cacheKey);
|
||||
if (cachedData) {
|
||||
console.log('Returning cached Last.fm data');
|
||||
return cachedData;
|
||||
}
|
||||
|
||||
const url = `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${username}&api_key=${apiKey}&format=json&limit=1`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
const track = data.recenttracks.track[0];
|
||||
return {
|
||||
|
||||
const result = {
|
||||
name: track.name,
|
||||
artist: track.artist['#text'],
|
||||
album: track.album['#text'],
|
||||
|
@ -31,6 +69,12 @@ const app = new Elysia()
|
|||
nowPlaying: track['@attr']?.nowplaying === 'true',
|
||||
image: track.image || [],
|
||||
};
|
||||
|
||||
// Cache the result
|
||||
setCachedData(cacheKey, result);
|
||||
console.log('Fetched and cached new Last.fm data');
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('Error fetching from Last.fm:', error);
|
||||
return new Response(JSON.stringify({ error: 'Failed to fetch last song' }), {
|
||||
|
@ -39,7 +83,10 @@ const app = new Elysia()
|
|||
});
|
||||
}
|
||||
})
|
||||
.listen(3000);
|
||||
.listen({
|
||||
port: process.env.PORT || 3000,
|
||||
hostname: process.env.NODE_ENV === 'production' ? '0.0.0.0' : 'localhost'
|
||||
});
|
||||
|
||||
console.log(
|
||||
`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue