Skip to content

Commit

Permalink
💥 Change argument ordering
Browse files Browse the repository at this point in the history
Also add changelog
  • Loading branch information
MystPi committed Feb 4, 2024
1 parent ae325ac commit a501ea7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Changelog to keep track of changes and releases

### Changed

- Argument ordering of `serve` and `custom_serve` to be `use`-friendly and more idiomatic. [BREAKING]
> ```gleam
> // Before
> pub fn main() {
> glen.serve(handle_req, 8000)
> }
>
> // Now
> pub fn main() {
> glen.serve(8000, handle_req)
> }
> ```
> ```gleam
> // Before
> pub fn main() {
> glen.custom_serve(handle_req, my_serve, 8000)
> }
>
> // Now
> pub fn main() {
> glen.serve(8000, my_serve, handle_req)
> }
> ```
2 changes: 1 addition & 1 deletion src/ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as $gleam from './gleam.mjs';
import fs from 'node:fs';
import { createReadableStreamFromReadable } from './stream.mjs';

export function deno_serve(handler, port) {
export function deno_serve(port, handler) {
Deno.serve({ port }, handler);
}

Expand Down
29 changes: 13 additions & 16 deletions src/glen.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ pub type JsHandler =
/// # Examples
///
/// ```
/// fn my_serve(handler: glen.Handler, port: Int) -> Nil {
/// fn my_serve(port: Int, handler: glen.Handler) -> Nil {
/// io.println("Starting server...")
/// glen.serve(handler, port)
/// glen.serve(port, handler)
/// }
/// ```
pub type Handler =
Expand All @@ -72,7 +72,7 @@ pub type Handler =
// SERVER ----------------------------------------------------------------------

@external(javascript, "./ffi.mjs", "deno_serve")
fn deno_serve(handler: JsHandler, port: Int) -> Nil
fn deno_serve(port: Int, handler: JsHandler) -> Nil

/// Start a server using `Deno.serve`.
///
Expand All @@ -82,15 +82,14 @@ fn deno_serve(handler: JsHandler, port: Int) -> Nil
/// # Examples
///
/// ```
/// fn(_req) {
/// glen.serve(8000, fn(_req) {
/// "Hello, world!"
/// |> glen.text(status.ok)
/// |> promise.resolve
/// }
/// |> glen.serve(8000)
/// })
/// ```
pub fn serve(handler: Handler, port: Int) -> Nil {
custom_serve(handler, deno_serve, port)
pub fn serve(port: Int, handler: Handler) -> Nil {
custom_serve(port, deno_serve, handler)
}

/// Start a server using a custom JavaScript server.
Expand All @@ -107,24 +106,22 @@ pub fn serve(handler: Handler, port: Int) -> Nil {
/// @external(javascript, "./serve.mjs", "serve")
/// fn my_serve(handler: glen.JsHandler) -> Nil
///
/// fn(_req) {
/// glen.custom_serve(8000, my_serve, fn(_req) {
/// "Hello, world!"
/// |> glen.text(status.ok)
/// |> promise.resolve
/// }
/// |> glen.custom_serve(my_serve, 8000)
/// })
/// ```
pub fn custom_serve(
handler: Handler,
server: fn(JsHandler, Int) -> Nil,
port: Int,
server: fn(Int, JsHandler) -> Nil,
handler: Handler,
) -> Nil {
fn(req) {
server(port, fn(req) {
convert_request(req)
|> handler
|> promise.map(convert_response)
}
|> server(port)
})
}

/// Convert a JavaScript request into a Glen request.
Expand Down
2 changes: 1 addition & 1 deletion test/glen_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import glen.{type Request, type Response}
import glen/status

pub fn main() {
glen.serve(handle_req, 8000)
glen.serve(8000, handle_req)
}

fn handle_req(req: Request) -> Promise(Response) {
Expand Down

0 comments on commit a501ea7

Please sign in to comment.