Git Product home page Git Product logo

glen's Introduction

glen

Package Version Hex Docs

๐Ÿ•๏ธ A peaceful web framework for Gleam that targets JS.

โœจ Features:

  • Request & response helpers
  • Helpful middleware
  • File streaming
  • Websockets
  • Bring-your-own server (optional)
  • Easily deploy on serverless platforms such as Deno Deploy

...and other nice-to-haves!

Installation

Install all requirements:

gleam add glen gleam_http gleam_javascript

Or just Glen:

gleam add glen

Usage

import gleam/javascript/promise.{type Promise}
import glen
import glen/status

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

fn handle_req(req: glen.Request) -> Promise(glen.Response) {
  "<h1>Welcome to my webpage!</h1>
  <p>Make yourself at home ๐Ÿ˜„</p>"
  |> glen.html(status.ok)
  |> promise.resolve
}

Glen is heavily based off of Wisp, and many of Wisp's examples can easily be ported to Glen. Glen also has an example application of its own in ./test.

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 (>= v17.0.0) or Cloudflare Workers. The convert_request and convert_response functions are here to help you with this.

Cloudflare Workers example

Glen's static middleware will not work on the Cloudflare Workers runtime since it does not support the node:fs module. Everything else should work as expected.

src/index.js

import * as glen from '../glen/glen.mjs';
import * as my_app from './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
import glen
import glen/status

pub fn handle_req(_req) {
  "On a Cloudflare worker!"
  |> glen.html(status.ok)
  |> promise.resolve
}

wrangler.toml

main = "build/dev/javascript/my_app/index.js"
# ...

Docs

Documentation can be found at https://hexdocs.pm/glen.

Development & Contributing

You will need to have Gleam and a JavaScript runtime (most likely Deno) installed for developing Glen.

gleam run   # Run the project
gleam test  # Run the example application

Contributions are encouraged and greatly appreciated! Please note that the changelog should be updated accordingly (this is something that everyone has trouble remembering, including me ๐Ÿ˜…).

glen's People

Contributors

mystpi avatar zhuangya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

glen's Issues

error: Uncaught RangeError: Maximum call stack size exceeded

I'm trying to create a new project using glen. I am using the following code:

pub fn main() {
  glen.serve(8000, fn(_req) {
    "<h1>Welcome to my webpage!</h1>
    <p>Make yourself at home ๐Ÿ˜„</p>"
    |> glen.html(status.ok)
    |> promise.resolve
  })
}

with the following gleam.toml:

name = "app"
version = "1.0.0"
target = "javascript"
[javascript]
runtime = "deno"

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
glen = ">= 2.2.0 and < 3.0.0"
gleam_http = ">= 3.6.0 and < 4.0.0"
gleam_javascript = ">= 0.11.0 and < 1.0.0"
repeatedly = ">= 2.1.1 and < 3.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"

when I do gleam run I get

Compiled in 0.05s
    Running app.main
error: Uncaught RangeError: Maximum call stack size exceeded

If I downgrade glen, I still get this error. I don't know if I am missing something basic

Expose and document API using JS request and response types

It seems the only way to use this library is with Deno or other environments that have a very similar API, but internally this library works with any JS request and response.

It would be fab if functions were exposed to convert from a JS request into a Glen request, and from a Glen response into a JS response.

I would be tempted to also remove the custom serve function in favour of this, as it's quite convoluted to use unless the server you're using has exactly the same API as deno.

Thanks,
Louis

Consider always having the function in the last position

Hello!

Currently the serve function and other have the function first rather than last, which is the Gleam convention.

Having the function last would mean that they are suitable for use expressions, and the formatter will format them nicely.

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

vs

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

Thanks,
Louis

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.