Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 10943

Lapis - A web framework for MoonScript & OpenResty

$
0
0

Comments:"Show HN: Lapis – A web framework for MoonScript and OpenResty"

URL:http://leafo.net/lapis/


What is it?

Lapis is a framework for building web applications using MoonScript (or Lua) that runs inside of a customized version of Nginx called OpenResty.

Here’s what is looks like:

lapis=require"lapis"classextendslapis.Application-- Define a basic pattern that matches /"/":=>profile_url=@url_for"profile",name:"leafo"@html->h2"Welcome!"text"Go to my "ahref:profile_url,"profile"-- Define a named route pattern with a variable called name[profile:"/:name"]:=>@html->divclass:"profile",->text"Welcome to the profile of ",@params.name

How does it work?

Lua is run directly inside of the Nginx worker, giving you the smallest barrier between the webserver and your code. OpenResty executes your Lua/MoonScript with LuaJIT, so it’s blazing fast. Have a look at Web Framework Benchmarks just to see how OpenResty stacks up against other platforms.

Nginx’s event loop is used for all asynchronous actions, including HTTP requests and database queries. With the power of Lua coroutines code is written synchronously but runs asynchronously, without all that callback spaghetti seen in other asynchronous platforms. It’s fast, easy to read, and easy to write.

Perform HTTP requests and other asynchronous operations freely without being concerned about blocking your application and killing your throughput.

. . .

importModelfromrequire"lapis.models"-- Create a model, automatically backed by the table `users`classUsersextendsModelold_users=Users\select"where age > ? limit 5",10random_user=Users\find1233-- find by primary keylee=Users\findname:"Lee",email:"leemiller@example.com"user=Users\create{name:"Leaf"email:"leaf@example.com"age:6}user\updateage:10user\delete!

Write your templates with high composability by usingMoonScript directly. No need to learn any goofy templating syntax with arbitrary restrictions.

Either inline the HTML template in the action or create a separate view like so. Views are just classes, feel free to use inheritance and mix and match methods as much as you like.

-- views/index.moonimportWidgetfromrequire"lapis.html"classIndexextendsWidgetcontent:=>h1class:"header","Hello"@user_panel!divclass:"body",->text"Welcome to my site!"user_panel:=>returnunless@current_userdivclass:"user_panel","Welcome back "..@current_user.name

Using all the provided tools we can quickly and logically construct high performance and low memory web applications. Here’s a more complicated example complete with forms, CSRF protection, and various database queries.

importModelfromrequire"lapis.models"importrespond_to,capture_errorsfromrequire"lapis.application"csrf=require"lapis.csrf"classUsersextendsModelclassextendslapis.Application-- Execute code before every action@before_filter=>@csrf_token=csrf.generate_token@[list_users:"/users"]:=>users=Users\select!-- `select` all the users-- Render HTML inline for simplicity@html->ul->foruserin*usersli->ahref:@url_for("user",user.id),user.name[user:"/profile/:id"]:=>user=Users\findid:@params.idreturnstatus:404unlessuser@html->h2user.name-- Respond to different HTTP actions to do the right thing[new_user:"/user/new"]:respond_to{POST:capture_errors=>csrf.assert_token@Users\createname:@params.usernameredirect_to:@url_for"list_users"GET:=>@html->formmethod:"POST",action:@url_for("new_user"),->inputtype:"hidden",name:"csrf_token",value:@csrf_tokeninputtype:"text",name:"username"}

Anything else I should know?

You can deploy a new Lapis application in a few minutes.

If you don’t mind using Heroku then it’s just a matter of using the Lua Buildpack and installing the OpenResty module.

You can use most existing Lua libraries with Lapis with no problems. Here are some libraries you might find useful:

About

Lapis would not be possible without the following libraries:

Lapis is licensed under the MIT license.

Lapis is written by @moonscript.


Viewing all articles
Browse latest Browse all 10943

Trending Articles