Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Okirshen committed Jan 1, 2021
0 parents commit e06a45b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test.txt
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"deno.enable": true,
"deno.import_intellisense_origins": {
"https://deno.land": true
}
}
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Servey

Servey is a simple http static file server made with deno and typescript

## Installation

```bash
deno install --allow-net --allow-read -n servey https://deno.land/x/servey/mod.ts
```

## Usage

This will run a basic file server in the cwd on `localhost:5000/<file-path>`:

```bash
servey
```

options:

```
-h, --help: display a help message like this
-D, --download: makes files downloadable
-p, --port <port>: runs the server on <port> instead of 3000
```
55 changes: 55 additions & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env -S deno --allow-net --allow-read
import { parse } from 'https://deno.land/std/flags/mod.ts';
import { serve } from 'https://deno.land/std/http/server.ts';
import * as Colors from 'https://deno.land/std/fmt/colors.ts';

const args = parse(Deno.args);

const options = {
download: args.download || args.d || args.D,
port: args.port || args.p || args.P,
help: args.help || args.h || args.H,
};
if (!options.help) {
let port: number = 3000;
if (options.port && typeof options.port === 'number') port = options.port;
if (options.download)
console.log(Colors.blue(Colors.bold('Running with download')));

console.log(Colors.green(`Running on port ${port}`));
const server = serve({ port });

const decoder = new TextDecoder('utf-8');

for await (const req of server) {
console.log(req.url.slice(1));

if (options.download) {
let headers = new Headers();

headers.set(
'Content-disposition',
`attachment; filename=${req.url.substring(
req.url.lastIndexOf('/') + 1
)}`
);

req.respond({
body: await Deno.readFile(req.url.slice(1)),
headers,
});
} else {
req.respond({
body: decoder.decode(await Deno.readFile(req.url.slice(1))),
});
}
}
} else {
console.log(
'Servey is a simple static files server:\n\
--help, -h: shows this message\n\
--download, -d: enables file download\n\
--port <port>, -p <port>: changes the default port from 300 to <port>\
'
);
}

0 comments on commit e06a45b

Please sign in to comment.