在Heroku云上部署Lua应用

6.5k 词

在Heroku云上部署Lua应用

Running Lua on Heroku

Posted February 05, 2012 by leafo (@moonscript)

Since the release of Heroku’s Cedar platform they've opened up the opportunity for users to run web applications on any stack. Using something called a buildpack we can describe a template for deploying any kind of application. I saw this as a great opportunity to try using Lua on a cloud hosting platform (for free).

因为Heroku发布的Cedar平台,开放了为用户运行任意web应用的栈技术支持,buildpack使我们可以用模板来描述任意类型的应用部署,关键的是这东西,在云上运行lua还免费。

I put together heroku-buildpack-lua, a buildpack containing Lua 5.1 andLuaRocks (a Lua package manager) enable you to quickly deploy Lua along with any required dependencies that can be found on theLuaRocks server.

我做了一个heroku-buidlparck-lua的构建包,包里有lua 5.1和luarocks(LUA包管理器),充许你快速的部署一个lua程序,并且任何相关的依赖,都可以在LuaRocsks服务器上找到。

Here’s a tutorial on getting a very simple app running:
下面的是运行一个简单app的教程。

Feb 12 2012 — I’ve updated the buildpack and this post to simplify the process.

1.Creating An App 2.Describing Dependencies 3.Creating A Web Server 4.Deploying The Web Server 5.What’s Next?

1.创建APP
2.描述依赖
3.创建一个WEB服务器
4.部署WEB服务器
5.后续

1.创建APP

Creating An App
Assuming you've installed heroku we start by creating a new app:

假设你已经安装了heroku,我们就开始创建一个应用。

$ heroku create --stack cedar --buildpack 
http://github.com/leafo/heroku-buildpack-lua.git
Clone the repository it created and we're ready to begin. (stark-dust-4830 was the randomly generated name of my app, replace it with yours.)

克隆分支,创建后我们开始读取。(stark-dust-4830是一个随机生成app名称)

$ git clone git@heroku.com:stark-dusk-4830.git
$cd stark-dust-4830

2.描述依赖

Describing Dependencies
Heroku manages a collection of app servers for us, called web dynos in their terminology. Each application server must expose itself to the outside world. This is done by running a web server on the dyno.

Heroku为我们管理服务器上的应用集合, 术语叫做web dynos,每个应用服务必须对外界暴露自己,这是运行在dyno上的WEB服务。

The Xavante project is simple web server written in Lua with a couple dependencies.
Xavate project是一个简单的用lua弄的web服务,两依赖。
Using LuaRocks bundled in the Lua buildpack, we can easily install Xavante and all its dependencies. We describe the dependencies of our Lua project by creating a rockspec for it.

LuaRocks被绑定在lua级的buidlpack中, 我们可以很容易的安装Xavante和所有她的依赖, 我们创建一个rockkspec的文件来描述我们lua工程的依赖。

A rockspec is a special Lua file ending in .rockspec that describes meta-data about a Lua module. This meta-data includes things like the project name, the maintainer. It also holds any dependencies and how to build the module.

rockspeck是一个特殊的扩名是.rockspec的lua文件,描述关于lua模块的头信息,这个头信息包括了像项目名、维护者,甚至任何的关于如何创建模块的所需的依赖信息。

The Lua buildpack understands the rockspec format, but only looks at the dependencies. Thus, for simplicity we'll only define the dependencies. Go ahead and create app.rockspec and place inside of it:

Lua的构建包知道rockspec的格式,但是只能是依赖,我们的只能定义依赖,以下是之前创建的app.rockspec的内容。

-- app.rockspec
dependencies = {
  "xavante"
}
Xavante will be our only dependency. We're going to keep this tutorial short and leave out the web frameworks. Xavante’s API is flexible enough that it functions as a makeshift framework.

Xavante 是我们唯一的依赖,我们这教程不使用lua web框架。Xavante的API很灵活,是够应付一般函数的简易框架。

If you commit and push, you'll see the buildpack fetch and build all the dependencies. There will be a lot of output, don’t be be concerned.

如果你提交了代码,会看到build会取得重建所有的依赖,会有很多的输出,可无视。

$ git add app.rockspec
$ git commit -m "init"$ git push origin master

-----> Heroku receiving push
-----> Fetching custom buildpack… done
-----> Lua app detected
-----> Copying lua to bin
-----> Installing packages
Installing http://www.luarocks.org/repositories/rocks/xavante-2.2.1-1.all.rock

… output truncated …

-----> Discovering process types
Procfile declares types -> (none)
-----> Compiled slug size is 292K
-----> Launching… done, v5
http://stark-dusk-4830.herokuapp.com deployed to

Heroku
Our dependencies work, but we still haven’t set up a web server. This we'll do by writing some Lua.

依赖配置好了,但还需要配置web服务器,那我们就用lua写点啥。

3.创建WEB服务

Creating A Web Server
We'll use Xavante’s programmatic API to create and run our server through a simple Lua script

Create a file, web.lua, and place in it:

我们用Xavnate的可编程API来创建和运行我们的简单的lua脚本。
创建一个web.lua文件。

-- web.lua
require"xavante"
require"xavante.filehandler"

port =...

xavante.HTTP {
  server = { host ="*", port =tonumber(port) },
  defaultHost = {
    rules = {
      {
        match ="/$",
        with =function(req, res)
          res.headers["Content-type"] ="text/html"
          res.content ="hello world, the time is: "..os.date()
          return res
        end
      }, {
        match =".",
        with = xavante.filehandler,
        params = { baseDir ="static/" }
      }
    }
  }
}

xavante.start()
In this file we create a web server with two simple rules. If you go to the path / then we say hello and show the time. Otherwise, we default to trying to serve files from the static/ directory in our app.

在这个文件,我们创建一个web服务和两个简单的规则。当你访问路径’/'时,
会显示 'say hello’的字样,并且显示时间信息,另外静态文件是放在当前app的static目录 。

Go ahead and create the static/ directory now, and put something inside of it like a favicon or a html file.

继续,创建的 static目录 ,现在就可以把放一些图标和html文件放到里面。

If you have Xavante installed locally, we can test the app. (where 5000 is a port to bind to)

如果Xavante被安装在本地,我们测下APP。(绑定的端口是5000)

$ lua web.lua 5000
Xavante started on port(s) 5000
If not, go on to the next step.
如果不是,进入下一步。

4.部署 Web Server

Deploying The Web Server
Now that all the required code is written, the only thing left to do is to tell Heroku how to start it.

现在所有的代码都写了,接下来唯一要做的是告诉Heroku如何开始。

Heroku uses something called a Procfile to list the commands needed to start things like web severs and workers. We only need a single web server.
Heroku使用了一个叫Procfile的文件,列出了web server要执行的命令任务,我们只需要一个单独的web 服务。
Create a file called Procfile and place inside of it:
创建一个procfile文件。
web: lua web.lua $PORT
Now we're ready to deploy. Commit and push once again.
开始部署,再一次上传代码。
$ git commit -a -m "..."$ git push origin master
We can check and see if our app is running by typing into the console:
使用命令行检查我们正在运行的app。
$ heroku ps
You'll probably see nothing running! It’s because we deployed before without a Procfile. Tell Heroku to start up our web server:

你也看到了,什么都没运行! 因为之前的部署没有Procfile文件,来告诉Heroku是如何启动web服务的。

$ heroku scale web=1
Scaling web processes... done, now running 1

$ heroku ps
Process  State       Command                
-------  ----------  ---------------------  
web.1    up for 16s  bin/lua web.lua $PORT
If you still see nothing running you'll have to debug. Run heroku logs to see if anything failed.
如果你还是看到什么也没运行,执行一下heroku logs,看一下有什么错误发生。
Now our web server is running, navigate to the url of the app to see it live. Don’t forget to try out some of the static files you included.

此刻我们的Web服务在运行了,跳到的Url 证明app在运行中,别忘了测试一下static目录下的文件。

5.后续

What’s Next?
What we've created here is fairly primitive. There are a lot of opportunities for expanding:

这篇比较糙,后续还有很多可以展开的题目 。

Use a Lua web framework like Orbit Talk to a SQL database with LuaSQL Talk to Redis with redis-lua Talk to CouchDB with luachia Make a website in MoonScript

使用Orbit 这种Lua WEB框架。
探讨SQL数据库和LuaSQL
探讨Redis和redis-lua
探讨CouchDB和luachia
使用Moonscript创建站点。

It’s also worth reading the the Lua buildpack’s README because it explains how and where Lua and it’s packages are installed.

更多有价值的信息可以看buildpack里面的README文件, 里面解释了lua和对应包是如何被安装的。

leafo.net 2015 · Generated Fri Apr 8 14:00:07 2016 by Sitegen

翻译:糖果

Lua On Heroku