Movatterモバイル変換


[0]ホーム

URL:


Lapis

Lapis

A web framework forLua

v1.16.0
Reference Reference Manual
Install
luarocks install lapis
For Lua 5.1+/LuaJIT
Github Source on GitHub
November 2nd, 2023: Lapis v1.16.0 —types.params_map, improvedmodel:update:changelog

What is it?

Lapis is a framework for building web applications inLua (orMoonScript) that primarily targetsOpenResty, a high performance web platform that runs on a customized version ofNginx. Lapis can also be used in other server environments, being compatible with any modern version of Lua.

Want to talk Lapis?Join our Discord

locallapis=require"lapis"localapp=lapis.Application()app:match("/",function(self)return"Hello world!"end)returnapp
lapis=require"lapis"classextendslapis.Application"/":=>"Hello world!"

How does it work?

With OpenResty, Lua is run directly inside of the Nginx worker using LuaJIT, giving you thesmallest barrier between the webserver and your code. Have a look atWeb FrameworkBenchmarks just to see how OpenRestystacks up against other platforms.

Utilizing the power ofLuacoroutines, you can writeclean code that looks synchronous but can achieve high throughput byautomatically running asynchronously without blocking. Networking operationslike database queries and HTTP requests will automatically yield to allow forhandling concurrent requests, all without all that callback spaghetti seen inother asynchronous platforms. It’s fast, easy to read, and easy to write.

What does it come with?

Lapis includesURL routing,HTML Templating,CSRF Protection andSessionsupport,PostgreSQL/MySQL/SQLite backed models,schema generation andmigrations in addition to acollection of useful functions neededwhen developing a website.

locallapis=require"lapis"localapp=lapis.Application()-- Define a basic pattern that matches /app:match("/",function(self)localprofile_url=self:url_for("profile",{name="leafo"})-- Use HTML builder syntax helper to quickly and safely write markupreturnself:html(function()h2("Welcome!")text("Go to my ")a({href=profile_url},"profile")end)end)-- Define a named route pattern with a variable called nameapp:match("profile","/:name",function(self)returnself:html(function()div({class="profile"},"Welcome to the profile of "..self.params.name)end)end)returnapp
lapis=require"lapis"classextendslapis.Application-- Define a basic pattern that matches /"/":=>profile_url=@url_for"profile",name:"leafo"-- Use HTML builder syntax helper to quickly and safely write markup@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

Models

Get a powerful abstraction layer over your database tables just bysub-classingModel:

localModel=require("lapis.db.model").Model-- Create a model, backed by the table `users`localUsers=Model:extend("users")-- fetch some rows from the tablelocalelderly_users=Users:select("where age > ? limit 5",10)localrandom_user=Users:find(1233)-- find by primary keylocallee=Users:find({name="Lee",email="leemiller@example.com"})-- create a new row and edit itlocaluser=Users:create({name="Leaf",email="leaf@example.com",age=6})user:update({age=10})user:delete()
importModelfromrequire"lapis.db.model"-- Create a model, automatically backed by the table `users`classUsersextendsModel-- fetch some rows from the tableelderly_users=Users\select"where age > ? limit 5",10random_user=Users\find1233-- find by primary keylee=Users\findname:"Lee",email:"leemiller@example.com"-- create a new row and edit ituser=Users\create{name:"Leaf"email:"leaf@example.com"age:6}user\updateage:10user\delete!

Templates

Write your templates either inetlua or inpure Lua/MoonScript.

TheWidget base class allows you to organize your templates as modules,enabling you to use inheritance and mixins to mix and match methods combinedwith the HTML builder syntax that lets you express HTML with the full power ofthe language you're already using.

The HTML builder syntax makes you immune to cross site scripting attacks fromuser-provided input by ensuring all written content is escaped correctly.

<!-- views/index.etlua --><h1class="header"><%="Hello"%></h1><%ifcurrent_userthen%><divclass="user_panel">Welcomeback<%=current_user.name%></div><%end%><divclass="body">Welcometomysite</div>
importWidgetfromrequire"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

Full Example

Using all the provided tools we can quickly and logically construct highperformance and low memory web applications. Here's a more complicated examplecomplete with forms, CSRF protection, and various database queries.

locallapis=require"lapis"localModel=require("lapis.db.model").Modellocalcapture_errors=require("lapis.application").capture_errorslocalcsrf=require"lapis.csrf"localUsers=Model:extend("users")localapp=lapis.Application()app:before_filter(function(self)self.csrf_token=csrf.generate_token(self)end)app:get("list_users","/users",function(self)self.users=Users:select()-- `select` all usersreturn{render=true}end)app:get("user","/profile/:id",function(self)localuser=Users:find({id=self.params.id})ifnotuserthenreturn{status=404}endreturn{render=true}end)app:post("new_user","/user/new",capture_errors(function(self)csrf.assert_token(self)Users:create({name=assert_error(self.params.username,"Missing username")})return{redirect_to=self:url_for("list_users")}end))app:get("new_user","/user/new",function(self)return{render=true}end)returnapp
lapis=require"lapis"importModelfromrequire"lapis.db.model"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"}

Where can I learn more?

TheReference Manual is both a complete guide and a tutorial to using Lapis.

The source of Lapis can befound on Githuband issues can be reported on theissuestracker.

LuaRocks.org is an open source application written inLapis. It is the public host for all Lua Rocks and thesource can be found onGitHub.

Anything else I should know?

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 projects:

Lapis is licensed under theMIT license.

Lapis is written by@moonscript.

Reference Manual ·Source on GitHub
↑ Top
byleaf corcoran· licensed under MIT
Thu Nov 2 12:54:59 2023 PST
Fork me on GitHub
[8]ページ先頭

©2009-2025 Movatter.jp