From a807e5cb03d721d05f09baf8ea7c8abf31af374c Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 9 Jun 2023 03:58:38 +0000 Subject: [PATCH] move source files to src folder, move localdb to config folder, consolidate vars.js with localdb, move service scripts to service folder --- .gitignore | 1 - README.md | 7 +-- .../localdb.json.template | 12 ++++++ db.js | 35 --------------- package.json | 2 +- .../proxmoxaas-api.service | 0 src/db.js | 43 +++++++++++++++++++ main.js => src/main.js | 5 +-- pve.js => src/pve.js | 2 +- utils.js => src/utils.js | 0 vars.js.template | 10 ----- 11 files changed, 63 insertions(+), 54 deletions(-) rename localdb.json.template => config/localdb.json.template (86%) delete mode 100644 db.js rename proxmoxaas-api.service => service/proxmoxaas-api.service (100%) create mode 100644 src/db.js rename main.js => src/main.js (96%) rename pve.js => src/pve.js (98%) rename utils.js => src/utils.js (100%) delete mode 100644 vars.js.template diff --git a/.gitignore b/.gitignore index 2e965cb..0dbb217 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ **/package-lock.json **/node_modules -**/vars.js **/localdb.json \ No newline at end of file diff --git a/README.md b/README.md index eed56b9..3403fbc 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,14 @@ In Proxmox VE, follow the following steps: ## Installation - API 1. Clone this repo onto `Client Host` 2. Run `npm install` to initiaze the package requirements -3. Copy `vars.js.template` as `vars.js` and modify the following values: +3. Copy `localdb.json.template` as `localdb.json` and modify the following values: - pveAPI - the URI to the Proxmox API, ie `:8006/api2/json` or `/api2/json` if Proxmox VE is behind a reverse proxy. - hostname - the ProxmoxAAS-Client URL, ie `host.domain.tld` - domain - the base domain for the client and proxmox, ie `domain.tld` - listenPort - the port you want the API to listen on, ie `8080` - pveAPIToken - the user(name), authentication realm, token id, and token secrey key (uuid) -4. Start the service using `node .`, or call the provided shell script, or use the provided systemctl service script +4. You may also wish to confuigure users at this point as well. An example user config is shown in the template. +5. Start the service using `node .`, or call the provided shell script, or use the provided systemctl service script ## Installation - Reverse Proxy 1. Configure nginx or preferred reverse proxy to reverse proxy the client. The configuration should include at least the following: @@ -47,4 +48,4 @@ server { 2. Start nginx with the new configurations by running `systemctl reload nginx` ## Result -After these steps, the ProxmoxAAS Client should be avaliable and fully functional at `client.`. +After these steps, the ProxmoxAAS Client should be avaliable and fully functional at `client.`. \ No newline at end of file diff --git a/localdb.json.template b/config/localdb.json.template similarity index 86% rename from localdb.json.template rename to config/localdb.json.template index 2c77371..9f8362c 100644 --- a/localdb.json.template +++ b/config/localdb.json.template @@ -1,4 +1,16 @@ { + "application": { + "pveAPI": "https://pve.mydomain/api2/json", + "pveAPIToken": { + "user": "proxmoxaas-api", + "realm": "pve", + "id": "token", + "uuid": "secret-key" + }, + "listenPort": 80, + "hostname": "client.mydomain", + "domain": "mydomain" + }, "resources": { "cores": { "type": "numeric", diff --git a/db.js b/db.js deleted file mode 100644 index 3c12a46..0000000 --- a/db.js +++ /dev/null @@ -1,35 +0,0 @@ -import { readFileSync, writeFileSync } from "fs"; - -class localdb { - #template = "localdb.json.template"; - #filename = "localdb.json"; - #data = null; - constructor() { - try { - this.load(this.#filename); - } - catch { - this.load(this.#template); - this.save(this.#filename); - } - } - load(path) { - this.#data = JSON.parse(readFileSync(path)); - } - save(path) { - writeFileSync(path, JSON.stringify(this.#data)); - } - getResourceConfig() { - return this.#data.resources; - } - getUserConfig(username) { - if (this.#data.users[username]) { - return this.#data.users[username]; - } - else { - return null; - } - } -} - -export const db = new localdb(); \ No newline at end of file diff --git a/package.json b/package.json index 41f91b2..8d2906f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "proxmoxaas-api", "version": "0.0.1", "description": "REST API for ProxmoxAAS", - "main": "main.js", + "main": "src/main.js", "type": "module", "dependencies": { "axios": "^1.3.2", diff --git a/proxmoxaas-api.service b/service/proxmoxaas-api.service similarity index 100% rename from proxmoxaas-api.service rename to service/proxmoxaas-api.service diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..1be8cd3 --- /dev/null +++ b/src/db.js @@ -0,0 +1,43 @@ +import { readFileSync, writeFileSync } from "fs"; +import { exit } from "process"; + +class localdb { + #filename = "config/localdb.json"; + #data = null; + constructor() { + try { + this.load(this.#filename); + } + catch { + console.log("Error: localdb.json was not found. Please follow the directions in the README to initialize localdb.json."); + exit(1); + } + } + load(path) { + this.#data = JSON.parse(readFileSync(path)); + } + save(path) { + writeFileSync(path, JSON.stringify(this.#data)); + } + getApplicationConfig() { + return this.#data.application; + } + getResourceConfig() { + return this.#data.resources; + } + getUserConfig(username) { + if (this.#data.users[username]) { + return this.#data.users[username]; + } + else { + return null; + } + } +} + +export const db = new localdb(); +export const pveAPI = db.getApplicationConfig().pveAPI; +export const pveAPIToken = db.getApplicationConfig().pveAPIToken; +export const listenPort = db.getApplicationConfig().listenPort; +export const hostname = db.getApplicationConfig().hostname; +export const domain = db.getApplicationConfig().domain; \ No newline at end of file diff --git a/main.js b/src/main.js similarity index 96% rename from main.js rename to src/main.js index 76a13cb..4f87cc7 100644 --- a/main.js +++ b/src/main.js @@ -3,12 +3,11 @@ import bodyParser from "body-parser"; import cookieParser from "cookie-parser"; import cors from "cors"; import morgan from "morgan"; -import api from "./package.json" assert {type: "json"}; +import api from "../package.json" assert {type: "json"}; -import { pveAPIToken, listenPort, hostname, domain } from "./vars.js"; import { requestPVE, handleResponse, getDiskInfo } from "./pve.js"; import { checkAuth, approveResources, getUserResources } from "./utils.js"; -import { db } from "./db.js"; +import { db, pveAPIToken, listenPort, hostname, domain } from "./db.js"; const app = express(); app.use(bodyParser.urlencoded({ extended: true })); diff --git a/pve.js b/src/pve.js similarity index 98% rename from pve.js rename to src/pve.js index 7c3405d..363b00c 100644 --- a/pve.js +++ b/src/pve.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import { pveAPI, pveAPIToken } from "./vars.js"; +import { pveAPI, pveAPIToken } from "./db.js"; export async function requestPVE(path, method, cookies, body = null, token = null) { let url = `${pveAPI}${path}`; diff --git a/utils.js b/src/utils.js similarity index 100% rename from utils.js rename to src/utils.js diff --git a/vars.js.template b/vars.js.template deleted file mode 100644 index 0a6dcd8..0000000 --- a/vars.js.template +++ /dev/null @@ -1,10 +0,0 @@ -export const pveAPI = ""; -export const pveAPIToken = { - user: "", - realm: "", - id: "", - uuid: "" -}; -export const listenPort = 80; -export const hostname = ""; -export const domain = "";