1. Installieren Sie die erforderlichen Tools und Bibliotheken
apt-get install capnproto luarocks
luarocks install lua-capnproto
luarocks install lua-cjson
Die lua-cjson-Bibliothek ist nicht erforderlich. Sie wird zusammen installiert, da sie im nachfolgenden Demonstrationsprogramm verwendet wird.
Sie können den Installationsort der Bibliothek überprüfen:
root@350983744ebf:~# luarocks show lua-capnproto
lua-capnproto 0.1.4-5 - Lua-capnproto is a pure lua implementation of capnproto based on LuaJIT.
Lua-capnproto is a pure lua implementation of capnproto based on LuaJIT.
License: BSD
Homepage: https://github.com/calio/lua-capnproto
Installed in: /usr/local
Modules:
capnp (/usr/local/share/lua/5.1/capnp.lua)
capnp.compile (/usr/local/share/lua/5.1/capnp/compile.lua)
capnp.util (/usr/local/share/lua/5.1/capnp/util.lua)
handwritten_capnp (/usr/local/share/lua/5.1/handwritten_capnp.lua)
handwritten_data_generator (/usr/local/share/lua/5.1/handwritten_data_generator.lua)
random (/usr/local/share/lua/5.1/random.lua)
schema_capnp (/usr/local/share/lua/5.1/schema_capnp.lua)
test (/usr/local/share/lua/5.1/test.lua)
tool (/usr/local/share/lua/5.1/tool.lua)
Depends on:
lua ~> 5.1 (using 5.1-1)
Die Standardbibliotheken werden im Verzeichnis /usr/local/share/lua/5.1/ installiert.
2. Kompilieren Sie die CAPNP-Datei
Fügen Sie zunächst das mit openresty gelieferte luajit-Programmverzeichnis zum PATH-Pfad hinzu:
export PATH=$PATH:/usr/local/openresty/luajit/bin
Erstellen Sie eine neue AddressBook.capnp-Datei mit folgendem Inhalt:
@0xdbb9ad1f14bf0b36; # unique file ID, generated by `capnp id`
struct Person {
id @0 :UInt32;
name @1 :Text;
email @2 :Text;
phones @3 :List(PhoneNumber);
struct PhoneNumber {
number @0 :Text;
type @1 :Type;
enum Type {
mobile @0;
home @1;
work @2;
}
}
employment :union {
unemployed @4 :Void;
employer @5 :Text;
school @6 :Text;
selfEmployed @7 :Void;
# We assume that a person is only one of these.
}
}
struct AddressBook {
people @0 :List(Person);
}
Kompilieren Sie dann die capnp-Datei:
root@350983744ebf:~# capnp compile -olua AddressBook.capnp
root@350983744ebf:~# ls -lh
total 36K
-rw-r--r-- 1 root root 532 Oct 22 21:18 AddressBook.capnp
-rw-r--r-- 1 root root 21K Oct 22 21:20 AddressBook_capnp.lua
Rufen Sie die Datei AddressBook_capnp.lua ab.
3. Schreiben Sie ein Testprogramm
Erstellen Sie ein neues Verzeichnis und platzieren Sie die Datei AddressBook_capnp.lua in diesem Verzeichnis.
root@350983744ebf:~# mkdir /usr/local/openresty/lua
root@350983744ebf:~# cd /usr/local/openresty/lua
root@350983744ebf:/usr/local/openresty/lua# cp /root/AddressBook_capnp.lua .
Erstellen Sie in diesem Verzeichnis eine neue Lua-Programmdatei main.lua mit folgendem Inhalt:
local addressBook = require "AddressBook_capnp"
local capnp = require "capnp"
local cjson = require "cjson"
local util = require "capnp.util"
local data = {
people = {
{
id = 123,
name = "Alice",
email = "[email protected]",
phones = {
{
number = "555-1212",
["type"] = "MOBILE",
},
},
employment = {
school = "MIT",
},
},
{
id = 456,
name = "Bob",
email = "[email protected]",
phones = {
{
number = "555-4567",
["type"] = "HOME",
},
{
number = "555-7654",
["type"] = "WORK",
},
},
employment = {
unemployed = "Void",
},
},
}
}
local bin = addressBook.AddressBook.serialize(data)
local decoded = addressBook.AddressBook.parse(bin)
ngx.say(cjson.encode(decoded))
Dieses Programm serialisiert Lua-Daten [das Datenformat ist gemäß der Datei AddressBook.capnp definiert] in capnp-Binärdaten, deserialisiert dann die Binärdaten in Lua-Daten und verwendet schließlich die CJSON-Bibliothek, um die LuA-Daten in das JSON-Format zu serialisieren und zu verwenden nginx, um es auszugeben.
4. Verifizierungsverfahren
Ändern Sie die Nginx-Konfigurationsdatei [/usr/local/openresty/nginx/conf/nginx.conf] wie folgt:
worker_processes 1;
events {
worker_connections 1024;
}
http {
lua_package_path "/usr/local/openresty/lua/?.lua;;";
server {
listen 80;
server_name localhost;
location /capnp {
content_by_lua_file /usr/local/openresty/lua/main.lua;
}
}
}
Starten Sie nginx und überprüfen Sie die Ergebnisse:
root@350983744ebf:~# /usr/local/openresty/nginx/sbin/nginx
root@350983744ebf:~#
root@350983744ebf:~# curl 127.0.0.1/capnp
{"people":[{"phones":[{"number":"555-1212","type":"MOBILE"}],"id":123,"name":"Alice","employment":{"school":"MIT"},"email":"[email protected]"},{"phones":[{"number":"555-4567","type":"HOME"},{"number":"555-7654","type":"WORK"}],"id":456,"name":"Bob","employment":{"unemployed":"Void"},"email":"[email protected]"}]}
Entspricht den Erwartungen.