Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded Hapi from v13 to v21 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions .tool-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 16.16.0
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
strobelight-ci-alarm
====================
# strobelight-ci-alarm

**strobelight-ci-alarm** is a Electronic Relay toggler to be used on our Continuous Integration system (while builds are running).

Expand Down Expand Up @@ -29,7 +28,7 @@ Changes the relay state switching either to `ON` or `OFF`

This `hapi.js` project has the following structure:

```
```shell
|-config # any config required - you can change your PIN ports here
|-controllers # controllers for the application
|-handlers # handlers for routes
Expand All @@ -44,4 +43,16 @@ This `hapi.js` project has the following structure:

## Logging

Logging is performed via [good-console](https://github.com/hapijs/good-console) and redirected to `/tmp/strobelight.log` file.
Logging is performed via [good-console](https://github.com/hapijs/good-console) and redirected to `/tmp/strobelight.log` file.

## Run

Run using:

```shell
npm start
```

## Systemd and nginx

INside `system_configs` folder you have a systemd config file and an nginx sites-available config file if you want to enable the service on port `80`.
31 changes: 15 additions & 16 deletions config/constants.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module.exports = Object.freeze({

SERVER_PORT: 3000,
GPIO_PORT: 17,
VERSION: 1,
STATUS : {
ACTIVE : 0,
INACTIVE : 1
},
WRITE_STATE : {
ON : "on",
OFF : "off",
},
AUDIO_BASE_PATH: '/var/lib/strobelight-ci-alarm/sounds',
AUDIO_MAX_RANDOM: 10
});
"use strict";

export const SERVER_PORT = 3000;
export const GPIO_PORT = 17;
export const VERSION = process.env.npm_package_version;
export const STATUS = {
ACTIVE: 0,
INACTIVE: 1,
};
export const WRITE_STATE = {
ON: "on",
OFF: "off",
};
export const AUDIO_BASE_PATH = "/var/lib/strobelight-ci-alarm/sounds";
export const AUDIO_MAX_RANDOM = 10;
34 changes: 16 additions & 18 deletions controllers/authenticator.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
'use strict';
"use strict";

const Bcrypt = require('bcrypt');
const Basic = require('hapi-auth-basic');
const User = require('../models/user.js');
import { compare } from "bcrypt";
import User from "../models/user.js";

const users = {

admin: {
username: 'admin',
password: '$2a$10$B/bV1O6ebwNvyWPYYMvs1.AM4F1mn0bq/TZFPtGkh/dOGdEiEJSwa',
name: 'admin',
id: '2133d32a'
}
username: "admin",
password: "$2a$10$B/bV1O6ebwNvyWPYYMvs1.AM4F1mn0bq/TZFPtGkh/dOGdEiEJSwa",
name: "admin",
id: "2133d32a",
},
};

module.exports = function(request, username, password, callback) {

export const validate = async (request, username, password) => {
const user = users[username];
if (!user) {
return callback(null, false);
return { credentials: null, isValid: false };
}

/*
var cryptedPassword = new User(username, password).encryptedPass();
console.log("cryptedPassword => " + cryptedPassword);
*/

Bcrypt.compare(password, user.password, (err, isValid) => {
callback(err, isValid, { id: user.id, name: user.name });
});
}

const isValid = await compare(password, user.password);
const credentials = { id: user.id, name: user.name };

return { isValid, credentials };
};
108 changes: 51 additions & 57 deletions controllers/gpiotoggler.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,62 @@
'use strict';
"use strict";

const constants = require('../config/constants.js');
const os = require('os');
import { GPIO_PORT, WRITE_STATE, STATUS } from "../config/constants.js";
import { platform } from "os";

module.exports = class GpioToggler {

constructor() {

console.info("Setting up GpioToggler")
if(os.platform() != 'linux'){
return;
}
var Gpio = require('onoff').Gpio;
this.pin = new Gpio(constants.GPIO_PORT, 'out');
var weakthis = this;
this.turnOn(function(err){
console.info("Initial on/off cycle...");
weakthis.turnOff(function(err){
console.info("Strobelight booted up!");
}); // defaults to OFF at startup
});

process.on('SIGINT', this.exit);
export default class GpioToggler {
constructor() {
console.info("Setting up GpioToggler");
if (platform() != "linux") {
return;
}

toggle(){

console.info("Toggling");
if(os.platform() != 'linux'){
return 1;
}
var previousState = this.pin.readSync();
var newState = previousState ^ 1;
this.pin.writeSync(newState);
return (newState ? constants.WRITE_STATE.OFF : constants.WRITE_STATE.ON);
var Gpio = require("onoff").Gpio;
this.pin = new Gpio(GPIO_PORT, "out");
var weakthis = this;
this.turnOn(function (err) {
console.info("Initial on/off cycle...");
weakthis.turnOff(function (err) {
console.info("Strobelight booted up!");
}); // defaults to OFF at startup
});

process.on("SIGINT", this.exit);
}

toggle() {
console.info("Toggling");
if (platform() != "linux") {
return 1;
}

turnOn(callback){

console.info("Turning on");
if(os.platform() != 'linux'){
return;
}
this.pin.write(constants.STATUS.ACTIVE, callback);
var previousState = this.pin.readSync();
var newState = previousState ^ 1;
this.pin.writeSync(newState);
return newState ? WRITE_STATE.OFF : WRITE_STATE.ON;
}

turnOn(callback) {
console.info("Turning on");
if (platform() != "linux") {
return;
}
this.pin.write(STATUS.ACTIVE, callback);
}

turnOff(callback){

console.info("Turning off");
if(os.platform() != 'linux'){
return;
}
this.pin.write(constants.STATUS.INACTIVE, callback);
turnOff(callback) {
console.info("Turning off");
if (platform() != "linux") {
return;
}
this.pin.write(STATUS.INACTIVE, callback);
}

exit() {

if(this.pin){
this.pin.unexport();
}
process.exit();
exit() {
if (this.pin) {
this.pin.unexport();
}
process.exit();
}

toString() {
return '(' + this.pin + ')';
}
toString() {
return "(" + this.pin + ")";
}
}
18 changes: 18 additions & 0 deletions github-actions-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: GitHub Actions for strobelight
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
Loading