Skip to content

Commit

Permalink
Feat: Logging (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
tts-jwiebe authored Jul 14, 2023
1 parent 130bc67 commit 854de8e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
42 changes: 39 additions & 3 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ jobs:
{
"md": "text/markdown"
}
log: "log.txt"
-
run: node .github/test-default.js
run: |
node .github/test-default.js
cat log.txt
test-index-files:
strategy:
Expand All @@ -49,8 +52,11 @@ jobs:
no-cache: true
index-files: |
["PULL_REQUEST_TEMPLATE.md"]
log: "log.txt"
-
run: node .github/test-index-files.js
run: |
node .github/test-index-files.js
cat log.txt
test-allowed-methods:
strategy:
Expand All @@ -72,8 +78,11 @@ jobs:
no-cache: true
allowed-methods: |
["POST"]
log: "log.txt"
-
run: node .github/test-allowed-methods.js
run: |
node .github/test-allowed-methods.js
cat log.txt
# draft your next release notes as pull requests are merged into "master"
# the configuration is at /.github/release-drafter.yml.
Expand All @@ -86,3 +95,30 @@ jobs:
config-name: release-drafter.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
test-logging:
strategy:
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
-
name: Checkout code
uses: actions/checkout@v3
-
uses: actions/setup-node@v3
-
name: Serve Files
uses: ./
with:
directory: ${{ github.workspace }}/.github
port: 9090
no-cache: true
allowed-methods: |
["GET", "POST"]
log: "log.txt"
-
run: |
curl -vvvv http://localhost:9090/
curl --header "Content-Type: application/json" --request POST --data '[{"msg":"hello"},{"msg":"World"}]' http://localhost:9090/
- name: Show log
run: cat log.txt
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Default:
"xml": "text/xml"
}
```
### `log`
Create a log file with the given name (default is `off`) (*optional*)

## Example
```yaml
Expand Down Expand Up @@ -70,6 +72,9 @@ steps:
"txt": "text/plain",
"xml": "text/xml"
}
log: "log.txt"
-
run: curl -vvvv http://localhost:8080/index.html
run: |
curl -vvvv http://localhost:8080/index.html
cat log.txt
```
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ inputs:
"txt": "text/plain",
"xml": "text/xml"
}
log:
description: 'Create a log file with given name'
required: false
default: 'off'
runs:
using: 'node16'
main: 'main.js'
Expand Down
5 changes: 4 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ let config = {
noCache: null,
indexFiles: null,
allowedMethods: null,
contentTypes: null
contentTypes: null,
log: null
};

config.root = core.getInput('directory');
Expand Down Expand Up @@ -93,6 +94,8 @@ if (config.allowedMethods === null || config.allowedMethods.length == 0) {
config.allowedMethods = JSON.parse(config.allowedMethods);
}

config.log = core.getInput("log");

const cp = require('child_process');
const child = cp.fork(__filename, ['serve'], { detached: true, silent: true });
child.on('error', (err) => {
Expand Down
32 changes: 29 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs');

function deploy(config, ready) {
const server = http.createServer();
const formatTime = Intl.DateTimeFormat('en-US', { hour: "2-digit", minute: "2-digit", second:"2-digit", hour12: false })

if (config.root === undefined || config.root === null || config.root.length == 0) {
config.root = process.cwd();
Expand All @@ -24,6 +25,9 @@ function deploy(config, ready) {
if (config.contentTypes === undefined || config.contentTypes === null || config.contentTypes.length == 0) {
config.contentTypes = {};
}
if (config.log == undefined || config.log == null) {
config.log = "off";
}

const root = path.resolve(path.normalize(config.root));
let cwd = root;
Expand All @@ -34,8 +38,30 @@ function deploy(config, ready) {
function toPosixPath(url) {
return path.posix.join(...url.split(path.sep));
}

let writeLine = (line) => {
if (config.log !== "off") {
let txtLogger = fs.createWriteStream(config.log, {
flags: 'a'
});

txtLogger.write(`\n${line}`);
}
};

server.on('request', (request, response) => {
let now = formatTime.format(new Date());
let data = '';

request.on('data', (chunk) => {
data += chunk;
});

request.on('end', () => {
writeLine(`[${now}] ${request.method} ${request.url} ${JSON.stringify(data)}`);
});


if (config.noCache) {
response.setHeader(
'Cache-Control',
Expand Down Expand Up @@ -84,15 +110,15 @@ function deploy(config, ready) {
}
const noIndexFound = config.indexFiles.every(elem => {
const indexFile = requestedFile + elem;
if(fs.existsSync(indexFile)){
if (fs.existsSync(indexFile)) {
requestedFile = indexFile;
stat = fs.statSync(requestedFile);
return false;
}
return true;
});

if(noIndexFound) {
if (noIndexFound) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
Expand Down Expand Up @@ -149,4 +175,4 @@ function deploy(config, ready) {
});
}

exports.deploy = deploy;
exports.deploy = deploy;

0 comments on commit 854de8e

Please sign in to comment.