Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
JGeek00 committed Apr 23, 2023
2 parents 9226bb9 + 29af06f commit 06b2f21
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "my-server-status",
"version": "1.0.0",
"version": "1.1.0",
"description": "REST API to get information about your server",
"main": "src/index.ts",
"scripts": {
"build": "npx tsc",
"start": "sudo node dist/index.js",
"start:noroot": "node dist/index.js",
"dev": "concurrently \"npx tsc --watch\" \"nodemon -q dist/index.js\"",
"change-password": "node dist/auth/changePassword.js",
"set-credentials": "node dist/auth/setCredentials.js"
Expand Down
38 changes: 38 additions & 0 deletions src/controllers/reboot.controller.ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { execSync } from "child_process";
import si, { Systeminformation } from "systeminformation";

const rebootMachine = async () => {
const { osInfo }: { osInfo: Systeminformation.OsData } = await si.get({
osInfo: "platform",
});

if (osInfo.platform === "linux") {
try {
execSync("sudo shutdown -r now");
return true;
} catch (error) {
return false;
}
}
else if (osInfo.platform === "Windows") {
try {
execSync("shutdown /r");
return true;
} catch (error) {
return false;
}
}
else if (osInfo.platform === "darwin") {
try {
execSync("sudo shutdown -r now");
return true;
} catch (error) {
return false;
}
}
else {
return false;
}
}

export default rebootMachine;
38 changes: 38 additions & 0 deletions src/controllers/shutdown.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { execSync } from "child_process";
import si, { Systeminformation } from "systeminformation";

const shutdownMachine = async () => {
const { osInfo }: { osInfo: Systeminformation.OsData } = await si.get({
osInfo: "platform",
});

if (osInfo.platform === "linux") {
try {
execSync("sudo shutdown -h now");
return true;
} catch (error) {
return false;
}
}
else if (osInfo.platform === "Windows") {
try {
execSync("shutdown /s");
return true;
} catch (error) {
return false;
}
}
else if (osInfo.platform === "darwin") {
try {
execSync("sudo shutdown -h now");
return true;
} catch (error) {
return false;
}
}
else {
return false;
}
}

export default shutdownMachine;
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { checkRootAccess } from './functions/checkRootAccess';
import server from "./server";
import { readAppVersion } from './utils/readAppVersion';

const startServer = () => {
server
.listen(server.get("port"))
.on("listening", () =>
console.log("Server listening on port", server.get("port"))
)
.on("listening", async () => {
const appVersion = await readAppVersion();
console.log("Server listening on port", server.get("port"));
console.log(`API v${appVersion}`);
})
.on("error", (e) => console.log(e));
}

Expand Down
9 changes: 9 additions & 0 deletions src/routes/v1/apiVersion.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Request, Response } from "express";
import { readAppVersion } from "../../utils/readAppVersion";

const ApiVersionRute = async (req: Request, res: Response) => {
const appVersion = await readAppVersion();
res.send(`v${appVersion}`);
}

export default ApiVersionRute;
16 changes: 11 additions & 5 deletions src/routes/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ import StatusRoute from './status.route';
import StorageRoute from './storage.route';
import SystemRoute from './system.route';
import OsRoute from './os.route';
import ApiVersionRute from './apiVersion.route';
import ShutdownRoute from './shutdown.route';
import RebootRoute from './reboot.route';

const router: Router = Router();

router.get('/check-credentials', CheckCredentialsRoute);
router.get('/api-version', ApiVersionRute);
router.get('/general-info', GeneralInfo);
router.get('/cpu', CpuRoute);
router.get('/memory', MemoryRoute);
router.get('/system', SystemRoute)
router.get('/storage', StorageRoute)
router.get('/network', NetworkRoute)
router.get('/status', StatusRoute)
router.get('/os', OsRoute)
router.get('/system', SystemRoute);
router.get('/storage', StorageRoute);
router.get('/network', NetworkRoute);
router.get('/status', StatusRoute);
router.get('/os', OsRoute);
router.get('/shutdown', ShutdownRoute);
router.get('/reboot', RebootRoute);

export default router;
14 changes: 14 additions & 0 deletions src/routes/v1/reboot.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response } from "express";
import rebootMachine from "../../controllers/reboot.controller.ts";

const RebootRoute = async (req: Request, res: Response) => {
const result = await rebootMachine();
if (result === true) {
res.send('Success');
}
else {
res.send('Error');
}
}

export default RebootRoute;
14 changes: 14 additions & 0 deletions src/routes/v1/shutdown.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response } from "express";
import shutdownMachine from "../../controllers/shutdown.controller";

const ShutdownRoute = async (req: Request, res: Response) => {
const result = await shutdownMachine();
if (result === true) {
res.send('Success');
}
else {
res.send('Error');
}
}

export default ShutdownRoute;
10 changes: 10 additions & 0 deletions src/utils/readAppVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from "fs/promises";

export const readAppVersion = async () => {
try {
const file = await fs.readFile("./package.json", "utf-8");
return JSON.parse(file).version;
} catch (error) {
return false;
}
};

0 comments on commit 06b2f21

Please sign in to comment.