-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e06a45b
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/test.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>\ | ||
' | ||
); | ||
} |