Resolved issue 5

This commit is contained in:
Alex Rennie-Lis 2024-04-07 18:54:30 +01:00
parent a0ba25ed2f
commit d762fc1c15
3 changed files with 45 additions and 14 deletions

14
code/data.json Normal file
View File

@ -0,0 +1,14 @@
{
"users": [
{
"username": "test",
"password": "test",
"vlan": "123"
},
{
"username": "test2",
"password": "test",
"vlan": "123"
}
]
}

View File

@ -23,7 +23,8 @@ catch (error) {
config = {
ports: {
radius_authentication: 1812,
radius_accounting: 1813
radius_accounting: 1813,
api: 8080
},
storage: "json:./data.json",
client_secret: "password",
@ -32,10 +33,18 @@ catch (error) {
}
if (process.env['NETRADIUS_PORT_RADIUS_AUTH']) config.ports.radius_authentication = process.env['NETRADIUS_PORT_RADIUS_AUTH'];
if (process.env['NETRADIUS_PORT_RADIUS_ACCT']) config.ports.radius_accounting = process.env['NETRADIUS_PORT_RADIUS_ACCT'];
if (process.env['NETRADIUS_PORT_API']) config.ports.api = process.env['NETRADIUS_PORT_API'];
if (process.env['NETRADIUS_STORAGE']) config.storage = process.env['NETRADIUS_STORAGE'];
if (process.env['NETRADIUS_DEFAULT_VLAN']) config.default_vlan_enabled = process.env['NETRADIUS_DEFAULT_VLAN'];
if (process.env['NETRADIUS_DEFAULT_VLAN_ID']) config.default_vlan_id = process.env['NETRADIUS_DEFAULT_VLAN_ID'];
if (process.env['NETRADIUS_CLIENT_SECRET']) config.client_secret = process.env['NETRADIUS_CLIENT_SECRET'];
// Set defaults
config.ports.radius_authentication = 1812;
config.ports.radius_accounting = 1813;
config.ports.api = 8080;
// Display active configuration
log.write ('Using configuration: ' + JSON.stringify (config));
// Listeners
@ -87,13 +96,18 @@ listeners.accounting.socket.on ('listening', () => {
listeners.accounting.socket.bind (config.ports.radius_accounting);
// HTTP listener
const respond = (res, content, status) {
res.write (content);
const respond = (res, content, status) => {
if (typeof (content) == "string") {
res.write (content);
}
else {
res.write (JSON.stringify (content));
}
res.statusCode = status;
res.end ();
};
http.createServer(function (req, res) {
http.createServer (function (req, res) {
var url = req.url.substring (0, req.url.lastIndexOf ("/")) || req.url;
var endpoint = req.method + " " + url;
switch (endpoint) {
@ -104,19 +118,19 @@ http.createServer(function (req, res) {
case "GET /users":
handlers.user.getall ((users, err) => {
if (!err) {
respond (res, users, 200);
if (err) {
respond (res, err, 404);
}
else {
respond (res, err, 404);
respond (res, users, 200);
}
});
break;
case "GET /user/":
case "GET /user":
handlers.user.getone (req.url.substring (req.url.lastIndexOf ("/") + 1), (user, err) => {
if (err) {
respond (res, "Error\n\n", 500);
respond (res, err, 404);
}
else {
respond (res, user, 200);
@ -174,6 +188,7 @@ http.createServer(function (req, res) {
}
}).listen (8080);
log.write ("API listening on port " + config.ports.api);
// Exit handles
const exitHandler = () => {

View File

@ -61,7 +61,7 @@ module.exports = {
callback ("OK\n\n", null);
}
catch (error) {
callback (null, "Error\n\n");
callback (null, "Error");
}
},
@ -78,16 +78,18 @@ module.exports = {
callback (response, null);
}
catch (error) {
callback (null, "Not found\n\n");
log.write (error);
callback (null, "Not found");
}
},
getUser: (username, callback) => {
try {
callback (users[username], null);
var user = users[username];
callback (user, null);
}
catch (error) {
callback (null, "Not found\n\n");
callback (null, error);
}
},
@ -103,7 +105,7 @@ module.exports = {
callback ("OK\n\n", null);
}
catch (error) {
callback (null, "Error\n\n");
callback (null, "Error");
}
}
}