-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch PHP prestart from Perl to JS (#1000)
* Switch PHP prestart from Perl to JS * Update snapshots * gosh hecking csharp dev kit * fix /app/storage permissions * Fix snapshots * hecking clippy
- Loading branch information
1 parent
125659a
commit 58c8a81
Showing
20 changed files
with
200 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { readFile, writeFile } from "fs/promises"; | ||
import { getNixPath } from "../util/nix.mjs"; | ||
|
||
const replaceStr = input => | ||
input | ||
// If statements | ||
.replaceAll(/\$if\s*\((\w+)\)\s*\(([^]*?)\)\s*else\s*\(([^]*?)\)/gm, | ||
(_all, condition, value, otherwise) => | ||
process.env[condition] ? replaceStr(value) : replaceStr(otherwise) | ||
) | ||
// Variables | ||
.replaceAll(/\${(\w+)}/g, | ||
(_all, name) => process.env[name] | ||
) | ||
// Nix paths | ||
.replaceAll(/\$!{(\w+)}/g, | ||
(_all, exe) => getNixPath(exe) | ||
) | ||
|
||
export async function compileTemplate(infile, outfile) { | ||
await writeFile(outfile, | ||
replaceStr(await readFile(infile, { encoding: 'utf8' })), | ||
{ encoding: 'utf8' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env node | ||
import { compileTemplate } from "./config/template.mjs"; | ||
import { e } from "./util/cmd.mjs"; | ||
import { checkEnvErrors, isLaravel } from "./util/laravel.mjs"; | ||
import Logger from "./util/logger.mjs"; | ||
import { access, constants } from 'node:fs/promises' | ||
|
||
const prestartLogger = new Logger('prestart'); | ||
const serverLogger = new Logger('server'); | ||
|
||
if (process.argv.length != 4) { | ||
prestartLogger.error(`Usage: ${process.argv[1]} <config-file> <output-file>`) | ||
process.exit(1); | ||
} | ||
|
||
if (isLaravel()) { | ||
checkEnvErrors('/app') | ||
} | ||
|
||
await Promise.all([ | ||
access('/app/storage', constants.R_OK) | ||
.then(() => e('chmod -R ugo+rw /app/storage')) | ||
.catch(() => {}), | ||
compileTemplate(process.argv[2], process.argv[3]) | ||
]).catch(err => prestartLogger.error(err)); | ||
|
||
serverLogger.info(`Server starting on port ${process.env.PORT}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { execSync } from "child_process"; | ||
|
||
export const e = cmd => execSync(cmd).toString().replace('\n', ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Logger from "./logger.mjs" | ||
import * as fs from 'node:fs/promises' | ||
import * as path from 'node:path' | ||
|
||
const variableHints = { | ||
'APP_ENV': 'You should probably set this to `production`.' | ||
}; | ||
|
||
const logger = new Logger('laravel'); | ||
|
||
export const isLaravel = () => process.env['IS_LARAVEL'] != null; | ||
|
||
function checkVariable(name) { | ||
if (!process.env[name]) { | ||
let hint = | ||
`Your app configuration references the ${name} environment variable, but it is not set.` | ||
+ (variableHints[name] ?? ''); | ||
|
||
logger.warn(hint); | ||
} | ||
} | ||
|
||
export async function checkEnvErrors(srcdir) { | ||
const envRegex = /env\(["']([^,]*)["']\)/g; | ||
const configDir = path.join(srcdir, 'config'); | ||
|
||
const config = | ||
(await Promise.all( | ||
(await fs.readdir(configDir)) | ||
.filter(fileName => fileName.endsWith('.php')) | ||
.map(fileName => fs.readFile(path.join(configDir, fileName))) | ||
)).join(''); | ||
|
||
for (const match of config.matchAll(envRegex)) { | ||
if (match[1] != 'APP_KEY') checkVariable(match[1]); | ||
} | ||
|
||
if (!process.env.APP_KEY) { | ||
logger.warn('Your app key is not set! Please set a random 32-character string in your APP_KEY environment variable. This can be easily generated with `openssl rand -hex 16`.'); | ||
} | ||
} |
Oops, something went wrong.