move source files to src folder,

move localdb to config folder,
consolidate vars.js with localdb,
move service scripts to service folder
This commit is contained in:
Arthur Lu 2023-06-09 03:58:38 +00:00
parent 24df8df731
commit 485ba0120e
11 changed files with 63 additions and 54 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
**/package-lock.json **/package-lock.json
**/node_modules **/node_modules
**/vars.js
**/localdb.json **/localdb.json

View File

@ -22,13 +22,14 @@ In Proxmox VE, follow the following steps:
## Installation - API ## Installation - API
1. Clone this repo onto `Client Host` 1. Clone this repo onto `Client Host`
2. Run `npm install` to initiaze the package requirements 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 `<proxmoxhost>:8006/api2/json` or `<proxmox URL>/api2/json` if Proxmox VE is behind a reverse proxy. - pveAPI - the URI to the Proxmox API, ie `<proxmoxhost>:8006/api2/json` or `<proxmox URL>/api2/json` if Proxmox VE is behind a reverse proxy.
- hostname - the ProxmoxAAS-Client URL, ie `host.domain.tld` - hostname - the ProxmoxAAS-Client URL, ie `host.domain.tld`
- domain - the base domain for the client and proxmox, ie `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` - 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) - 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 ## Installation - Reverse Proxy
1. Configure nginx or preferred reverse proxy to reverse proxy the client. The configuration should include at least the following: 1. Configure nginx or preferred reverse proxy to reverse proxy the client. The configuration should include at least the following:

View File

@ -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": { "resources": {
"cores": { "cores": {
"type": "numeric", "type": "numeric",

35
db.js
View File

@ -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();

View File

@ -2,7 +2,7 @@
"name": "proxmoxaas-api", "name": "proxmoxaas-api",
"version": "0.0.1", "version": "0.0.1",
"description": "REST API for ProxmoxAAS", "description": "REST API for ProxmoxAAS",
"main": "main.js", "main": "src/main.js",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"axios": "^1.3.2", "axios": "^1.3.2",

43
src/db.js Normal file
View File

@ -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;

View File

@ -3,12 +3,11 @@ import bodyParser from "body-parser";
import cookieParser from "cookie-parser"; import cookieParser from "cookie-parser";
import cors from "cors"; import cors from "cors";
import morgan from "morgan"; 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 { requestPVE, handleResponse, getDiskInfo } from "./pve.js";
import { checkAuth, approveResources, getUserResources } from "./utils.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(); const app = express();
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));

View File

@ -1,5 +1,5 @@
import axios from 'axios'; 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) { export async function requestPVE(path, method, cookies, body = null, token = null) {
let url = `${pveAPI}${path}`; let url = `${pveAPI}${path}`;

View File

@ -1,10 +0,0 @@
export const pveAPI = "";
export const pveAPIToken = {
user: "",
realm: "",
id: "",
uuid: ""
};
export const listenPort = 80;
export const hostname = "";
export const domain = "";