起步
cpp
#include "servlet.hpp"
namespace hi{
class index : public servlet {
public:
void handler(request& req, response& res) {
res.headers.find("Content-Type")->second = "text/plain;charset=UTF-8";
res.content = "hello,world";
res.status = 200;
}
};
}
extern "C" hi::servlet* create() {
return new hi::index();
}
extern "C" void destroy(hi::servlet* p) {
delete p;
}
java
package hi;
public class index implements hi.servlet {
public index() {
}
public void handler(hi.request req, hi.response res) {
res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
res.status = 200;
res.content = "hello,world";
}
}
python
data='{client},{method},{uri},{user_agent},{param}'.format(client=hi_req.client(),method=hi_req.method(),uri=hi_req.uri(),user_agent=hi_req.user_agent(),param=hi_req.param())
hi_res.header('Content-Type','text/plain;charset=UTF-8')
hi_res.content(data)
hi_res.status(200)
或者使用hi.py框架:
from hi import hi
app =hi()
@app.route(r'^/test/?$',['GET','POST'])
@app.route(r"^/$",['GET'])
def hello_world(req,res,param):
res.header('Content-Type','text/plain;charset=utf-8')
res.content('hello,world')
res.status(200)
@app.route(r"^/client/?$",['GET','POST'])
def client(req,res,param):
res.content('{}<br>{}<br>{}<br>{}<br>{}'.format(req.client(),req.method(),req.uri(),req.user_agent(),req.param()))
res.status(200)
@app.route(r"^/hello/(?P<who>\w+)?$",['GET'])
def hello(req,res,param):
res.content('{}={}'.format('who',param['who']))
res.status(200)
if __name__ == '__main__':
app.run(hi_req,hi_res)
lua
local data=string.format('%s,%s,%s,%s,%s',hi_req:client(),hi_req:method(),hi_req:uri(),hi_req:user_agent(),hi_req:param())
hi_res:header('Content-Type','text/plain;charset=UTF-8')
hi_res:content(data)
hi_res:status(200)
php
require_once 'hi/servlet.php';
require_once 'hi/route.php';
class index implements \hi\servlet {
public function handler(\hi\request $req, \hi\response $res) {
$app = \hi\route::get_instance();
$app->add('{^/$}', array('GET'), function ($rq, $rs, &$param) {
$rs->content = 'hello,world';
$rs->status = 200;
});
$app->add('{^/who/(?P<name>\w+)/?$}', array('GET'), function ($rq, $rs, &$param) {
$rs->content = 'hello,'.$param['name'];
$rs->status = 200;
});
$app->add('{^/phpinfo/?$}', array('GET'), function($rq, $rs, &$param) {
ob_start();
phpinfo();
$rs->content = ob_get_contents();
$rs->status = 200;
ob_end_clean();
});
$app->add('{^/cache/?$}', array('GET'), function($rq, $rs, &$param) {
$key = 'cache_test';
$value = 0;
if (array_key_exists($key, $rq->cache)) {
$value = intval($rq->cache[$key]) + 1;
}
$rs->cache[$key] = $value;
$rs->content = "$key : " . $value;
$rs->status = 200;
});
$app->run($req, $res);
}
}
javascript
if (typeof (Mustache) == 'undefined') {
load('https://cdn.bootcss.com/mustache.js/2.3.0/mustache.min.js')
}
var list = java.util.Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
var template = "{{#list}}* {{.}}\n{{/list}}"
var key = 'test', output
if (hi_req.cache.containsKey(key)) {
output = hi_req.cache.get(key)
} else {
output = Mustache.render(template, {'list': JSON.parse(list.toString())})
hi_res.cache.put(key, output)
}
hi_res.headers.get('Content-Type').set(0, 'text/plain;charset=UTF-8')
hi_res.content = output
hi_res.status = 200;
duktape
hi_res.header('Content-Type','text/plain;charset=UTF-8')
hi_res.content('hello,world')
hi_res.status(200)