Skip to content

Commit

Permalink
🔥 Remove custom_serve function
Browse files Browse the repository at this point in the history
  • Loading branch information
MystPi committed Feb 4, 2024
1 parent a501ea7 commit b73d2a7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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).
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://hexdocs.pm/glen>.
### 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 <https://hexdocs.pm/glen>.

## Development

Expand Down
39 changes: 5 additions & 34 deletions src/glen.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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.
Expand Down

0 comments on commit b73d2a7

Please sign in to comment.