• Stars
    star
    101
  • Rank 325,956 (Top 7 %)
  • Language
    Lua
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A web framework for luvit 2.0 and lit

weblit

A web framework for luvit 2.0 and lit

Weblit is a collection of lit packages that together form a nice web framework.

weblit-app

This is the core of the framework. It's export value is the app itself. The config functions can be chained off this config for super terse syntax.

require('weblit-app')

  .bind({
    host = "0.0.0.0",
    port = 8080
  })

  .use(require('weblit-logger'))
  .use(require('weblit-auto-headers'))
  .use(require('weblit-etag-cache'))

  .route({
    method = "GET",
    path = "/do/:user/:action",
    domain = "*.myapp.io"
  }, function (req, res, go)
    -- Handle route
  end)

  .start()

bind(options)

Use this to configure your server. You can bind to multiple addresses and ports. For example, the same server can listen on port 8080 using normal HTTP while also listening on port 8443 using HTTPS.

-- Listen on port 8080 internally using plain HTTP.
.bind({
  host = "127.0.0.1",
  port = 8080
})

-- Also listen on port 8443 externally using encrypted HTTPS.
.bind({
  host = "0.0.0.0",
  port = 8443,
  tls = {
    cert = module:load("cert.pem"),
    key = module:load("key.pem"),
  }
})

The host option defaults to "127.0.0.1". The default port depends on if you're running as root and if the connection is TLS encrypted.

Root User
HTTP 80 8080
HTTPS 442 8443

use(middleware)

This adds a raw middleware to the chain. It's signature is:

.use(function (req, res, go)
  -- Log the request table
  p("request", req)
  -- Hand off to the next layer.
  return go()
end)

The req table will contain information about the HTTP request. This includes several fields:

  • socket - The raw libuv uv_tty_t socket.
  • method - The HTTP request method verb like GET or POST.
  • path - The raw HTTP request path (including query string).
  • headers - A list of headers. Each header is a table with two entries for key and value. For convenience, there are special __index and __newindex metamethods that let you treat this like a case insensitive key/value store.
  • version - The HTTP version (Usually either 1.0 or 1.1).
  • keepAlive - A flag telling you if this should be a keepalive connection.
  • body - The request body as a string. In the future, this may also be a stream.

The res table also has some conventions used to form the response a piece at a time. Initially it contains:

  • code - The response status code. Initially contains 404.
  • headers - Another special headers table like in req.
  • body - The response body to send. Initially contains "Not Found\n".

The go function is to be called if you wish to not handle a request. This allows other middleware layers to get a chance to respond to the request. Use a tail call if there is nothing more to do.

Otherwise do further processing after go returns. At this point, all inner layers have finished and a response is ready in res.

route(options, middleware)

Route is like use, but allows you to pre-filter the requests before the middleware is called.

.route({
  method = "PUT",
  path = "/upload/:username"
}, function (req, res, go)
  local url = saveFile(req.params.username, req.body)
  res.code = 201
  res.headers.Location = url
end)

The route options accept several parameters:

  • method - This is a simple filter on a specific HTTP method verb.
  • path - This is either an exact match or can contain patterns. Segments looking like :name will match single path segments while :name: will match multiple segments. The matches will go into req.params. Also any query string will be stripped off, parsed out, and stored in req.query.
  • host - Will filter against the Host header. This can be an exact match or a glob match like *.mydomain.org.
  • filter - Filter is a custom lua function that accepts req and returns true or false.

If the request matches all the requirements, then the middleware is called the same as with use.

start

Bind to the port(s), listen on the socket(s) and start accepting connections.

weblit-logger

This is a simple middleware that logs the request method, url and user agent. It also includes the response status code.

Make sure to use it at the top of your middleware chain so that it's able to see the final response code sent to the client.

.use(require('weblit-logger'))

weblit-auto-headers

This implements lots of conventions and useful defaults that help your app implement a proper HTTP server.

You should always use this near the top of the list. The only middleware that goes before this is the logger.

.use(require('weblit-auto-headers'))

weblit-etag-cache

This caches responses in memory keyed by etag. If there is no etag, but there is a response body, it will use the body to generate an etag.

Put this in your list after auto-headers, but before custom server logic.

.use(require('weblit-etag-cache'))

weblit-static

This middleware serves static files to the user. Use this to serve your client- side web assets.

Usage is pretty simplistic for now.

local static = require('weblit-static')
app.use(static("path/to/static/assets"))

If you want to only match a sub-path, use the router.

app.route({
  path = "/blog/:path:"
}, static(pathJoin(module.dir, "articles")))

The path param will be used if it exists and the full path will be used otherwise.

weblit-websocket

This implements a websocket upgrade handler. You can choose the subprotocol and other routing information.

app.websocket({
  path = "/v2/socket", -- Prefix for matching
  protocol = "virgo/2.0", -- Restrict to a websocket sub-protocol
}, function (req, read, write)
  -- Log the request headers
  p(req)
  -- Log and echo all messages
  for message in read do
    write(message)
  end
  -- End the stream
  write()
end)

weblit

This is the metapackage that simply includes the other modules.

It exposes the other modules as a single exports table.

return {
  app = require('weblit-app'),
  autoHeaders = require('weblit-auto-headers'),
  etagCache = require('weblit-etag-cache'),
  logger = require('weblit-logger'),
  static = require('weblit-static'),
  websocket = require('weblit-websocket'),
}

More Repositories

1

js-git

A JavaScript implementation of Git.
JavaScript
3,815
star
2

step

An async control-flow library that makes stepping through logic easy.
JavaScript
2,214
star
3

wheat

Wheat is a blog engine for coders written in node.JS
JavaScript
1,364
star
4

haml-js

Haml ported to server-side Javascript. This is a traditional server-side templating language. Tested with node-js
JavaScript
901
star
5

howtonode.org

This repo contains the actual articles for HowToNode.org
JavaScript
886
star
6

topcube

Webkit Bindings for node
JavaScript
404
star
7

nstore

nStore is a simple, in-process key/value database for node.js
JavaScript
393
star
8

jsonparse

A streaming JSON parser written in pure JavaScript for node.js
JavaScript
349
star
9

do

Do is a simple library for managing async actions in node.JS.
JavaScript
294
star
10

dukluv

LibUV bindings for duktape JS engine
C
265
star
11

msgpack-js

The msgpack protocol implemented in pure javascript.
JavaScript
252
star
12

http-parser-js

A pure JS HTTP parser for node.
JavaScript
237
star
13

node-gir

Node bindings to libgirepository
C++
229
star
14

node-git

Node.JS library to read git repositories.
JavaScript
205
star
15

node-persistence

A high level persistance/database system for node.js
JavaScript
193
star
16

tedit

Chrome app for tedit
JavaScript
188
star
17

node-sdl

Minimal sdl bindings for maximum fun
C++
180
star
18

brozula

VM that runs in the browser and interprets luajit bytecode.
JavaScript
168
star
19

node-router

A simple http server for node.js that has sinatra like qualities. Ideal for generating web services via node.
JavaScript
166
star
20

js-github

An implementation of the js-git interface that mounts a live github repo.
JavaScript
162
star
21

node-webgl

OpenGLES 2.0 bindings for nodeJS with a webGL API
JavaScript
161
star
22

postgres-js

The postgres backend protocol implemented in pure JS for use with node.js
JavaScript
121
star
23

jsgit

A command-line git client powered by js-git and node.js
JavaScript
114
star
24

node-leveldb

NodeJS bindings to levelDB - a fast and lightweight key/value database library
C++
112
star
25

jquery-haml

jQuery-haml is a haml like language written in JSON. This allows for easy dom building so that web apps can do more work independent of the server for a better user experience.
JavaScript
111
star
26

jack

Another new language. The name will probably change.
JavaScript
109
star
27

gen-run

Generator Async Runner. Makes it possible to yield and wait for callbacks and continuables.
JavaScript
105
star
28

stack

Stack is a minimal http module system for node.js
JavaScript
103
star
29

couch-client

A minimal CouchDB client that easy and powerful
JavaScript
101
star
30

node-gamepad

node.js bindings for Alex Diener's cross-platform gamepad code
C
95
star
31

msgpack-js-browser

A browser port of msgpack-js using DataView and ArrayBuffer.
JavaScript
92
star
32

git-browser

Browse Git Repos offline.
JavaScript
88
star
33

js-git-app

A js-git packaged app for chrome and chromebooks.
JavaScript
77
star
34

node-blog

This repo is the code behind the blog HowToNode.org
JavaScript
76
star
35

domchanger

dombuilder that applies diffs only to the real dom
JavaScript
70
star
36

nuv

N-API bindings to libuv with async/await friendly JS wrapping on top.
JavaScript
66
star
37

ivy

Ivy is Tim Caswell's personal node.js distribution, fork and customize!
Shell
59
star
38

conductor

Conducts async functions into orchestrated masterpieces
JavaScript
52
star
39

lua-git

Git implementation in pure lua for luvit.
Lua
51
star
40

nstore-session

This is a connect session store using nStore
JavaScript
51
star
41

libco

Mirror of http://byuu.org/library/libco/
C
51
star
42

rack-php

A php 5.3 framework with built in rack support. You can serve the php site using thin, mongrel, webrick, passenger, mod_php, or mod_fcgid.
JavaScript
51
star
43

jack-old

A new programming language that sits on top of JavaScript in order to iron out the rough spots and please the programmer
JavaScript
47
star
44

wheaty

JS-Git based application hosting platform
JavaScript
45
star
45

sousaball

social networking game project using html5 canvas for the front end and node.js/V8 for the backend.
JavaScript
45
star
46

adventure

Adventure is the start of a real-time RPG world using node.js and Websockets.
JavaScript
41
star
47

movies

The server I use to stream my kids dvd backups over http using node.js from a mac mini running linux.
JavaScript
40
star
48

luvmonkey

Spidermonkey shell with libuv bindings built-in.
C
39
star
49

safereturn

Functional helpers for callback based code.
JavaScript
38
star
50

moonslice-luv

A port of moonslice running on top of luv and lhttp_parser
Lua
38
star
51

seaduk

An implementation of nucleus-js using duktape and libuv implemented in C
C
35
star
52

dombuilder

An easy dombuilder using json-ml style syntax
JavaScript
35
star
53

min-stream

A meta-package for min-stream helper modules.
JavaScript
33
star
54

node-web

A small and fast web/http library for nodejs. (replaces the built-in http module)
JavaScript
33
star
55

refresh

Refreshing REST/Webdav for node
JavaScript
33
star
56

proto

Adds nice utilities to Object.prototype for JavaScript using Object.defineProperty
JavaScript
32
star
57

creationix

My personal sandbox for growing packages.
JavaScript
31
star
58

pattern

An experiment with my new prototypal style programming
JavaScript
30
star
59

rec

A tool for recording CLI programs and posting their output.
JavaScript
29
star
60

uscript

μScript is a simple programming language for μControllers co-designed by a 9-year-old.
C
29
star
61

simple-mime

A small, simple mime database for node.js
JavaScript
29
star
62

grain

Grain is an async framework for node-js templating languages.
JavaScript
28
star
63

fastqueue

A fast push/shift sync queue
JavaScript
28
star
64

sftp-server

sftp-server implemented in pure JS using node
JavaScript
28
star
65

websocket

A simple drop-in replacement for node's TCP server that supports the WebSocket protocol.
JavaScript
26
star
66

nodemcu-webide

A websocket based IDE for nodemcu devices.
JavaScript
25
star
67

LESS.tmbundle

a textmate bundle for less css using the node version of the library at http://github.com/cloudhead/less.js
25
star
68

boxcode

A tiling code editor
JavaScript
23
star
69

firmata-chrome

Firmata driver for chrome.serial
JavaScript
23
star
70

nibs

Lua
21
star
71

topcloud

High level application framework for Javascript (built on top of jquery-haml and modeled somewhat after SproutCore)
JavaScript
21
star
72

basic-games

An archive of the best games I wrote for Q-Basic over a 10 year span.
C++
21
star
73

experiments

Random collection of my experiments
JavaScript
20
star
74

drop-dat

A quick file sharing mechanism using dat libraries.
JavaScript
20
star
75

uvrun

Tiny node module to expose uv_run and uv_run_once to JavaScript
C++
19
star
76

candor.io

candor.io is a server framework that uses the candor language combined with libuv to make fast scripted network servers.
C++
19
star
77

chrome-app-module-loader

A module loader that lets you load npm modules from a chrome app.
JavaScript
19
star
78

nodeconf2012

My slides and content from nodeconf 2012
Lua
18
star
79

world-db

A specialized database for massive tilemaps
JavaScript
18
star
80

git-node-fs

A node adapter for the fs-db mixin for js-git.
JavaScript
18
star
81

lshell

A remote shell using luvit, websockets, and term.js
JavaScript
17
star
82

mine

Miner digs into a javascript file looking for require calls. Used to statically extract common js dependencies.
JavaScript
17
star
83

coffeepot

A compiler for coffee-script written in coffee-script.
JavaScript
17
star
84

git-node

A convenience wrapper for js-git preconfigured for node.js
JavaScript
17
star
85

moonslice

A fast web framework for luvit
JavaScript
16
star
86

git-sha1

A pure js SHA1 implementation created for js-git.
JavaScript
16
star
87

bench-suite

A benchmark skeleton for something better than static hello world http servers.
C
15
star
88

candor.js

An alternate vm for candor that runs in your browser!
JavaScript
14
star
89

ujkl

Micro Jack Lisp - For ESP8266 and Raspberry PI
C
14
star
90

staff

A port of the excellent Backbone.JS with minor API style changes and no dependencies. ES5 support only.
JavaScript
14
star
91

revision

TypeScript
14
star
92

git-repo

A local git repository using any pluggable backend.
JavaScript
14
star
93

msgpack-es

Msgpack implemented in pure ecmascript.
JavaScript
14
star
94

dukcurl

libcurl bindings for duktape
JavaScript
13
star
95

redis-luvit

A redis client for luvit
Lua
12
star
96

jack-lang

C VM for Jack Language
C
12
star
97

wscat

A netcat like client for websockets
Lua
12
star
98

ts-git

TypeScript re-implementation of js-git (aka js-git 2.0)
TypeScript
12
star
99

git-tree

Create a composite tree of js-git repos and work via paths.
JavaScript
11
star
100

luvit-quest

An interactive workshop that teaches network programming with luvit.
JavaScript
11
star