@@ -2,6 +2,7 @@ import fs from "node:fs";
22import { createServer } from "node:http" ;
33import type { Socket } from "node:net" ;
44import path from "node:path" ;
5+ import { execSync } from "node:child_process" ;
56import fastifyMiddie from "@fastify/middie" ;
67import fastifyStatic from "@fastify/static" ;
78// @ts -expect-error shut
@@ -80,59 +81,71 @@ function Revert() {
8081 }
8182}
8283
83- const port = Number . parseInt ( process . env . PORT as string ) || inConfig . port || 8080 ;
84- const app = Fastify ( {
85- serverFactory : ( handler ) =>
86- createServer ( handler ) . on ( "upgrade" , ( req , socket : Socket , head ) =>
87- req . url ?. startsWith ( "/f" )
88- ? wisp . routeRequest ( req , socket , head )
89- : socket . destroy ( ) ,
90- ) ,
91- } ) ;
92-
93- // This is necessary to randomize the page names while preventing them from being committed by contributors.
94- if ( ! fs . existsSync ( "dist" ) ) {
95- RandomizeNames ( ) ;
96- console . log ( "Interstellar's not built yet! Building now..." ) ;
97- await build ( { } ) . catch ( ( err ) => {
98- console . error ( err ) ;
99- process . exit ( 1 ) ;
84+ async function Start ( ) {
85+ const FirstRun = process . env . FIRST === "true" ;
86+
87+ if ( ! fs . existsSync ( "dist" ) ) {
88+ RandomizeNames ( ) ;
89+ console . log ( "Interstellar's not built yet! Building now..." ) ;
90+ await build ( { } ) . catch ( ( err ) => {
91+ console . error ( err ) ;
92+ process . exit ( 1 ) ;
93+ } ) ;
94+ Revert ( ) ;
95+
96+ if ( FirstRun ) {
97+ console . log ( "Restarting Server..." ) ;
98+ execSync ( "pnpm disable && pnpm start" , { stdio : "inherit" } ) ;
99+ process . exit ( 0 ) ;
100+ }
101+ }
102+
103+ const port = Number . parseInt ( process . env . PORT as string ) || inConfig . port || 8080 ;
104+ const app = Fastify ( {
105+ serverFactory : ( handler ) =>
106+ createServer ( handler ) . on ( "upgrade" , ( req , socket : Socket , head ) =>
107+ req . url ?. startsWith ( "/f" )
108+ ? wisp . routeRequest ( req , socket , head )
109+ : socket . destroy ( ) ,
110+ ) ,
111+ } ) ;
112+
113+ await app . register ( import ( "@fastify/compress" ) , {
114+ encodings : [ "br" , "gzip" , "deflate" ] ,
100115 } ) ;
101- Revert ( ) ;
102- }
103116
104- await app . register ( import ( "@fastify/compress" ) , {
105- encodings : [ "br" , "gzip" , "deflate" ] ,
106- } ) ;
107-
108- if ( inConfig . auth ?. challenge ) {
109- await app . register ( import ( "@fastify/basic-auth" ) , {
110- authenticate : true ,
111- validate ( username , password , _req , _reply , done ) {
112- for ( const [ user , pass ] of Object . entries ( inConfig . auth ?. users || { } ) ) {
113- if ( username === user && password === pass ) {
114- return done ( ) ;
117+ if ( inConfig . auth ?. challenge ) {
118+ await app . register ( import ( "@fastify/basic-auth" ) , {
119+ authenticate : true ,
120+ validate ( username , password , _req , _reply , done ) {
121+ for ( const [ user , pass ] of Object . entries ( inConfig . auth ?. users || { } ) ) {
122+ if ( username === user && password === pass ) {
123+ return done ( ) ;
124+ }
115125 }
116- }
117- return done ( new Error ( "Invalid credentials" ) ) ;
118- } ,
126+ return done ( new Error ( "Invalid credentials" ) ) ;
127+ } ,
128+ } ) ;
129+ await app . after ( ) ;
130+ app . addHook ( "onRequest" , app . basicAuth ) ;
131+ }
132+
133+ // @ts -ignore
134+ const { handler } = await import ( "./dist/server/entry.mjs" ) ;
135+ await app
136+ . register ( fastifyStatic , {
137+ root : path . join ( import . meta. dirname , "dist" , "client" ) ,
138+ } )
139+ . register ( fastifyMiddie ) ;
140+ app . use ( handler ) ;
141+ app . listen ( { port } , ( err , addr ) => {
142+ if ( err ) {
143+ console . error ( err ) ;
144+ process . exit ( 1 ) ;
145+ }
146+ console . log ( "Listening on %s" , addr ) ;
119147 } ) ;
120- await app . after ( ) ;
121- app . addHook ( "onRequest" , app . basicAuth ) ;
122148}
123149
124- // @ts -ignore
125- const { handler } = await import ( "./dist/server/entry.mjs" ) ;
126- await app
127- . register ( fastifyStatic , {
128- root : path . join ( import . meta. dirname , "dist" , "client" ) ,
129- } )
130- . register ( fastifyMiddie ) ;
131- app . use ( handler ) ;
132- app . listen ( { port } , ( err , addr ) => {
133- if ( err ) {
134- console . error ( err ) ;
135- process . exit ( 1 ) ;
136- }
137- console . log ( "Listening on %s" , addr ) ;
138- } ) ;
150+ process . env . FIRST = process . env . FIRST || "true" ;
151+ await Start ( ) ;
0 commit comments