|
| 1 | +import { AngularAppEngine, createRequestHandler } from '@angular/ssr'; |
| 2 | +import { isMainModule } from '@angular/ssr/node'; |
| 3 | +import { Elysia } from 'elysia'; |
| 4 | +import { staticPlugin } from '@elysiajs/static'; |
| 5 | +import { dirname, resolve } from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import { parse } from 'valibot'; |
| 8 | +import podcasts from '../public/assets/data/podcast.json'; |
| 9 | +import { PodcastListSchema } from './server/schemas/podcast.schema'; |
| 10 | +import { CommunityListSchema } from './server/schemas/community.schema'; |
| 11 | +import organizers from '../public/assets/data/organizers.json'; |
| 12 | +import events from '../public/assets/data/events.json'; |
| 13 | +import { Community } from './models/community.model'; |
| 14 | +import { Podcast } from './models/podcast.model'; |
| 15 | +import { isFuture, isToday } from 'date-fns'; |
| 16 | +import { EventCallForPapers } from './models/call-for-papers.model'; |
| 17 | +import { Event } from './models/event.model'; |
| 18 | +import { CommunityEvent } from './models/community-event.model'; |
| 19 | + |
| 20 | +export function app() { |
| 21 | + const server = new Elysia(); |
| 22 | + const angularAppEngine = new AngularAppEngine(); |
| 23 | + |
| 24 | + const serverDistFolder = dirname(fileURLToPath(import.meta.url)); |
| 25 | + const browserDistFolder = resolve(serverDistFolder, '../browser'); |
| 26 | + |
| 27 | + server.use( |
| 28 | + staticPlugin({ |
| 29 | + prefix: '', |
| 30 | + assets: browserDistFolder, |
| 31 | + alwaysStatic: true, |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + server |
| 36 | + .get('/*', async (c) => { |
| 37 | + const res = await angularAppEngine.handle(c.request, { |
| 38 | + server: 'elysia', |
| 39 | + }); |
| 40 | + return res || undefined; |
| 41 | + }) |
| 42 | + .get( |
| 43 | + '/api/v1/podcasts', |
| 44 | + async ({ |
| 45 | + query: { order = 'DESC' }, |
| 46 | + }: { |
| 47 | + query: { order: 'ASC' | 'DESC' }; |
| 48 | + }) => { |
| 49 | + try { |
| 50 | + parse(PodcastListSchema, podcasts); |
| 51 | + |
| 52 | + return podcasts.sort((a: Podcast, b: Podcast) => |
| 53 | + orderPredicate(order, a.name, b.name), |
| 54 | + ); |
| 55 | + } catch (error) { |
| 56 | + throw new Error('Invalid podcast data format'); |
| 57 | + } |
| 58 | + }, |
| 59 | + ) |
| 60 | + .get( |
| 61 | + '/api/v1/communities', |
| 62 | + async ({ |
| 63 | + query: { order = 'DESC' }, |
| 64 | + }: { |
| 65 | + query: { order: 'ASC' | 'DESC' }; |
| 66 | + }) => { |
| 67 | + try { |
| 68 | + // parse(CommunityListSchema, communities); |
| 69 | + return (organizers as Community[]).filter( |
| 70 | + (community) => |
| 71 | + community.type !== 'workshop' && community.type !== 'other', |
| 72 | + ); |
| 73 | + } catch (error) { |
| 74 | + throw new Error('Invalid community data format'); |
| 75 | + } |
| 76 | + }, |
| 77 | + ) |
| 78 | + .get('/api/v1/communities/callforpapers', async () => { |
| 79 | + try { |
| 80 | + return []; |
| 81 | + } catch (error) { |
| 82 | + throw new Error('Invalid community data format'); |
| 83 | + } |
| 84 | + }) |
| 85 | + .get('/api/v1/events', async ({ query }) => { |
| 86 | + try { |
| 87 | + return []; |
| 88 | + } catch (error) { |
| 89 | + throw new Error('Invalid community data format'); |
| 90 | + } |
| 91 | + }) |
| 92 | + .get('/api/v1/events/callforpapers', async () => { |
| 93 | + try { |
| 94 | + return []; |
| 95 | + } catch (error) { |
| 96 | + console.error(error); |
| 97 | + throw new Error('Invalid community data format'); |
| 98 | + } |
| 99 | + }) |
| 100 | + .get('/api/v1/events/upcoming', async () => { |
| 101 | + try { |
| 102 | + const communityEvents: CommunityEvent[] = (events as Event[]) |
| 103 | + .filter((event) => { |
| 104 | + const date = new Date(event.date); |
| 105 | + return isToday(date) || isFuture(date) || event.toBeAnnounced; |
| 106 | + }) |
| 107 | + .map((event) => { |
| 108 | + const organizer = organizers.find( |
| 109 | + (organizer) => organizer.id === event.organizerId, |
| 110 | + ); |
| 111 | + return { |
| 112 | + ...event, |
| 113 | + name: event.name ?? organizer?.name ?? '', |
| 114 | + organizer: { |
| 115 | + name: organizer?.name ?? '', |
| 116 | + url: organizer?.websiteUrl ?? '', |
| 117 | + logo: organizer?.logo ?? '', |
| 118 | + }, |
| 119 | + }; |
| 120 | + }) |
| 121 | + .sort( |
| 122 | + (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(), |
| 123 | + ); |
| 124 | + |
| 125 | + return communityEvents; |
| 126 | + } catch (error) { |
| 127 | + throw new Error(error as string); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + console.warn('Elysia server started'); |
| 132 | + |
| 133 | + return server; |
| 134 | +} |
| 135 | + |
| 136 | +const server = app(); |
| 137 | +if (isMainModule(import.meta.url)) { |
| 138 | + const port = process.env['PORT'] || 4000; |
| 139 | + |
| 140 | + server.listen(port, () => { |
| 141 | + console.log(`Elysia server listening on http://localhost:${port}`); |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +function orderPredicate(order: 'ASC' | 'DESC' = 'DESC', a: string, b: string) { |
| 146 | + if (order === 'DESC') { |
| 147 | + return a.localeCompare(b); |
| 148 | + } else { |
| 149 | + return b.localeCompare(a); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function applyQueryFilter(event: any, query: Record<string, any>): boolean { |
| 154 | + for (const key in query) { |
| 155 | + if (event[key] !== Boolean(query[key])) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + return true; |
| 160 | +} |
| 161 | + |
| 162 | +export const reqHandler = createRequestHandler(server.fetch); |
0 commit comments