1- import Elysia from ' elysia' ;
2- import * as db from ' ./postgres' ;
3- import { Fortune } from ' ./types' ;
1+ import { Elysia , t } from " elysia" ;
2+ import * as db from " ./postgres" ;
3+ import { Fortune } from " ./types" ;
44
5- function rand ( ) {
6- return Math . ceil ( Math . random ( ) * 10000 )
5+ function rand ( ) {
6+ return Math . ceil ( Math . random ( ) * 10000 ) ;
77}
88
9- function parseQueriesNumber ( q ?: string ) {
10- return Math . min ( parseInt ( q || '1' ) || 1 , 500 )
9+ function parseQueriesNumber ( q ?: string ) {
10+ return Math . min ( parseInt ( q || "1" ) || 1 , 500 ) ;
1111}
1212
13- function renderTemplate ( fortunes : Fortune [ ] ) {
13+ function renderTemplate ( fortunes : Fortune [ ] ) {
1414 const n = fortunes . length ;
1515
16- let html = '' ;
16+ let html = "" ;
1717 for ( let i = 0 ; i < n ; i ++ ) {
1818 html += `<tr><td>${ fortunes [ i ] . id } </td><td>${ Bun . escapeHTML (
19- fortunes [ i ] . message
19+ fortunes [ i ] . message ,
2020 ) } </td></tr>`;
2121 }
2222
2323 return `<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>${ html } </table></body></html>` ;
2424}
2525
26- const dbHandlers = new Elysia ( {
27- name : 'db-handlers' ,
28- } )
29- . onAfterHandle ( ( { set } ) => {
30- set . headers [ 'server' ] = 'Elysia' ;
26+ export const dbHandlers = new Elysia ( )
27+ . headers ( {
28+ server : "Elysia" ,
3129 } )
32-
33- . get ( '/db' , ( ) => db . find ( rand ( ) ) )
34-
35- . get ( '/fortunes' , async ( { set } ) => {
30+ . get ( "/db" , ( ) => db . find ( rand ( ) ) )
31+ . get ( "/fortunes" , async ( c ) => {
3632 const fortunes = await db . fortunes ( ) ;
3733
3834 fortunes . push ( {
3935 id : 0 ,
40- message : 'Additional fortune added at request time.' ,
36+ message : "Additional fortune added at request time." ,
37+ } ) ;
38+
39+ fortunes . sort ( ( a , b ) => {
40+ if ( a . message < b . message ) return - 1 ;
41+
42+ return 1 ;
4143 } ) ;
4244
43- fortunes . sort ( ( a , b ) => ( a . message < b . message ? - 1 : 1 ) ) ;
45+ c . set . headers [ "content-type" ] = "text/html; charset=utf-8" ;
4446
45- set . headers [ 'content-type' ] = 'text/html; charset=utf-8' ;
4647 return renderTemplate ( fortunes ) ;
4748 } )
48-
49- . get ( '/queries' , async ( { query } ) => {
50- const num = parseQueriesNumber ( query . queries )
49+ . get ( "/queries" , ( c ) => {
50+ const num = parseQueriesNumber ( c . query . queries ) ;
5151 const worldPromises = new Array ( num ) ;
5252
5353 for ( let i = 0 ; i < num ; i ++ ) {
5454 worldPromises [ i ] = db . find ( rand ( ) ) ;
5555 }
5656
57- return await Promise . all ( worldPromises ) ;
57+ return Promise . all ( worldPromises ) ;
5858 } )
59-
60- . get ( '/updates' , async ( { query } ) => {
61- const num = parseQueriesNumber ( query . queries )
59+ . get ( "/updates" , async ( c ) => {
60+ const num = parseQueriesNumber ( c . query . queries ) ;
6261 const worldPromises = new Array ( num ) ;
6362
6463 for ( let i = 0 ; i < num ; i ++ ) {
@@ -74,5 +73,3 @@ const dbHandlers = new Elysia({
7473 await db . bulkUpdate ( worlds ) ;
7574 return worlds ;
7675 } ) ;
77-
78- export default dbHandlers ;
0 commit comments