Better config file support

This commit is contained in:
Gnarwhal 2024-10-05 19:43:02 +00:00
parent 2bac7f434e
commit 5a6ac24773
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
6 changed files with 39 additions and 7 deletions

3
.gitignore vendored
View file

@ -25,8 +25,9 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
# local conig files
.env*.local
config.toml
# vercel
.vercel

View file

@ -14,7 +14,8 @@
"prism-react-renderer": "^2.4.0",
"react": "^18",
"react-dom": "^18",
"react-markdown": "^9.0.1"
"react-markdown": "^9.0.1",
"toml": "^3.0.0"
},
"devDependencies": {
"@types/node": "^20",

View file

@ -2,6 +2,7 @@ import type { Metadata } from 'next';
import Image from 'next/image';
import * as style from './page.css';
import config from '../config.ts';
import Content from './components/content';
import Copy from './components/copy';
import download_image_dark from './download_dark.svg';
@ -33,9 +34,8 @@ export async function generateMetadata(
export default async function Page({
params, searchParams
}: Props) {
const enable_repointing = process.env.ENABLE_REPOINTING == 'true';
const path = get_path(params.file);
const root = get_root(enable_repointing, searchParams);
const root = get_root(config.enable_repointing, searchParams);
const full = `${root}${path}`;
const download_ttys = [
@ -69,7 +69,7 @@ export default async function Page({
<div className={style.download_tty_group}>
{download_ttys.map((text, index) => <Copy key={index} className={style.download_tty} text={text} show_text={true}></Copy>)}
</div>
{(!enable_repointing && searchParams['root'] != undefined) ?
{(!config.enable_repointing && searchParams['root'] != undefined) ?
<p>Error: this instance of Motto does not have repointing enabled</p> :
<Content src={`${root}${path}`} />
}

25
src/app/config.ts Normal file
View file

@ -0,0 +1,25 @@
import fs from 'fs';
import toml from 'toml';
type ConfigType = {
root_url: string,
enable_repointing: boolean,
};
function load_config(): ConfigType {
const config: ConfigType = {
// Default values
root_url: "",
enable_repointing: false,
};
const config_from_file = toml.parse(fs.readFileSync(`${process.cwd()}/config.toml`));
for (const key in config) {
if (key in config_from_file) {
config[key] = config_from_file[key];
}
}
return config;
}
export default load_config();

View file

@ -1,7 +1,7 @@
import * as style from './page.css';
import config from './config.ts';
export default function App() {
const supports_repointing = process.env.ENABLE_REPOINTING == 'true';
return (
<div className={style.center}>
<div className={style.content}>
@ -10,7 +10,7 @@ export default function App() {
Motto is a static file server beautifier. <a href="https://forge.monodon.me/Gnarwhal/motto/">Click here</a> for more information.
</p>
<p>This instance is currently pointed to <a href={process.env.ROOT_URL}>{process.env.ROOT_URL}</a> {
supports_repointing ?
config.enable_repointing ?
<>but supports <a href="https://forge.monodon.me/Gnarwhal/motto#Repointing">repointing.</a></> :
<>and does not support <a href="https://forge.monodon.me/Gnarwhal/motto#Repointing">repointing.</a></>
}</p>

View file

@ -3802,6 +3802,11 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"
toml@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee"
integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==
trim-lines@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/trim-lines/-/trim-lines-3.0.1.tgz#d802e332a07df861c48802c04321017b1bd87338"