@@ -4,46 +4,16 @@ import { swagger } from "@elysiajs/swagger";
44import { serverTiming } from "@elysiajs/server-timing" ;
55import { routes } from "@/api" ;
66import { loggerPlugin } from "@/plugins/loggerPlugin" ;
7- import { ALLOWED_ORIGINS , PORT , PRODUCTION } from "@/utils/config" ;
7+ import { ALLOWED_ORIGINS , PORT , PRODUCTION , FRONTEND_DIR } from "@/utils/config" ;
88import { consola } from "consola" ;
99import { initConfig } from "./utils/init" ;
1010import { staticPlugin } from "@elysiajs/static" ;
1111import { exists , readFile } from "node:fs/promises"
12+ import { join , resolve } from "node:path" ;
1213
1314await initConfig ( ) ;
1415
15- const staticRoute = await ( async ( ) => {
16- let elysia = new Elysia ( ) ;
17- if ( await exists ( "dist" ) ) {
18- elysia = elysia . use ( staticPlugin ( {
19- assets : "dist" ,
20- alwaysStatic : true ,
21- prefix : "/" ,
22- } ) )
23- if ( await exists ( "dist/index.html" ) ) {
24- const indexHtml = await readFile ( "dist/index.html" , "utf-8" ) ;
25- elysia = elysia . get ( "/*" , ( { headers : { accept } } ) => {
26- if ( typeof accept === "string" && ! accept . includes ( "text/html" ) ) {
27- return new Response ( "Not Found" , {
28- status : 404 ,
29- } ) ;
30- }
31- return new Response ( indexHtml , {
32- headers : {
33- "Content-Type" : "text/html" ,
34- } ,
35- } ) ;
36- } , {
37- headers : t . Object ( {
38- accept : t . Optional ( t . String ( ) )
39- } )
40- } )
41- }
42- }
43- return elysia ;
44- } ) ( )
45-
46- const app = new Elysia ( )
16+ let app = new Elysia ( )
4717 . use ( loggerPlugin )
4818 . use (
4919 cors ( {
@@ -75,14 +45,39 @@ const app = new Elysia()
7545 } ) ,
7646 )
7747 . use ( serverTiming ( ) )
78- . use ( routes )
79- . use ( staticRoute )
80- . listen ( {
81- port : PORT ,
82- reusePort : PRODUCTION ,
83- hostname : "0.0.0.0" ,
84- idleTimeout : 255 ,
85- } ) ;
48+ . use ( routes ) ;
49+
50+ if ( await exists ( FRONTEND_DIR ) ) {
51+ const dir = resolve ( FRONTEND_DIR ) ;
52+ consola . log ( `Setting up static file serving from ${ dir } ` ) ;
53+ app = app . use ( staticPlugin ( {
54+ assets : dir ,
55+ alwaysStatic : true ,
56+ prefix : "/" ,
57+ } ) )
58+ if ( await exists ( join ( dir , "index.html" ) ) ) {
59+ const indexHtml = await readFile ( join ( dir , "index.html" ) , "utf-8" ) ;
60+ app = app . get ( "/*" , ( { headers : { accept } } ) => {
61+ if ( typeof accept === "string" && ! accept . includes ( "text/html" ) ) {
62+ return new Response ( "Not Found" , {
63+ status : 404 ,
64+ } ) ;
65+ }
66+ return new Response ( indexHtml , {
67+ headers : {
68+ "Content-Type" : "text/html" ,
69+ } ,
70+ } ) ;
71+ } ) ;
72+ }
73+ }
74+
75+ app = app . listen ( {
76+ port : PORT ,
77+ reusePort : PRODUCTION ,
78+ hostname : "0.0.0.0" ,
79+ idleTimeout : 255 ,
80+ } ) ;
8681
8782consola . log ( `🦊 Elysia is running at ${ app . server ?. hostname } :${ app . server ?. port } ` ) ;
8883
0 commit comments