Skip to content

Commit 00a89c9

Browse files
committed
Convert to package, core functions
1 parent 385854d commit 00a89c9

4 files changed

Lines changed: 119 additions & 126 deletions

File tree

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,30 @@
22

33
Easily plan, manage, and track client commissions.
44

5-
![Banner](/frontend/public/banner.png)
5+
![Banner](/frontend/public/banner.png)
6+
7+
## Usage
8+
9+
Include the `commtrackr` package in your Express.js application:
10+
11+
```javascript
12+
const express = require('express');
13+
const commtrackr = require('commtrackr'); // Import the commtrackr package
14+
const app = express();
15+
app.use('/commtrackr', commtrackr.routes); // Mount routes to /commtrackr path
16+
commtrackr.init({ // Initialize CommTracker with configurations
17+
tenant: {
18+
slug: 'commtrackr', // Unique identifier for the tenant
19+
name: 'CommTrackr', // Name of the tenant
20+
description: 'Easily plan, manage, and track client commissions.', // Description of the tenant
21+
auth: {
22+
enabled: false, // Enable or disable authentication
23+
provider: '', // Recognizable name of authentication provider
24+
url: '', // URL to redirect to for authentication
25+
},
26+
},
27+
});
28+
app.listen(3000, () => {
29+
console.log('Server is running on http://localhost:3000');
30+
});
31+
```

index.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const express = require('express');
2+
const rateLimit = require('express-rate-limit');
3+
const cors = require('cors');
4+
const path = require('path');
5+
const app = express();
6+
require('dotenv').config();
7+
8+
app.engine('.ejs', require('ejs').renderFile);
9+
app.set('view engine', 'ejs');
10+
app.set('views', path.join(__dirname, 'frontend/pages'));
11+
app.use(express.static(path.join(__dirname, 'frontend/public'), { redirect: false }));
12+
app.use(express.urlencoded({ extended: true }));
13+
app.use(express.json());
14+
app.use(cors());
15+
app.use(rateLimit({
16+
windowMs: 60 * 1000,
17+
max: 15,
18+
skip: (req, res) => {
19+
return req.ip !== undefined;
20+
},
21+
message: "Too many requests from this IP, please try again in a minute."
22+
}));
23+
24+
let on = false;
25+
let tenant = {};
26+
let returnHandler = null;
27+
let user = {};
28+
29+
function init({
30+
tenant: newTenant = {
31+
slug: 'commtrackr',
32+
name: 'CommTrackr',
33+
description: 'This is a default tenant configuration.',
34+
auth: {
35+
enabled: false,
36+
provider: '',
37+
url: '',
38+
},
39+
},
40+
handler: newHandler = null
41+
}) {
42+
tenant = {
43+
slug: 'commtrackr',
44+
name: 'CommTrackr',
45+
description: 'This is a default tenant configuration.',
46+
auth: {
47+
enabled: false,
48+
provider: '',
49+
url: '',
50+
},
51+
...newTenant
52+
};
53+
returnHandler = newHandler;
54+
};
55+
56+
function activate(isOn = true) {
57+
on = isOn;
58+
if (!on) user = {};
59+
};
60+
61+
function setUser(newUser = {}) {
62+
if (!newUser.id) return user = {};
63+
user = newUser;
64+
};
65+
66+
app.get('/', async (req, res) => {
67+
if (!on) return res.send('Service is not active');
68+
if (!tenant.slug) return res.send('No tenant configured');
69+
if (tenant.auth && tenant.auth.enabled && !user.id) return res.send('Authentication required');
70+
res.send(tenant.slug);
71+
});
72+
73+
module.exports = {
74+
routes: app,
75+
init,
76+
activate,
77+
setUser
78+
};

package-lock.json

Lines changed: 0 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,36 @@
33
"version": "1.0.0",
44
"description": "Easily plan, manage, and track client commissions.",
55
"main": "index.js",
6+
"files": [
7+
"index.js",
8+
"frontend/"
9+
],
610
"scripts": {
711
"start": "node index.js",
812
"dev": "nodemon index.js",
913
"test": "echo \"Error: no test specified\" && exit 1"
1014
},
11-
"keywords": [],
12-
"author": "",
15+
"keywords": [
16+
"commissions",
17+
"tracking",
18+
"management"
19+
],
20+
"author": "Dango Web Solutions",
1321
"license": "ISC",
1422
"dependencies": {
1523
"cors": "^2.8.5",
1624
"dotenv": "^16.3.1",
1725
"ejs": "^3.1.9",
1826
"express": "^4.18.2",
1927
"express-rate-limit": "^8.0.1",
20-
"mysql2": "^3.14.3",
2128
"node-fetch": "^3.3.2",
2229
"path": "^0.12.7"
2330
},
2431
"devDependencies": {
2532
"nodemon": "^3.0.1"
33+
},
34+
"repository": {
35+
"type": "git",
36+
"url": "https://github.com/dangoweb/commtrackr.git"
2637
}
2738
}

0 commit comments

Comments
 (0)