From b73d2a7f2ebf1c077387e2964458084ee8d9cc69 Mon Sep 17 00:00:00 2001 From: MystPi <86574651+MystPi@users.noreply.github.com> Date: Sun, 4 Feb 2024 15:13:52 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20`custom=5Fserve`=20func?= =?UTF-8?q?tion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 ++++++- README.md | 39 ++++++++++++++++++++++++++++++++++++++- src/glen.gleam | 39 +++++---------------------------------- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d87072..1931af9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Argument ordering of `serve` and `custom_serve` to be `use`-friendly and more idiomatic. [BREAKING] +- Argument ordering of `serve` and `custom_serve` to be `use`-friendly and more idiomatic. > ```gleam > // Before > pub fn main() { @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 > glen.serve(8000, handle_req) > } > ``` + > > ```gleam > // Before > pub fn main() { @@ -36,3 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 > glen.serve(8000, my_serve, handle_req) > } > ``` + +### Removed + +- `custom_serve` function in favor of `convert_request` and `convert_response` (more info in the readme). diff --git a/README.md b/README.md index b660aad..dcb1462 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,44 @@ fn handle_req(req: glen.Request) -> Promise(glen.Response) { Glen is heavily based off of [Wisp](https://github.com/gleam-wisp/wisp), and many of Wisp's [examples](https://github.com/gleam-wisp/wisp/tree/main/examples) can easily be ported to Glen. Glen also has an example application of its own in [./test](https://github.com/MystPi/glen/tree/main/test). -Further documentation can be found at . +### Bring-your-own server + +Glen's `serve` function only works on the `deno` runtime, but you can bring your own server so Glen can work on any runtime, such as Node.js or Cloudflare Workers. The `convert_request` and `convert_response` functions are here to help you with this. Here is an example of a Glen Cloudflare Worker. + +`src/index.js` + +```js +import * as glen from './build/dev/javascript/glen/glen.mjs'; +import * as my_app from './build/dev/javascript/my_app/my_app.mjs'; + +export default { + async fetch(request, _env, _ctx) { + const req = glen.convert_request(request); + const response = await my_app.handle_req(req); + const res = glen.convert_response(response); + + return res; + }, +}; +``` + +`src/my_app.gleam` + +``` +import gleam/javascript/promise.{type Promise} +import glen +import glen/status + +pub fn handle_req(req: glen.Request) -> Promise(glen.Response) { + "On a Cloudflare worker!" + |> glen.html(status.ok) + |> promise.resolve +} +``` + +## Docs + +Documentation can be found at . ## Development diff --git a/src/glen.gleam b/src/glen.gleam index 3253627..f2eb711 100644 --- a/src/glen.gleam +++ b/src/glen.gleam @@ -76,8 +76,7 @@ fn deno_serve(port: Int, handler: JsHandler) -> Nil /// Start a server using `Deno.serve`. /// -/// > ℹ️ Only works when using the `deno` runtime. Try [`custom_serve`](#custom_serve) -/// for other runtimes such as Node.js. +/// > ℹ️ Only works when using the `deno` runtime. See the readme for more info. /// /// # Examples /// @@ -89,39 +88,11 @@ fn deno_serve(port: Int, handler: JsHandler) -> Nil /// }) /// ``` pub fn serve(port: Int, handler: Handler) -> Nil { - custom_serve(port, deno_serve, handler) -} + use req <- deno_serve(port) -/// Start a server using a custom JavaScript server. -/// -/// > ℹ️ The [`serve`](#serve) function is recommended when using the `deno` runtime. -/// -/// > ℹ️ For lower-level customizability and API freedom, consider using the -/// [`convert_request`](#convert_request) and -/// [`convert_response`](#convert_response) functions. -/// -/// # Examples -/// -/// ``` -/// @external(javascript, "./serve.mjs", "serve") -/// fn my_serve(handler: glen.JsHandler) -> Nil -/// -/// glen.custom_serve(8000, my_serve, fn(_req) { -/// "Hello, world!" -/// |> glen.text(status.ok) -/// |> promise.resolve -/// }) -/// ``` -pub fn custom_serve( - port: Int, - server: fn(Int, JsHandler) -> Nil, - handler: Handler, -) -> Nil { - server(port, fn(req) { - convert_request(req) - |> handler - |> promise.map(convert_response) - }) + convert_request(req) + |> handler + |> promise.map(convert_response) } /// Convert a JavaScript request into a Glen request.