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 #41 from Tolfix/dev
Browse files Browse the repository at this point in the history
Config Update & More
  • Loading branch information
Tolfx authored Jul 22, 2021
2 parents e72a0d0 + baf09ce commit 2fffefe
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 33 deletions.
6 changes: 3 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ install_docker() {
}

create_admin() {
npm run create-admin $MONGO_URI $USER_PASSWORD
npm run create-admin $MONGO_URI $USER_PASSWORD $FQDN $IS_SSL
}

configure_nginx() {
Expand All @@ -149,7 +149,8 @@ configure_nginx() {
configure_ssl() {
certbot --nginx --redirect --no-eff-email --email "$USER_EMAIL" -d "$FQDN" || FAILED_SSL=true

if [ "$FAILED_SSL" = false ]; then
if [ "$FAILED_SSL" = "false" ]; then
IS_SSL=true
PROTOCOL=https
fi
}
Expand Down Expand Up @@ -218,7 +219,6 @@ main() {

if [[ "$CONFIRM_SSL" =~ [Yy] ]]; then
CONFIGURE_SSL=true
IS_SSL=true
configure_ssl
fi

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"description": "Dockerize My Continuous Deployment",
"main": "index.js",
"scripts": {
"build": "npm run install && tsc",
"start": "npm run build && node ./build/Server.js",
"install": "npm install",
"build": "npm install && tsc",
"start": "npm i && node ./build/Server.js",
"start-nobuild": "node ./build/Server.js",
"nodemon": "nodemon ./build/Server",
"create-admin": "node ./build/Setup/CreateAdmin",
Expand Down
2 changes: 1 addition & 1 deletion public/Scripts/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Toast(msg, status, title)
<div id="toast-${toastCount}" data-bs-autohide="false" class="toast border-${status} border-top" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false">
<div class="toast-header">
<img src="https://cdn.tolfix.com/images/TX-Small.png" width="28" class="rounded mr-2" alt="...">
<strong class="me-auto">${title}</strong>
<strong class="me-auto text-light">${title}</strong>
<button id="toast-button-${toastCount}" type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Expand Down
9 changes: 6 additions & 3 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IConfig, ISMTP } from "./Interfaces/Config";
import { IConfigMapIndex } from "./Interfaces/ConfigMap";
import AW from "./Lib/Async";
import ConfigModel from "./Models/Config";

Expand All @@ -24,6 +25,8 @@ export const GetSMTPConfig = () => {
return resolve(c)
})
};
export const Domain = process.env.DOMAIN ?? "localhost";
export const HTTPS: "https" | "http" = process.env.HTTP as "https" ?? "http"
export const DockerDir = ((__dirname.replace("\\build", "")).replace("/build", ""))+"/Docker";
export const ConfigMap = new Map<keyof IConfigMapIndex, IConfigMapIndex[keyof IConfigMapIndex]>();
export const DockerDir = ((__dirname.replace("\\build", "")).replace("/build", ""))+"/Docker";

ConfigMap.set("domain", process.env.DOMAIN ?? "localhost")
ConfigMap.set("http", process.env.HTTP as "https" ?? "http")
9 changes: 7 additions & 2 deletions src/Docker/DockerCompose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import AW from "../Lib/Async";
import { ICreateDockerCompose } from "../Interfaces/Docker";
import SOCKET from "../Server";
import { getCDSocketActive, getCDSocketFail } from "../Lib/CDSocket";
import { DebugMode } from "../Config";

export function DockerCompose(dir: string, cdName?: string): Promise<Boolean>
{
Expand All @@ -18,12 +19,16 @@ export function DockerCompose(dir: string, cdName?: string): Promise<Boolean>
{
if(cdName)
SOCKET.emit(getCDSocketFail(cdName), `Failed to build`);
log.error(`${DS}`);
log.error(`Failed to build`);
log.error(D_Error);
if(DebugMode)
console.log(D_Error);
return resolve(false);
}

if(cdName)
SOCKET.emit(getCDSocketActive(cdName), `Recreation was a success`);
log.verbos(`Succesfully built`);
return resolve(true);
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Docker/Pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function PullImage(dir: string, cdName?: string): Promise<Boolean
if(cdName)
SOCKET.emit(getCDSocketLogs(cdName), `Failed to pull image`);
log.error(`Unable to pull image`);
log.error(I_Error);
return resolve(false);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Events/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import event from "events";

export const DockerEvent = (new event);
3 changes: 2 additions & 1 deletion src/Interfaces/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export interface IConfig extends Document
setupDone: Boolean;
title: String;
smtp: ISMTP;
domain?: string;
domain: string;
ssl: Boolean;
}

export interface ISMTP
Expand Down
5 changes: 5 additions & 0 deletions src/Interfaces/ConfigMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IConfigMapIndex
{
domain: string;
http: "http" | "https";
}
29 changes: 28 additions & 1 deletion src/Routers/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import log from "../Lib/Logger";
import ConfigModel from "../Models/Config";
import { IConfig } from "../Interfaces/Config";
import { SendEmail } from "../Lib/Email";
import { ConfigMap } from "../Config";
export default class ConfigRouter {
protected server: Application;
protected router: Router;
Expand Down Expand Up @@ -118,7 +119,33 @@ export default class ConfigRouter {
req.flash(`success`, `Succesfully sent email.`);
return res.redirect("back");
})
})
});

this.router.post("/edit/domain", async (req, res) => {
const { ssl, domain } = req.body;

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");
}

Config[0].domain = domain;
Config[0].ssl = ssl === "on" ? true : false;

ConfigMap.set("domain", domain);
ConfigMap.set("http", ssl === "on" ? "https" : "http");

await Config[0].save();

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

}
}
5 changes: 3 additions & 2 deletions src/Routers/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ export default class WebhookRouter {

SOCKET.emit(getCDSocketLogs(CD.name), `New image ready.`);

const [Image, I_Error] = await AW(PullImage(CD.image));
const dir = DockerDir+"/"+CD.name;
const [Image, I_Error] = await AW(PullImage(dir, CD.name));

if(!Image || I_Error)
{
log.error(`Unable to pull image: ${CD.image}`);
SOCKET.emit(getCDSocketFail(CD.name), `Unable to pull image: ${CD.image}`);
SOCKET.emit(getCDSocketLogs(CD.name), `Unable to pull image: ${CD.image}`);
return res.sendStatus(400);
}

Expand Down
22 changes: 13 additions & 9 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, Domain, Session_Secret, DebugMode, HTTPS } from "./Config"
import { PORT, Web_Title, MongoDB_URI, Session_Secret, DebugMode, ConfigMap } from "./Config"
import Auth from "./Passport/Auth";
import MainRouter from "./Routers/Main";
import MongodbEvent from "./Events/Mongodb";
Expand All @@ -18,7 +18,8 @@ import ConfigRouter from "./Routers/Config";
import CDRouter from "./Routers/CD";
import WebhookRouter from "./Routers/Webhook";
import SocketHandler from "./Socket/SocketHandler";
import { GenString, GenStringBetter } from "./Lib/Generator";
import { GenStringBetter } from "./Lib/Generator";
import reCache from "./Setup/reCache";

const server = express();

Expand All @@ -35,14 +36,15 @@ Auth(passport);

server.use(expressLayout);
server.set('view engine', 'ejs');
server.use(express.static('public'));
server.use(
compileSass({
src: process.cwd()+"/sass",
src: process.cwd()+"/Sass",
dest: process.cwd()+"/public",
debug: DebugMode ? true : false,
outputStyle: 'compressed'
})
);
server.use(express.static('public'));

server.use(cors({
origin: true,
Expand All @@ -56,8 +58,8 @@ let sessionMiddleWare = session({
cookie: {
path: "/",
maxAge: 24*60*60*1000,
domain: Domain === "localhost" ? '' : Domain,
sameSite: Domain === "localhost" ? false : 'strict',
domain: ConfigMap.get("domain") === "localhost" ? '' : ConfigMap.get("domain"),
sameSite: ConfigMap.get("domain") === "localhost" ? false : 'strict',
}
});

Expand All @@ -81,15 +83,15 @@ server.use((req, res, next) => {

res.locals.Port = PORT;

res.locals.Domain = Domain;
res.locals.HTTP = HTTPS;
res.locals.Domain = ConfigMap.get("domain");
res.locals.HTTP = ConfigMap.get("http");

res.setHeader('X-Powered-By', 'Tolfix');

next();
});

const sv = server.listen(PORT, () => log.info(`Server listing on ${HTTPS}://${Domain}${Domain === "localhost" ? ":"+PORT : ""}/`));
const sv = server.listen(PORT, () => log.info(`Server listing on ${ConfigMap.get("http")}://${ConfigMap.get("domain")}${ConfigMap.get("domain") === "localhost" ? ":"+PORT : ""}/`));
const io = (new SocketIo(sv)).io;

new MainRouter(server);
Expand All @@ -103,5 +105,7 @@ if(process.platform === "win32" && !DebugMode)
process.exit(1);
}

reCache();

const SOCKET = new SocketHandler(io, db);
export default SOCKET
52 changes: 48 additions & 4 deletions src/Setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* 5. Create a config file (aka .env file) with the information given.
* 6. Print everything is done and ready to start.
*/

require("dotenv").config();
import log from "./Lib/Logger";
import ce from "command-exists";
import AW from "./Lib/Async";
Expand All @@ -28,6 +28,9 @@ import User from "./Models/User";
import bcrypt from "bcryptjs";
import mongoose from "mongoose";
import ConfigModel from "./Models/Config";
import { MongoDB_URI } from "./Config";

let myArgs = process.argv.slice(2);

export interface ISetupPrompt
{
Expand All @@ -47,7 +50,7 @@ export interface ISetupPrompt
export async function Setup()
{
// if(process.getuid && process.getuid() === 0)
// return log.error(`Not root user, or you are using windows`);
// return log.error(`Not root user, or you are using windows`)

const [D_Yes, D_No] = await AW(ce("docker"));

Expand Down Expand Up @@ -155,6 +158,7 @@ export async function Setup()
},
},
domain: "",
ssl: false,
}).save().then(() => {return;});

new User({
Expand All @@ -170,9 +174,49 @@ export async function Setup()

});
});
};
};

async function CreateSetupConfig()
{
await mongoose.connect(MongoDB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});

const config = (await ConfigModel.find())[0];

if(config)
{
log.error(`A config has already been created`);
return process.exit(0)
}

await new ConfigModel({
setupDone: true,
title: "DMCD",
smtp: {
host: "",
port: 25,
secure: false,
auth: {
user: "",
password: ""
},
},
domain: "",
}).save();

log.verbos("Config created.");
return process.exit(0)
}


(async () => {
await Setup();
if(myArgs[0] === "create-setup")
{
return await CreateSetupConfig();
}

return await Setup();
})();
3 changes: 2 additions & 1 deletion src/Setup/CreateAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ bcrypt.genSalt(10, (err, salt) => {
password: ""
},
},
domain: "",
domain: myArgs[2] ?? "",
ssl: myArgs[3] === "true" ? true : false,
}).save().then(() => {return;});

new User({
Expand Down
21 changes: 21 additions & 0 deletions src/Setup/reCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ConfigMap } from "../Config";
import { IConfig } from "../Interfaces/Config";
import AW from "../Lib/Async";
import log from "../Lib/Logger";
import ConfigModel from "../Models/Config";

export default async function reCache()
{
const [Config, C_Error] = await AW<IConfig[]>(ConfigModel.find());

if(Config)
{
let config = Config[0];
ConfigMap.set("domain", config.domain);

ConfigMap.set("http", config.ssl ? "https" : "http");
}


log.info(`Re cached from database`);
}
Loading

0 comments on commit 2fffefe

Please sign in to comment.