Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtynjala committed May 16, 2024
0 parents commit 2a30fe1
Show file tree
Hide file tree
Showing 17 changed files with 1,567 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: joshtynjala # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: #
45 changes: 45 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
pull_request:
branches:
- main

jobs:
api:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: krdlab/setup-haxe@v1
with:
haxe-version: 4.3.4
- name: Set up Haxelib dependencies
run: |
haxelib install dox --quiet
haxelib install hxargs --quiet
haxelib dev snake-server ${{ github.workspace }}
- name: Build API reference
working-directory: scripts
run: haxe docs.hxml
- uses: actions/upload-artifact@v4
with:
name: snake-server-docs
path: bin/api/
if-no-files-found: error

haxelib:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/upload-artifact@v4
with:
name: snake-server-haxelib
path: |
src/
haxelib.json
build.hxml
README.md
LICENSE
CHANGELOG.md
if-no-files-found: error
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode/
bin/
.DS_Store
Thumbs.db
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Snake Server Change Log

## 1.0.0 (????-??-??)

- Initial release
43 changes: 43 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
PSF LICENSE AGREEMENT FOR PYTHON 3.12.3

1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"), and
the Individual or Organization ("Licensee") accessing and otherwise using Python
3.12.3 software in source or binary form and its associated documentation.

2. Subject to the terms and conditions of this License Agreement, PSF hereby
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
analyze, test, perform and/or display publicly, prepare derivative works,
distribute, and otherwise use Python 3.12.3 alone or in any derivative
version, provided, however, that PSF's License Agreement and PSF's notice of
copyright, i.e., "Copyright © 2001-2023 Python Software Foundation; All Rights
Reserved" are retained in Python 3.12.3 alone or in any derivative version
prepared by Licensee.

3. In the event Licensee prepares a derivative work that is based on or
incorporates Python 3.12.3 or any part thereof, and wants to make the
derivative work available to others as provided herein, then Licensee hereby
agrees to include in any such work a brief summary of the changes made to Python
3.12.3.

4. PSF is making Python 3.12.3 available to Licensee on an "AS IS" basis.
PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF
EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR
WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE
USE OF PYTHON 3.12.3 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.

5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 3.12.3
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 3.12.3, OR ANY DERIVATIVE
THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.

6. This License Agreement will automatically terminate upon a material breach of
its terms and conditions.

7. Nothing in this License Agreement shall be deemed to create any relationship
of agency, partnership, or joint venture between PSF and Licensee. This License
Agreement does not grant permission to use PSF trademarks or trade name in a
trademark sense to endorse or promote products or services of Licensee, or any
third party.

8. By copying, installing or otherwise using Python 3.12.3, Licensee agrees
to be bound by the terms and conditions of this License Agreement.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Snake Server for Haxe

Create TCP and HTTP servers with the [Haxe](https://haxe.org/) programming language, or run `haxelib run snake-server` to start a local HTTP server that serves static files in a specific directory.

> **Warning!** snake-server is not recommended for production. It only implements basic security checks.
Available on [Haxe sys targets](https://haxe.org/manual/std-sys.html) only.

## Installation

Use the [**haxelib install**](https://lib.haxe.org/documentation/using-haxelib/#install) command to launch Snake Server in the current directory on port 8000.

```sh
haxelib install snake-server
```

## Command Line

Use the [**haxelib run**](https://lib.haxe.org/documentation/using-haxelib/#run) command to download Snake Server.

```sh
haxelib run snake-server
```

### Options

The following options can be added to the **haxelib run snake-server** command to customize its behavior.

- **--bind _address_**

bind to this address (default: 127.0.0.1)

- **--directory _path/to/dir_**

serve this directory (default: current directory)

- **--protocol _HTTP/X.Y_**

conform to this HTTP version (default: HTTP/1.0)

- **--port _number_**

bind to this port (default: 8000)

Example:

```sh
haxelib run snake-server --address 0.0.0.0 --port 3000 --protocol HTTP/1.1 --directory www
```

## Why did you choose "snake" for the name? 🐍

The Snake Server project's Haxe code is actually ported from the [Python](https://python.org/) language's [http.server](https://docs.python.org/3/library/http.server.html) and [socketserver](https://docs.python.org/3/library/socketserver.html) modules. [Pythons](<https://en.wikipedia.org/wiki/Python_(genus)>) are also a [type of snake](https://en.wikipedia.org/wiki/Snake). The name is simply meant to pay tribute to the code's origins.
3 changes: 3 additions & 0 deletions build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--main snake.Run
-cp src
-lib hxargs
14 changes: 14 additions & 0 deletions haxelib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "snake-server",
"url": "https://bowlerhat.dev/",
"tags": ["tcp", "http", "server", "sys", "web"],
"description": "Create TCP/HTTP servers in Haxe, or start a simple HTTP server from the command line",
"version": "1.0.0-alpha.1",
"classPath": "src",
"main": "snake.Run",
"dependencies": {
"hxargs": ""
},
"contributors": ["bowlerhat"],
"releasenote": "Initial release"
}
10 changes: 10 additions & 0 deletions scripts/docs.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-cp ../src
-lib hxargs
-D doc_gen
--macro include("snake")
--no-output
-xml ../bin/xml/types.xml
--interp

--next
-cmd haxelib run dox -o ../bin/api -i ../bin/xml/types.xml --include snake --toplevel-package snake --title "Snake Server API Reference" -D website "https://bowlerhat.dev" -D source-path "https://github.com/BowlerHatLLC/snake-server/tree/main/src/"
66 changes: 66 additions & 0 deletions src/snake/Run.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package snake;

import hxargs.Args;
import snake.http.BaseHTTPRequestHandler;
import snake.http.HTTPServer;
import snake.http.SimpleHTTPRequestHandler;
import snake.socket.BaseRequestHandler;
import sys.net.Host;
import sys.net.Socket;

class Run {
private static final DEFAULT_PROTOCOL = "HTTP/1.0";
private static final DEFAULT_ADDRESS = "127.0.0.1";
private static final DEFAULT_PORT = 8000;

public static function main():Void {
var args = Sys.args();
trace(args);
#if interp
var cwd = args.pop();
Sys.setCwd(cwd);
#end

var address:String = DEFAULT_ADDRESS;
var port:Int = DEFAULT_PORT;
var directory:String = null;
var protocol:String = DEFAULT_PROTOCOL;
var argHandler = Args.generate([
@doc('bind to this address (default: ${DEFAULT_ADDRESS})')
["--bind"] => function(host:String) {
address = host;
},
@doc('serve this directory (default: current directory)')
["--directory"] => function(path:String) {
directory = path;
},
@doc('conform to this HTTP version (default: ${DEFAULT_PROTOCOL})')
["--protocol"] => function(version:String) {
protocol = version;
},
@doc('bind to this port (default: ${DEFAULT_PORT})')
["--port"] => function(tcpPort:Int) {
port = tcpPort;
},
]);
argHandler.parse(args);
BaseHTTPRequestHandler.protocolVersion = protocol;
var httpServer = new RunHTTPServer(new Host(address), port, SimpleHTTPRequestHandler, directory);
httpServer.serveForever();
}
}

private class RunHTTPServer extends HTTPServer {
private var directory:String;

public function new(serverHost:Host, serverPort:Int, requestHandlerClass:Class<BaseRequestHandler>, bindAndActivate:Bool = true, ?protocolVersion:String,
?directory:String) {
this.directory = directory;
super(serverHost, serverPort, requestHandlerClass, bindAndActivate);
Sys.print('Serving HTTP on ${serverAddress.host} port ${serverAddress.port} (http://${serverAddress.host}:${serverAddress.port})\n');
}

override private function finishRequest(request:Socket, clientAddress:{host:Host, port:Int}):Void {
Type.createInstance(requestHandlerClass, [request, clientAddress, this, directory]);
}
}
Loading

0 comments on commit 2a30fe1

Please sign in to comment.