Skip to content

Commit

Permalink
feat: added two parameters: only-get-or-head and check-index (#16) (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Salzmann <[email protected]>
Co-authored-by: tts-sdrissen <[email protected]>
  • Loading branch information
Eun and tts-sdrissen authored Sep 21, 2022
1 parent 71ceb19 commit 46b4de0
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 43 deletions.
41 changes: 41 additions & 0 deletions .github/test-allowed-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const http = require('http');
const fs = require('fs');
const process = require('process');

const options = {
hostname: 'localhost',
port: 9090,
path: '/',
method: 'GET'
};


const req = http.request(options, res => {
if (res.statusCode !== 405) {
console.error(`expected 405, got ${res.statusCode}`);
process.exit(1);
return;
}
const buf = 'Method Not Allowed';
let allTheData = "";

res.on('data', d => {
allTheData += d.toString();
});

res.on('end', () => {
if (allTheData !== buf) {
console.error(`expected ${buf}, but got ${allTheData}`);
process.exit(1);
return;
}
});
})

req.on('error', error => {
console.error(error);
process.exit(1);
return;
});

req.end();
File renamed without changes.
51 changes: 51 additions & 0 deletions .github/test-index-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const http = require('http');
const fs = require('fs');
const process = require('process');

const options = {
hostname: 'localhost',
port: 9090,
path: '/',
method: 'GET'
};


const buf = fs.readFileSync('.github/PULL_REQUEST_TEMPLATE.md').toString();

const req = http.request(options, res => {
if (res.statusCode !== 200) {
console.error(`expected 200, got ${res.statusCode}`);
process.exit(1);
return;
}

if (res.headers['content-length'] !== buf.length.toString()) {
console.error(`expected ${buf.length}, got ${res.headers['content-length']}`);
process.exit(1);
return;
}



let allTheData = "";

res.on('data', d => {
allTheData += d.toString();
});

res.on('end', () => {
if (allTheData !== buf) {
console.error(`expected ${buf}, but got ${allTheData}`);
process.exit(1);
return;
}
});
})

req.on('error', error => {
console.error(error);
process.exit(1);
return;
});

req.end();
50 changes: 48 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:

name: "push"
jobs:
test:
test-default:
strategy:
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]
Expand All @@ -27,7 +27,53 @@ jobs:
"md": "text/markdown"
}
-
run: node .github/test.js
run: node .github/test-default.js

test-index-files:
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
index-files: |
["PULL_REQUEST_TEMPLATE.md"]
-
run: node .github/test-index-files.js

test-allowed-methods:
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: |
["POST"]
-
run: node .github/test-allowed-methods.js

# draft your next release notes as pull requests are merged into "master"
# the configuration is at /.github/release-drafter.yml.
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# http-server-action
---
An action that spawns an http server to serve files.
An action that spawns an http server to serve files.

## Inputs
### `directory`
Expand All @@ -12,6 +12,12 @@ Port that should be used (default is `8080`) (*optional*)
### `no-cache`
No-Cache determiantes wheter the server sets the Cache-Control header or not (default is `false`) (*optional*)

### `index-files`
If set and directory is requested, look for those files, instead of show directory listing (default is EMPTY, sample is `["index.html", "index.htm"]`) (*optional*)

### `allowed-methods`
Throw HTTP-Error 405 on other methods than the methods given (default is `["GET", "HEAD"]`) (*optional*)

### `content-types`
A JSON object of content-types that should be served (*optional*)
Default:
Expand Down Expand Up @@ -45,6 +51,10 @@ steps:
directory: ${{ github.workspace }}
port: 8080
no-cache: false
index-files: |
["index.html", "index.htm"]
allowed-methods: |
["GET", "HEAD"]
content-types: |
{
"appcache": "text/cache-manifest",
Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ inputs:
description: 'Whether to set the Cache-Control header or not'
required: false
default: false
index-files:
description: 'If set and directory is requested, look for those files, instead of show directory listing'
required: false
default: |
[]
allowed-methods:
description: 'Throw HTTP-Error 405 on other methods than the methods given'
required: false
default: |
["GET", "HEAD"]
content-types:
description: 'Content Types to serve'
required: false
Expand Down
22 changes: 16 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const process = require('process');
const core = require('@actions/core');
const server = require('./server.js');


if (process.argv.length === 3 && process.argv[2] === 'serve') {
process.on('SIGTERM', () => {
process.exit(0);
Expand All @@ -27,16 +26,15 @@ if (process.argv.length === 3 && process.argv[2] === 'serve') {
return;
}



let config = {
root: null,
port: null,
noCache: null,
contentTypes: null,
indexFiles: null,
allowedMethods: null,
contentTypes: null
};


config.root = core.getInput('directory');
if (config.root === null || config.root.length == 0) {
config.root = '.';
Expand All @@ -61,6 +59,13 @@ if (config.noCache === null || config.noCache.length == 0) {
config.noCache = config.noCache === 'true';
}

config.indexFiles = core.getInput('index-files');
if (config.indexFiles === null || config.indexFiles.length == 0) {
config.indexFiles = [];
} else {
config.indexFiles = JSON.parse(config.indexFiles);
}

config.contentTypes = core.getInput('content-types');
if (config.contentTypes === null || config.contentTypes.length == 0) {
config.contentTypes = {
Expand All @@ -81,7 +86,12 @@ if (config.contentTypes === null || config.contentTypes.length == 0) {
config.contentTypes = JSON.parse(config.contentTypes);
}


config.allowedMethods = core.getInput('allowed-methods');
if (config.allowedMethods === null || config.allowedMethods.length == 0) {
config.allowedMethods = ['GET', 'HEAD'];
} else {
config.allowedMethods = JSON.parse(config.allowedMethods);
}

const cp = require('child_process');
const child = cp.fork(__filename, ['serve'], { detached: true, silent: true });
Expand Down
Loading

0 comments on commit 46b4de0

Please sign in to comment.