Skip to content

Commit

Permalink
add github action (#142)
Browse files Browse the repository at this point in the history
* add action

* move to workflow

* fix yml
  • Loading branch information
jackkav authored Jan 3, 2024
1 parent babb88d commit 3dc1f1c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 312 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Test
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm ci
- run: npm test
108 changes: 54 additions & 54 deletions lib/routes/bins.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
var debug = require("debug")("mockbin");
var express = require("express");
var mw = require("../middleware");
var redis = require("redis");
var routes = require("./bins/");
var URL = require("url").URL;
var debug = require('debug')('mockbin')
var express = require('express')
var mw = require('../middleware')
var redis = require('redis')
var routes = require('./bins/')
var URL = require('url').URL

module.exports = function bins(dsnStr) {
// parse redis dsn
var dsn = new URL(dsnStr);
module.exports = function bins (dsnStr) {
// parse redis dsn
var dsn = new URL(dsnStr)

this.dsn = dsn;
this.dsn = dsn

// connect to redis
this.client = redis.createClient(
{
host: dsn.hostname,
port: dsn.port,
no_ready_check: true
})
// connect to redis
this.client = redis.createClient(
{
host: dsn.hostname,
port: dsn.port,
no_ready_check: true
})

// Disable client's AUTH command.
this.client.auth = null
this.client.send_command('AUTH', [dsn.username, dsn.password])
// Disable client's AUTH command.
this.client.auth = null
this.client.send_command('AUTH', [dsn.username, dsn.password])

this.client.on("error", (err) => {
debug("redis error:", err);
});
this.client.on('error', (err) => {
debug('redis error:', err)
})

var router = express.Router();
var router = express.Router()

var defaults = [
mw.forwarded,
mw.errorHandler,
mw.bodyParser,
null,
mw.cors,
mw.negotiateContent,
];
var defaults = [
mw.forwarded,
mw.errorHandler,
mw.bodyParser,
null,
mw.cors,
mw.negotiateContent
]

var endpoints = [
{ action: "get", path: "/create", route: routes.form.bind(this) },
{ action: "post", path: "/create", route: routes.create.bind(this) },
{ action: "get", path: "/:uuid/view", route: routes.view.bind(this) },
{ action: "get", path: "/:uuid/sample", route: routes.sample.bind(this) },
{ action: "get", path: "/:uuid/log", route: routes.log.bind(this) },
{
action: "delete",
path: "/:uuid/delete",
route: routes.delete.bind(this),
},
{ action: "put", path: "/:uuid", route: routes.update.bind(this) },
{ action: "all", path: "/:uuid*", route: routes.run.bind(this) },
];
var endpoints = [
{ action: 'get', path: '/create', route: routes.form.bind(this) },
{ action: 'post', path: '/create', route: routes.create.bind(this) },
{ action: 'get', path: '/:uuid/view', route: routes.view.bind(this) },
{ action: 'get', path: '/:uuid/sample', route: routes.sample.bind(this) },
{ action: 'get', path: '/:uuid/log', route: routes.log.bind(this) },
{
action: 'delete',
path: '/:uuid/delete',
route: routes.delete.bind(this)
},
{ action: 'put', path: '/:uuid', route: routes.update.bind(this) },
{ action: 'all', path: '/:uuid*', route: routes.run.bind(this) }
]

endpoints.forEach((endpoint) => {
// add route to middleware
defaults.splice(3, 1, endpoint.route);
endpoints.forEach((endpoint) => {
// add route to middleware
defaults.splice(3, 1, endpoint.route)

// assign router to action at path
router[endpoint.action].apply(router, [endpoint.path].concat(defaults));
});
// assign router to action at path
router[endpoint.action].apply(router, [endpoint.path].concat(defaults))
})

return router;
};
return router
}
8 changes: 3 additions & 5 deletions lib/routes/bins/delete.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')

module.exports = function (req, res, next) {
this.client.del('bin:' + req.params.uuid, function (err) {
this.client.del('bin:' + req.params.uuid, (err) => {
if (err) {
debug(err)

Expand All @@ -12,7 +10,7 @@ module.exports = function (req, res, next) {
next()
})

this.client.del('log:' + req.params.uuid, function (err) {
this.client.del('log:' + req.params.uuid, (err) => {
if (err) {
debug(err)

Expand Down
21 changes: 11 additions & 10 deletions lib/routes/bins/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')
var util = require('util')
var validate = require('har-validator')

Expand Down Expand Up @@ -43,15 +41,18 @@ module.exports = function (req, res, next) {

mock.content.size = 0

validate.response(mock)
.then(function () {
this.client.set('bin:' + id, JSON.stringify(mock))
validate
.response(mock)
.then(
function () {
this.client.set('bin:' + id, JSON.stringify(mock))

res.view = 'redirect'
res.status(200).location(util.format('/bin/%s', id)).body = id
}.bind(this))
res.view = 'redirect'
res.status(200).location(util.format('/bin/%s', id)).body = id
}.bind(this)
)

.catch(function (err) {
.catch((err) => {
res.body = {
errors: err.errors
}
Expand Down
Loading

0 comments on commit 3dc1f1c

Please sign in to comment.