Giter Club home page Giter Club logo

wuhuanhui / hi-nginx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webcpp/hi-nginx

0.0 1.0 0.0 57.27 MB

A fast and robust web server and application server for C++,Python,Lua ,Java, PHP7,Javascript and multiple jsr-223 JVM language

Home Page: https://doc.hi-nginx.com

License: BSD 2-Clause "Simplified" License

Perl 0.04% Vim Script 0.69% HTML 0.03% Shell 0.01% C 60.48% C++ 36.92% XS 0.14% Objective-C 0.01% JavaScript 1.21% Ruby 0.22% Lua 0.16% Makefile 0.04% XSLT 0.06% PHP 0.02%

hi-nginx's Introduction

Features

  • All features of nginx(latest release) are inherited, i.e., it is 100% compatible with nginx.
  • Web development using python, c++, lua, php7, java , javascript and jsr-223 JVM language

Dependency

  • linux
  • gcc and g++(c++11) or clang and clang++
  • python-devel,if --enable-http-hi-python=YES and with-http-hi-python-version=python2
  • python3.x-devel if --enable-http-hi-python=YES and with-http-hi-python-version=python3
  • lua-devel(lua5.x),if --enable-http-hi-lua=YES and --with-http-hi-lua-version=lua5.x
  • luajit-devel,if --enable-http-hi-lua=YES and --with-http-hi-lua-version=luajit
  • jdk 1.8,9,10,11,12,if --enable-http-hi-java=YES
  • PHP 7.0,7.1, 7.2,7.3(--enable-embed=shared),if --enable-http-hi-php=YES

Installation

./configure --help or see install_demo.sh or

                --enable-http-hi-cpp=YES                           \
                --enable-http-hi-python=NO                         \
                --enable-http-hi-lua=NO                            \
                --enable-http-hi-duktape=NO                        \
                --enable-http-hi-java=NO                           \
                --enable-http-hi-php=NO                            \
                --with-http-hi-python-version=python3              \
                --with-http-hi-lua-version=lua5.3                  \
                --add-module=module/ngx_http_hi_module             \
                --add-module=module/ngx_autoblacklist_module               

python, lua and duktape api

hi_req

  • uri
  • method
  • client
  • param
  • user_agent
  • has_header
  • get_header
  • has_form
  • get_form
  • has_session
  • get_session
  • has_cookie
  • get_cookie
  • has_cache
  • get_cache

hi_res

  • status
  • content
  • header
  • session
  • cache

hello,world

cpp servlet class

#include "servlet.hpp"
namespace hi{
class hello : 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::hello();
}

extern "C" void destroy(hi::servlet* p) {
    delete p;
}

cpp compile

g++ -std=c++11 -I/usr/local/nginx/include  -shared -fPIC hello.cpp -o hello.so
install hello.so /usr/local/nginx/cpp

java servlet class

package hi;

public class jhello implements hi.servlet {

    public jhello() {

    }

    public void handler(hi.request req, hi.response res) {
        res.status = 200;
        res.content = "hello,world";

    }
}

java compile

${JAVA_HOME}/bin/javac -classpath .:${NGINX_INSTALL_DIR}/java/hi-nginx-java.jar hi/jhello.java

php servlet class

see php/hi/request.php,php/hi/response.php and php/hi/servlet.php

<?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->run($req, $res);
    }

}

javascript hello world

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

/*
var loaded = hi_module.require('libadder','adder')
hi_res.header('Content-Type','text/plain;charset=UTF-8')
hi_res.content(loaded?adder(1,2).toString():'failed load c module.')
hi_res.status(200)
*/

/*
var echo = require('echo')
var t = new echo()
var route = require('route').get_instance()
route.get('^\/(.*)$', function (req, res, param) {
    res.header('Content-Type', 'text/plain;charset=UTF8')
    res.content(req.method()+'\n'+req.uri() + '\n' + t.concat(param.toString()))
    res.status(200)
})

route.run(hi_req, hi_res)
*/

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

3rd party module

  • nginx-rtmp-module
  • nchan

ngx_http_hi_module directives

  • directives : content: loc,if in loc

    • hi,default: ""

    example:

            location = /hello {
                hi cpp/hello.so ;
            }
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_need_tokens,default: on

    example:

        hi_need_tokens on|off;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_cache_method,default: GET

    example:

        hi_cache_method GET|POST|PUT|HEAD;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_need_cache,default: on

    example:

        hi_need_cache on|off;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_cache_size,default: 10

    example:

        hi_cache_size 10;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_cache_expires,default: 300s

    example:

        hi_cache_expires 300s;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_need_kvdb,default: off

    example:

        hi_need_kvdb on|off;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_kvdb_size,default: 10

    example:

        hi_kvdb_size 10;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_kvdb_expires,default: 300s

    example:

        hi_kvdb_expires 300s;
  • directives : content: http,srv,loc,if in loc ,if in srv
    • hi_need_headers,default: off

      example:

        hi_need_headers on|off;
  • directives : content: http,srv,loc,if in loc ,if in srv
    • hi_need_cookies,default: off

      example:

        hi_need_cookies on|off;
  • directives : content: http,srv,loc,if in loc ,if in srv
    • hi_need_session,default: off

      example:

        hi_need_session on|off;
  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_session_expires,default: 300s

    example:

        hi_session_expires 300s;
  • directives : content: http,srv ,if in srv

    • hi_redis_host,default: ""

    example:

        hi_redis_host 127.0.0.1;
  • directives : content: http,srv,if in srv

    • hi_redis_port,default: 0

    example:

        hi_redis_port 6379;
  • directives : content: loc,if in loc

    • hi_python_content,default: ""

    example:

            location = /pyecho {
                hi_python_content "hi_res.status(200)\nhi_res.content('hello,world')" ;
            }
  • directives : content: loc,if in loc

    • hi_python_script,default: ""

    example:

            location ~ \.py$  {
                hi_python_script python;
            }

or

            location / {
                hi_python_script python/index.py;
            }
  • directives : content: http,srv,if in srv

    • hi_lua_package_path,default: ""

    example:

            hi_lua_package_path '/usr/local/nginx/lua/?.lua;';

  • directives : content: http,srv,if in srv

    • hi_lua_package_cpath,default: ""

    example:

            hi_lua_package_cpath '/usr/local/nginx/lua/?.so;';

  • directives : content: loc,if in loc

    • hi_lua_content,default: ""

    example:

            location = /luaecho {
                hi_lua_content "hi_res:status(200)\nhi_res:content('hello,world')" ;
            }
  • directives : content: loc,if in loc

    • hi_lua_script,default: ""

    example:

            location ~ \.lua$  {
                hi_lua_script lua;
            }

or

            location / {
                hi_lua_script lua/index.lua;
            }
  • directives : content: http,srv,if in srv

    • hi_duktape_package_path,default: ""

    example:

            hi_duktape_package_path '/usr/local/nginx/duktape/package';

  • directives : content: http,srv,if in srv

    • hi_duktape_package_cpath,default: ""

    example:

            hi_duktape_package_cpath '/usr/local/nginx/duktape/package';

  • directives : content: loc,if in loc

    • hi_duktape_content,default: ""

    example:

            location = /jsecho {
                hi_duktape_content "hi_res.status(200);hi_res.content('hello,world')" ;
            }
  • directives : content: loc,if in loc

    • hi_duktape_script,default: ""

    example:

            location ~ \.js$  {
                hi_duktape_script duktape;
            }

or

            location / {
                hi_duktape_script duktape/index.js;
            }
  • directives : content: http,srv,if in srv

    • hi_java_classpath,default:"-Djava.class.path=."

    example:

hi_java_classpath "-Djava.class.path=.:/usr/local/nginx/java:/usr/local/nginx/java/hi-nginx-java.jar";

  • directives : content: http,srv,if in srv

    • hi_java_options,default:"-server -d64 -Xmx1G -Xms1G -Xmn256m"

    example:

hi_java_options "-server -d64 -Xmx3G -Xms3G -Xmn768m -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseNUMA -XX:+CMSParallelRemarkEnabled -XX:MaxTenuringThreshold=15 -XX:MaxGCPauseMillis=30 -XX:GCPauseIntervalMillis=150 -XX:+UseAdaptiveGCBoundary -XX:-UseGCOverheadLimit -XX:+UseBiasedLocking -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=15 -Dfml.ignorePatchDiscrepancies=true -Dfml.ignoreInvalidMinecraftCertificates=true -XX:+UseFastAccessorMethods -XX:+UseCompressedOops -XX:+OptimizeStringConcat -XX:+AggressiveOpts -XX:ReservedCodeCacheSize=2048m -XX:+UseCodeCacheFlushing -XX:SoftRefLRUPolicyMSPerMB=10000 -XX:ParallelGCThreads=10";

  • directives : content: loc,if in loc

    • hi_java_servlet,default:""

    example:

hi_java_servlet hi/jhello;


  • directives : content : http,srv,loc,if in loc ,if in srv

    • hi_java_servlet_cache_expires,default:300s

    example:

hi_java_servlet_cache_expires 300s;

  • directives : content : http,srv,loc,if in loc ,if in srv

    • hi_java_servlet_cache_size,default:10

    example:

hi_java_servlet_cache_size 10;

  • directives : content: http,srv,if in srv

    • hi_java_version,default:8

    example:

hi_java_version 8;

  • directives : content: loc,if in loc

    • hi_javascript_lang,default:javascript

    example:

hi_javascript_lang javascript;

  • directives : content: loc,if in loc

    • hi_javascript_extension,default:js

    example:

hi_javascript_extension js;

  • directives : content: loc,if in loc

    • hi_javascript_content,default:""

    example:

hi_javascript_content "hi_res.content='hello,world';hi_res.status=200;";

  • directives : content: loc,if in loc

    • hi_javascript_script,default:""

    example:

hi_javascript_script javascript/index.js;

or

hi_javascript_script javascript;

  • directives : content: http,srv,loc,if in loc ,if in srv

    • hi_javascript_compiledscript_expires,default:300s

    example:

hi_javascript_compiledscript_expires 300s;
  • directives : content: loc,if in loc

    • hi_php_script,default: ""

    example:

            location ~ \.php$  {
                hi_php_script php;
            }

or

            location / {
                hi_php_script php/index.php;
            }
  • directives : content: loc if in loc

    • hi_subrequest, default : ""

    example:

        location ^~ /sub {
                hi_subrequest '/query';
                hi_lua_content 'hi_res:header("Content-Type",hi_req:get_form("__subrequest_content_type__"))\nhi_res:status(tonumber(hi_req:get_form("__subrequest_status__")))\nhi_res:content(hi_req:get_form("__subrequest_body__"))';
        }

        location ^~ /query {
                internal;
                proxy_pass http://http://hq.sinajs.cn/;
                proxy_set_header Accept-Encoding '';
        }
#  curl -i http://localhost/sub?list=sh601006

ngx_http_autoblacklist_module directives

  • directives : content: http,srv,loc,if in loc ,if in srv

    • autoblacklist, default : "off"
    • autoblacklist_size, default : 1024
    • autoblacklist_limit, default : 30
    • autoblacklist_expires, default : 86400s

    example:

	autoblacklist on|off;
	autoblacklist_size 1024;
	autoblacklist_expires 86400s;
	autoblacklist_limit 30;

nginx.conf

hi_demo_conf

hi-nginx's People

Contributors

webcpp avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.