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 #44 from Tolfix/dev
Browse files Browse the repository at this point in the history
Docs & Notifications
  • Loading branch information
Tolfx authored Aug 7, 2021
2 parents ec5ba18 + b120751 commit da69912
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 14 deletions.
4 changes: 3 additions & 1 deletion public/Scripts/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ function Copy(text)
document.execCommand("copy");

document.body.removeChild(textArea);
}
}

Notification.requestPermission();
5 changes: 4 additions & 1 deletion public/Scripts/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ function Toast(msg, status, title)
$(document).ready(function(){
$('.toast').toast('show');
});

new Notification(`DMCD | ${status} | ${title}`, { body: msg, icon: "https://cdn.tolfix.com/images/TX-Small.png" });

let tempCount = toastCount;
document.querySelector(`#toast-button-${toastCount}`).addEventListener("click", (msg) => {
document.querySelector(`#toast-${tempCount}`).remove()
})

toastCount++
}
}
11 changes: 11 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export const DebugMode = process.env.DEBUG === "true" ? true : false;
export const MongoDB_URI = process.env.MONGODB_URI ?? "mongodb://localhost/dmcd";
export const PORT = 56251;
export const Session_Secret = process.env.SESSION_SECRET ?? undefined;

/**
*
* @returns {ISMTP}
* @description
* Gives the SMTP configs from database.
*/
export const GetSMTPConfig = () => {
return new Promise<ISMTP|null>(async (resolve, reject) =>
{
Expand All @@ -25,6 +32,10 @@ export const GetSMTPConfig = () => {
return resolve(c)
})
};
/**
* @description
* Contains all "general" configs in a map
*/
export const ConfigMap = new Map<keyof IConfigMapIndex, IConfigMapIndex[keyof IConfigMapIndex]>();
export const Dir = ((__dirname.replace("\\build", "")).replace("/build", ""));
export const DockerDir = Dir+"/Docker";
Expand Down
10 changes: 9 additions & 1 deletion src/Docker/DockerCompose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { stripIndent } from "common-tags"
import { ICD } from "../Interfaces/CD";
import docker from "docker-compose";
import log from "../Lib/Logger";
import AW from "../Lib/Async";
Expand All @@ -8,6 +7,10 @@ import SOCKET from "../Server";
import { getCDSocketActive, getCDSocketFail } from "../Lib/CDSocket";
import { DebugMode } from "../Config";

/**
* @description
* ----> "docker-compose up -d" <----
*/
export function DockerCompose(dir: string, cdName?: string): Promise<Boolean>
{
return new Promise(async (resolve, reject) => {
Expand All @@ -33,6 +36,11 @@ export function DockerCompose(dir: string, cdName?: string): Promise<Boolean>
});
}

/**
*
* @description
* Gives a string formated for a `docker-compose.yml` file.
*/
export function CreateDockerCompose(options: ICreateDockerCompose): string
{

Expand Down
4 changes: 4 additions & 0 deletions src/Docker/DockerDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import docker from "docker-compose";
import AW from "../Lib/Async";
import log from "../Lib/Logger";

/**
* @description
* Removes a container
*/
export default function DockerRemove(dir: string): Promise<Boolean>
{
return new Promise(async (resolve, reject) => {
Expand Down
4 changes: 4 additions & 0 deletions src/Docker/Pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { getCDSocketLogs } from "../Lib/CDSocket";
import log from "../Lib/Logger";
import SOCKET from "../Server";

/**
* @description
* Pulls a image from a CD
*/
export default function PullImage(dir: string, cdName?: string): Promise<Boolean>
{
return new Promise(async (resolve, reject) => {
Expand Down
5 changes: 4 additions & 1 deletion src/Docker/Setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { CreateDockerCompose, DockerCompose } from "./DockerCompose";
import AW from "../Lib/Async";
import PullImage from "./Pull";
import { getCDSocketActive, getCDSocketBuild, getCDSocketFail } from "../Lib/CDSocket";import SOCKET from "../Server";
;

/**
* @description
* Setups a CD
*/
export default function SetupDocker(options: ISetupDocker, mong: any): Promise<string>
{
return new Promise(async (resolve, reject) => {
Expand Down
43 changes: 43 additions & 0 deletions src/Interfaces/CD.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
import { Document } from "mongoose"

/**
* @description
* ICD but extended with Doucment
*/
export interface IDCD extends ICD, Document {};
/**
* @description
* CD object.
*/
export interface ICD
{
/**
* The name for the CD
*/
name: string;
/**
* Image for CD
*/
image: string;
/**
* Env variables for CD
*/
env?: Array<ENV>;
/**
* Ports for cd
*/
ports?: Array<PORTS>;
/**
* The webhook endpoint which is linked to this CD
*/
webhookUrl: string;
/**
* The current status for this CD
*/
status: string;
/**
* The logs for this CD
*/
logs: Array<ICDLog>;
/**
* If this CD should send emails on notifications
*/
email: Boolean;
/**
* What kind of emails should we send when a event happens
*/
email_noti: EmailNoti;
/**
* The email which should recieve the notification
*/
email_reciever: string;
restartPolicy: "always" | "never" | "on_failure" | "unless_stopped";
}
Expand All @@ -30,6 +68,11 @@ export interface PORTS

export interface ICDLog
{
/**
* @description
* If someone has read it.
* @deprecated
*/
read: Boolean;
msg: string;
date: Date;
Expand Down
7 changes: 6 additions & 1 deletion src/Interfaces/ENV.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export interface IENV {
/**
* @description
* All ENV that is being used.
*/
export interface IENV
{
SESSION_SECRET: string;
TITLE: string;
MONGODB_URI: string;
Expand Down
9 changes: 8 additions & 1 deletion src/Lib/Async.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// Promise<P> extends P ? P extends P ? any : Promise<P extends P ? P : any> : any
/**
* @description
* Used for async code, to skip `.catch` or `try {...}`
* @async
* ```ts
* const [Returns, ReturnsError] = await AW(Promise.resolve("hello"));
* ```
*/
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 Down
4 changes: 4 additions & 0 deletions src/Lib/EditEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { Dir } from "../Config";
import { IENV } from "../Interfaces/ENV";
import { env_seperator } from "./Seperator";

/**
* @description
* Edits the .env file on the machine.
*/
export function EditEnvFile(envOptions: Partial<IENV>)
{
// Get our file and parse through each.
Expand Down
4 changes: 4 additions & 0 deletions src/Lib/Email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import mail from "nodemailer";
import AW from "./Async";
import { GetSMTPConfig } from "../Config";

/**
* @description
* Send a email
*/
export async function SendEmail(
reciever: string,
subject: string,
Expand Down
11 changes: 11 additions & 0 deletions src/Lib/Seperator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @description
* Seperates a .env value
*
* ```
* ENV=VALUE => {
* value: VALUE,
* name: ENV
* }
* ```
*/
export function env_seperator
<Key extends string = string, Value extends string = string>
(env: string): { value: Value, name: Key }
Expand Down
4 changes: 4 additions & 0 deletions src/Middlewares/EnsureAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Request, Response, NextFunction } from "express"

/**
* @description
* Ensures a user is auth.
*/
export default function EnsureAuth(req: Request, res: Response, next: NextFunction)
{
if (req.isAuthenticated()) {
Expand Down
10 changes: 6 additions & 4 deletions src/Passport/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { PassportStatic } from 'passport';
// Load User model
import User from '../Models/User';

export default function Auth(passport: PassportStatic) {
export default function Auth(passport: PassportStatic)
{
passport.use(
new LocalStrategy({ usernameField: 'username' }, (username, password, done) =>
{
User.findOne({
username: username,
}).then((user) => {
}).then((user) =>
{
if (!user) {
return done(null, false, { message: 'That username is not registered' });
}
Expand All @@ -28,8 +30,8 @@ export default function Auth(passport: PassportStatic) {
return done(null, user);
});
});
})
);
})
);

passport.serializeUser((user, done) => {
//@ts-ignore
Expand Down
1 change: 0 additions & 1 deletion src/Routers/CD.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Router, Application } from "express";
import EnsureAuth from "../Middlewares/EnsureAuth";
import { Server } from "socket.io";
import fs from "fs";
import { ENV, ICD, IDCD, PORTS } from "../Interfaces/CD";
import AW from "../Lib/Async";
Expand Down
1 change: 0 additions & 1 deletion src/Routers/Main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Router, Application } from "express";
import passport from "passport";
import EnsureAuth from "../Middlewares/EnsureAuth";
import { Server } from "socket.io";
import AW from "../Lib/Async";
import CDModel from "../Models/CD";

Expand Down
3 changes: 1 addition & 2 deletions src/Routers/Webhook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Router, Application } from "express";
import { Server } from "socket.io";
import { DockerDir } from "../Config";
import { DockerCompose } from "../Docker/DockerCompose";
import PullImage from "../Docker/Pull";
import { ICD } from "../Interfaces/CD";
import AW from "../Lib/Async";
import { getCDSocketBuild, getCDSocketFail, getCDSocketLogs } from "../Lib/CDSocket";
import { getCDSocketBuild, getCDSocketLogs } from "../Lib/CDSocket";
import log from "../Lib/Logger";
import CDModel from "../Models/CD";
import SOCKET from "../Server";
Expand Down
28 changes: 28 additions & 0 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,38 @@ import SocketHandler from "./Socket/SocketHandler";
import { GenStringBetter } from "./Lib/Generator";
import reCache from "./Setup/reCache";

/**
* @description
* The express server
*/
const server = express();

/**
* @description
* Connecting to database
*/
mongoose.connect(MongoDB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});

/**
* @description
* Mongoose client;
*/
const db = mongoose.connection;

/**
* @description
* Load all mongoose events
*/
MongodbEvent(db);

/**
* @description
* Passport local auth
*/
Auth(passport);

server.use(expressLayout);
Expand Down Expand Up @@ -94,6 +115,9 @@ server.use((req, res, next) => {
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;

/*
* All of the routes gets loaded here
*/
new MainRouter(server);
new ConfigRouter(server);
new CDRouter(server);
Expand All @@ -105,6 +129,10 @@ if(process.platform === "win32" && !DebugMode)
process.exit(1);
}

/**
* @description
* Caches everything
*/
reCache();

const SOCKET = new SocketHandler(io, db);
Expand Down
4 changes: 4 additions & 0 deletions src/Setup/reCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import AW from "../Lib/Async";
import log from "../Lib/Logger";
import ConfigModel from "../Models/Config";

/**
* @description
* Caches general stuff from our database.
*/
export default async function reCache()
{
const [Config, C_Error] = await AW<IConfig[]>(ConfigModel.find());
Expand Down
Loading

0 comments on commit da69912

Please sign in to comment.