Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #42 from Tolfix/dev
Browse files Browse the repository at this point in the history
Stable build
  • Loading branch information
Tolfx authored Jul 29, 2021
2 parents 2fffefe + 5fe49f4 commit ec5ba18
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { config } from "docker-compose";
import { IConfig, ISMTP } from "./Interfaces/Config";
import { IConfigMapIndex } from "./Interfaces/ConfigMap";
import AW from "./Lib/Async";
import ConfigModel from "./Models/Config";

export const DebugMode = process.env.DEBUG === "true" ? true : false;
export const MongoDB_URI = process.env.MONGODB_URI ?? "mongodb://localhost/dmcd";
export const Web_Title = process.env.TITLE ?? "DMCD";
export const PORT = 56251;
export const Session_Secret = process.env.SESSION_SECRET ?? undefined;
export const GetSMTPConfig = () => {
Expand All @@ -26,7 +26,9 @@ export const GetSMTPConfig = () => {
})
};
export const ConfigMap = new Map<keyof IConfigMapIndex, IConfigMapIndex[keyof IConfigMapIndex]>();
export const DockerDir = ((__dirname.replace("\\build", "")).replace("/build", ""))+"/Docker";
export const Dir = ((__dirname.replace("\\build", "")).replace("/build", ""));
export const DockerDir = Dir+"/Docker";

ConfigMap.set("domain", process.env.DOMAIN ?? "localhost")
ConfigMap.set("http", process.env.HTTP as "https" ?? "http")
ConfigMap.set("title", process.env.TITLE ?? "DMCD");
ConfigMap.set("domain", process.env.DOMAIN ?? "localhost");
ConfigMap.set("http", process.env.HTTP as "https" ?? "http");
1 change: 1 addition & 0 deletions src/Interfaces/ConfigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface IConfigMapIndex
{
domain: string;
http: "http" | "https";
title: string;
}
8 changes: 8 additions & 0 deletions src/Interfaces/ENV.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface IENV {
SESSION_SECRET: string;
TITLE: string;
MONGODB_URI: string;
DEBUG: string;
DOMAIN: string;
HTTP: string;
}
3 changes: 2 additions & 1 deletion src/Lib/Async.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Promise<P> extends P ? P extends P ? any : Promise<P extends P ? P : any> : any
export default async function
AW<P>(data: Promise<P> extends P ? P extends P ? any : Promise<P extends P ? P : any> : any)
: Promise<[P | null, PromiseRejectedResult | null]>
Expand All @@ -7,6 +8,6 @@ export default async function
return [await data, null];
} catch (e)
{
return [null, e];
return [null, e as PromiseRejectedResult];
}
}
2 changes: 1 addition & 1 deletion src/Lib/CDSocket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Active, Building, Failed, Statues } from "../Types/Statues";
import { Active, Building, Failed } from "../Types/Statues";

//@ts-nocheck
export function getCDSocketFail<Name extends string>(name: Name): `cd-${Name}-fail`
Expand Down
32 changes: 32 additions & 0 deletions src/Lib/EditEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from "fs";
import { Dir } from "../Config";
import { IENV } from "../Interfaces/ENV";
import { env_seperator } from "./Seperator";

export function EditEnvFile(envOptions: Partial<IENV>)
{
// Get our file and parse through each.
const envFile = fs.readFileSync(Dir+"/.env").toString("ascii");
const envs = envFile.split("\n");

let tempEnvOptions: typeof envOptions = {};

for(const env of envs)
{
const { value, name } = env_seperator<keyof IENV>(env);
tempEnvOptions[name] = value;

// Replace data
if(envOptions[name] && envOptions[name] !== tempEnvOptions[name])
tempEnvOptions[name] = envOptions[name];
}

let data = ``;

for(const key in tempEnvOptions)
{
data += `${key}=${tempEnvOptions[key as keyof IENV]}\n`
}

fs.writeFileSync(Dir+"/.env", data);
}
6 changes: 3 additions & 3 deletions src/Lib/Email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function SendEmail(
subject: string,
body: IBodyEmail,
callback?: (error: Error|null, sent: Boolean|null) => void
): Promise<void>
): Promise<Boolean | void>
{
const [SMTPConfig, SMTP_Error] = await AW(GetSMTPConfig());
if(!SMTPConfig || SMTP_Error)
Expand Down Expand Up @@ -43,8 +43,8 @@ export async function SendEmail(
const transport = mail.createTransport(config);

transport.sendMail(email).then(e => {
callback?.(null, true);
callback ? callback?.(null, true) : Promise.resolve(true);
}).catch(e => {
callback?.(e, false);
callback ? callback?.(e, false) : Promise.resolve(false);
});
}
10 changes: 6 additions & 4 deletions src/Lib/Seperator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function env_seperator(env: string)
export function env_seperator
<Key extends string = string, Value extends string = string>
(env: string): { value: Value, name: Key }
{
let value, name;
let value: Value, name: Key;
let indexOfName = 0;

for(let char of env)
Expand All @@ -10,8 +12,8 @@ export function env_seperator(env: string)
indexOfName++;
}

value = env.slice(indexOfName+1);
name = env.slice(0, indexOfName)
value = env.slice(indexOfName+1) as Value;
name = env.slice(0, indexOfName) as Key;

return {
value,
Expand Down
43 changes: 43 additions & 0 deletions src/Routers/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ConfigModel from "../Models/Config";
import { IConfig } from "../Interfaces/Config";
import { SendEmail } from "../Lib/Email";
import { ConfigMap } from "../Config";
import { EditEnvFile } from "../Lib/EditEnv";
export default class ConfigRouter {
protected server: Application;
protected router: Router;
Expand Down Expand Up @@ -141,11 +142,53 @@ export default class ConfigRouter {
ConfigMap.set("domain", domain);
ConfigMap.set("http", ssl === "on" ? "https" : "http");

EditEnvFile({
DOMAIN: domain,
HTTP: ssl === "on" ? "https" : "http"
});

await Config[0].save();

req.flash("success", "Succesfully changed domain settings, ensure to restart too.");
return res.redirect("back");
});

this.router.post("/edit/title", async (req, res) => {

const title = req.body.title;

if(!title)
{
req.flash("error", `Please include a title in the body`);
return res.redirect("back");
}

const [Config, C_Error] = await AW<IConfig[]>(ConfigModel.find());

if(!Config || C_Error)
{
if(C_Error)
log.error(C_Error);

req.flash("error", `Something went wrong, try again later.`);
return res.redirect("back");
};

let config = Config[0];

config.title = config.title === title ? config.title : title;

ConfigMap.set("title", title);

EditEnvFile({
TITLE: title
});

await config.save();

req.flash("success", "Succesfully changed domain settings, ensure to restart too.");
return res.redirect("back");
});

}
}
4 changes: 2 additions & 2 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cors from "cors";
import compileSass from "node-sass-middleware";
import methodOverride from "method-override";
import log from "./Lib/Logger";
import { PORT, Web_Title, MongoDB_URI, Session_Secret, DebugMode, ConfigMap } from "./Config"
import { PORT, MongoDB_URI, Session_Secret, DebugMode, ConfigMap } from "./Config"
import Auth from "./Passport/Auth";
import MainRouter from "./Routers/Main";
import MongodbEvent from "./Events/Mongodb";
Expand Down Expand Up @@ -77,7 +77,7 @@ server.use((req, res, next) => {
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');

res.locals.title = Web_Title;
res.locals.title = ConfigMap.get("title");

res.locals.isAuth = req.isAuthenticated();

Expand Down
11 changes: 11 additions & 0 deletions views/Config/Main.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@
<button type="submit" class="btn btn-danger">Change Domain Settings</button>
</form>
</div>
<div>
<form action="/config/edit/title" method="POST">

<div class="mb-3">
<label for="password">Domain settings</label>
<input class="form-control mb-2" type="text" name="title" value="<%= title %>" placeholder="DMCD" required>
</div>

<button type="submit" class="btn btn-danger">Change Title</button>
</form>
</div>
</div>
</div>
<div>
Expand Down

0 comments on commit ec5ba18

Please sign in to comment.