起步

cpp

  1. #include "servlet.hpp"
  2. namespace hi{
  3. class index : public servlet {
  4. public:
  5. void handler(request& req, response& res) {
  6. res.headers.find("Content-Type")->second = "text/plain;charset=UTF-8";
  7. res.content = "hello,world";
  8. res.status = 200;
  9. }
  10. };
  11. }
  12. extern "C" hi::servlet* create() {
  13. return new hi::index();
  14. }
  15. extern "C" void destroy(hi::servlet* p) {
  16. delete p;
  17. }

java

  1. package hi;
  2. public class index implements hi.servlet {
  3. public index() {
  4. }
  5. public void handler(hi.request req, hi.response res) {
  6. res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
  7. res.status = 200;
  8. res.content = "hello,world";
  9. }
  10. }

python

  1. 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())
  2. hi_res.header('Content-Type','text/plain;charset=UTF-8')
  3. hi_res.content(data)
  4. hi_res.status(200)

或者使用hi.py框架:

  1. from hi import hi
  2. app =hi()
  3. @app.route(r'^/test/?$',['GET','POST'])
  4. @app.route(r"^/$",['GET'])
  5. def hello_world(req,res,param):
  6. res.header('Content-Type','text/plain;charset=utf-8')
  7. res.content('hello,world')
  8. res.status(200)
  9. @app.route(r"^/client/?$",['GET','POST'])
  10. def client(req,res,param):
  11. res.content('{}<br>{}<br>{}<br>{}<br>{}'.format(req.client(),req.method(),req.uri(),req.user_agent(),req.param()))
  12. res.status(200)
  13. @app.route(r"^/hello/(?P<who>\w+)?$",['GET'])
  14. def hello(req,res,param):
  15. res.content('{}={}'.format('who',param['who']))
  16. res.status(200)
  17. if __name__ == '__main__':
  18. app.run(hi_req,hi_res)

lua

  1. 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())
  2. hi_res:header('Content-Type','text/plain;charset=UTF-8')
  3. hi_res:content(data)
  4. hi_res:status(200)

php

  1. require_once 'hi/servlet.php';
  2. require_once 'hi/route.php';
  3. class index implements \hi\servlet {
  4. public function handler(\hi\request $req, \hi\response $res) {
  5. $app = \hi\route::get_instance();
  6. $app->add('{^/$}', array('GET'), function ($rq, $rs, &$param) {
  7. $rs->content = 'hello,world';
  8. $rs->status = 200;
  9. });
  10. $app->add('{^/who/(?P<name>\w+)/?$}', array('GET'), function ($rq, $rs, &$param) {
  11. $rs->content = 'hello,'.$param['name'];
  12. $rs->status = 200;
  13. });
  14. $app->add('{^/phpinfo/?$}', array('GET'), function($rq, $rs, &$param) {
  15. ob_start();
  16. phpinfo();
  17. $rs->content = ob_get_contents();
  18. $rs->status = 200;
  19. ob_end_clean();
  20. });
  21. $app->add('{^/cache/?$}', array('GET'), function($rq, $rs, &$param) {
  22. $key = 'cache_test';
  23. $value = 0;
  24. if (array_key_exists($key, $rq->cache)) {
  25. $value = intval($rq->cache[$key]) + 1;
  26. }
  27. $rs->cache[$key] = $value;
  28. $rs->content = "$key : " . $value;
  29. $rs->status = 200;
  30. });
  31. $app->run($req, $res);
  32. }
  33. }

javascript

  1. if (typeof (Mustache) == 'undefined') {
  2. load('https://cdn.bootcss.com/mustache.js/2.3.0/mustache.min.js')
  3. }
  4. var list = java.util.Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  5. var template = "{{#list}}* {{.}}\n{{/list}}"
  6. var key = 'test', output
  7. if (hi_req.cache.containsKey(key)) {
  8. output = hi_req.cache.get(key)
  9. } else {
  10. output = Mustache.render(template, {'list': JSON.parse(list.toString())})
  11. hi_res.cache.put(key, output)
  12. }
  13. hi_res.headers.get('Content-Type').set(0, 'text/plain;charset=UTF-8')
  14. hi_res.content = output
  15. hi_res.status = 200;

duktape

  1. hi_res.header('Content-Type','text/plain;charset=UTF-8')
  2. hi_res.content('hello,world')
  3. hi_res.status(200)

原文: https://doc.hi-nginx.com/00/0003.html