From 1506294de8b773e383e74496b99cbe430a5bea50 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 14 Jan 2025 18:14:18 -0500 Subject: [PATCH] Added initial code. --- .gitignore | 17 ++++++++++++++ LICENSE | 1 + dub.json | 14 ++++++++++++ dub.selections.json | 10 +++++++++ source/handy_http_starter/package.d | 35 +++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 dub.json create mode 100644 dub.selections.json create mode 100644 source/handy_http_starter/package.d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1293506 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +.dub +docs.json +__dummy.html +docs/ +/handy-http-starter +handy-http-starter.so +handy-http-starter.dylib +handy-http-starter.dll +handy-http-starter.a +handy-http-starter.lib +handy-http-starter-test-* +*.exe +*.pdb +*.o +*.obj +*.lst +*.a diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..77d90b5 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +Handy-Http by Andrew Lalis is marked with CC0 1.0 Universal. To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0/ \ No newline at end of file diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..f01bb43 --- /dev/null +++ b/dub.json @@ -0,0 +1,14 @@ +{ + "authors": [ + "Andrew Lalis" + ], + "copyright": "Copyright © 2025, Andrew Lalis", + "dependencies": { + "handy-http-primitives": "~>1", + "handy-http-transport": "~>1" + }, + "description": "A collection of Handy-HTTP dependencies and common boilerplate code for starting a web server in minutes.", + "license": "CC0", + "name": "handy-http-starter", + "targetType": "library" +} \ No newline at end of file diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..9b179ea --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,10 @@ +{ + "fileVersion": 1, + "versions": { + "handy-http-primitives": "1.0.0", + "handy-http-transport": "1.0.2", + "photon": "0.10.2", + "sharded-map": "2.7.0", + "streams": "3.5.0" + } +} diff --git a/source/handy_http_starter/package.d b/source/handy_http_starter/package.d new file mode 100644 index 0000000..8bc8a0e --- /dev/null +++ b/source/handy_http_starter/package.d @@ -0,0 +1,35 @@ +module handy_http_starter; + +public import handy_http_transport; +public import handy_http_primitives; + +/** + * Starts an HTTP server, using the given handler to handle all incoming + * requests. + * Params: + * handler = The handler to use for requests. + * port = The port to host the server on. Defaults to 8080. + */ +void startServer(HttpRequestHandler handler, ushort port = 8080) { + HttpTransport tp = new Http1Transport(handler, port); + tp.start(); +} + +/** + * Starts an HTTP server, using the given delegate function to handle all + * incoming requests. + * Params: + * dg = The handler delegate function. + * port = The port to host the server on. Defaults to 8080. + */ +void startServer( + void delegate(ref ServerHttpRequest, ref ServerHttpResponse) dg, + ushort port = 8080 +) { + auto handler = new class HttpRequestHandler { + void handle(ref ServerHttpRequest req, ref ServerHttpResponse resp) { + dg(req, resp); + } + }; + startServer(handler, port); +}