├── ansible ├── roles │ ├── front-app │ │ ├── tests │ │ │ ├── inventory │ │ │ └── test.yml │ │ ├── vars │ │ │ └── main.yml │ │ ├── handlers │ │ │ └── main.yml │ │ ├── files │ │ │ └── lua-templates │ │ │ │ ├── layouts │ │ │ │ └── default.lsp │ │ │ │ ├── include │ │ │ │ ├── header.lsp │ │ │ │ └── footer.lsp │ │ │ │ └── index.lsp │ │ ├── defaults │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ ├── .travis.yml │ │ ├── templates │ │ │ └── app.conf │ │ ├── README.md │ │ └── meta │ │ │ └── main.yml │ ├── jdk8 │ │ ├── vars │ │ │ └── main.yml │ │ ├── handlers │ │ │ └── main.yml │ │ ├── defaults │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ ├── README.md │ │ └── meta │ │ │ └── main.yml │ ├── redis │ │ ├── vars │ │ │ └── main.yml │ │ ├── handlers │ │ │ └── main.yml │ │ ├── templates │ │ │ ├── redis.server.service │ │ │ ├── redis.conf.j2 │ │ │ └── redis.init.conf.j2 │ │ ├── README.md │ │ ├── defaults │ │ │ └── main.yml │ │ ├── meta │ │ │ └── main.yml │ │ └── tasks │ │ │ └── main.yml │ ├── common │ │ ├── vars │ │ │ └── main.yml │ │ ├── defaults │ │ │ └── main.yml │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ ├── files │ │ │ └── sources.list │ │ └── meta │ │ │ └── main.yml │ └── openresty │ │ ├── vars │ │ └── main.yml │ │ ├── handlers │ │ └── main.yml │ │ ├── defaults │ │ └── main.yml │ │ ├── templates │ │ ├── nginx.conf │ │ └── nginx │ │ ├── README.md │ │ ├── files │ │ ├── resty-template-1.9 │ │ │ ├── template │ │ │ │ ├── html.lua │ │ │ │ └── microbenchmark.lua │ │ │ └── template.lua │ │ └── lua-resty-redis-0.25 │ │ │ └── redis.lua │ │ ├── tasks │ │ └── main.yml │ │ └── meta │ │ └── main.yml ├── deploy-front-app.yml ├── inventory ├── playbook.yml ├── vars │ └── base-env.yml ├── deploy-worker.yml └── deploy-local.yml ├── autocomplete-worker ├── src │ ├── main │ │ ├── resources │ │ │ └── env.properties │ │ └── java │ │ │ └── codes │ │ │ └── showme │ │ │ └── autocomplete │ │ │ ├── common │ │ │ ├── Configuration.java │ │ │ ├── LoadConfigFromPropertiesException.java │ │ │ ├── Pair.java │ │ │ └── PropertiesConfig.java │ │ │ └── InitWorker.java │ └── test │ │ └── java │ │ └── codes │ │ └── showme │ │ └── autocomple │ │ └── MainTest.java └── pom.xml ├── doc ├── index.lsp.gif ├── meituan.png ├── architecture.png └── key-value-score.png ├── files └── places.txt.zip ├── README.md ├── .gitignore └── Vagrantfile /ansible/roles/front-app/tests/inventory: -------------------------------------------------------------------------------- 1 | localhost -------------------------------------------------------------------------------- /ansible/roles/jdk8/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for jdk8 3 | -------------------------------------------------------------------------------- /ansible/roles/redis/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for redis 3 | -------------------------------------------------------------------------------- /ansible/roles/common/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for common 3 | -------------------------------------------------------------------------------- /ansible/roles/front-app/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for app 3 | -------------------------------------------------------------------------------- /ansible/roles/jdk8/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for jdk8 3 | -------------------------------------------------------------------------------- /ansible/roles/common/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file for common 3 | -------------------------------------------------------------------------------- /ansible/roles/common/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for common 3 | -------------------------------------------------------------------------------- /ansible/roles/openresty/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for openresty 3 | -------------------------------------------------------------------------------- /autocomplete-worker/src/main/resources/env.properties: -------------------------------------------------------------------------------- 1 | redis_ip=192.168.10.12 2 | redis_port=6392 -------------------------------------------------------------------------------- /doc/index.lsp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zacker330/hot-word-recommend/HEAD/doc/index.lsp.gif -------------------------------------------------------------------------------- /doc/meituan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zacker330/hot-word-recommend/HEAD/doc/meituan.png -------------------------------------------------------------------------------- /doc/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zacker330/hot-word-recommend/HEAD/doc/architecture.png -------------------------------------------------------------------------------- /files/places.txt.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zacker330/hot-word-recommend/HEAD/files/places.txt.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 一个热词推荐的简单实现 3 | --- 4 | 详见我的博客: https://showme.codes/2016-12-31/hot-word-recommend-demo/ 5 | 6 | -------------------------------------------------------------------------------- /doc/key-value-score.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zacker330/hot-word-recommend/HEAD/doc/key-value-score.png -------------------------------------------------------------------------------- /ansible/roles/front-app/tests/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | remote_user: root 4 | roles: 5 | - app -------------------------------------------------------------------------------- /ansible/roles/front-app/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart nginx 3 | service: name="nginx" state="restarted" 4 | -------------------------------------------------------------------------------- /ansible/roles/openresty/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart nginx 3 | service: name="nginx" state="restarted" 4 | -------------------------------------------------------------------------------- /ansible/deploy-front-app.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: hotnode1 3 | sudo: yes 4 | vars_files: 5 | - ./vars/base-env.yml 6 | roles: 7 | - front-app 8 | -------------------------------------------------------------------------------- /ansible/roles/front-app/files/lua-templates/layouts/default.lsp: -------------------------------------------------------------------------------- 1 | 2 | {(include/header.lsp)} 3 | 4 | {*view*} 5 | {(include/footer.lsp)} 6 | 7 | 8 | -------------------------------------------------------------------------------- /ansible/roles/redis/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart redis 3 | service: 4 | name: "{{ redis_service_name }}" 5 | state: restarted 6 | when: redis_as_service 7 | -------------------------------------------------------------------------------- /ansible/roles/jdk8/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file for jdk8 3 | jdk8_path: jdk-8u66-linux-x64.tar.gz 4 | jdk8_version_name: jdk1.8.0_66 5 | JAVA_HOME: /usr/lib/jvm/java 6 | user: cloud 7 | user_group: cloud 8 | -------------------------------------------------------------------------------- /ansible/roles/front-app/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file for app 3 | lua_resty_redis_version: 0.25 4 | lua_resty_redis_download_url: "https://github.com/openresty/lua-resty-redis/archive/v{{lua_resty_redis_version}}.tar.gz" 5 | -------------------------------------------------------------------------------- /ansible/inventory: -------------------------------------------------------------------------------- 1 | [all] 2 | 192.168.10.11 3 | 192.168.10.12 4 | 5 | [hotnode1] 6 | 192.168.10.11 ip=192.168.10.11 7 | 8 | [hotnode2] 9 | 192.168.10.12 ip=192.168.10.12 10 | 11 | 12 | [local] 13 | 127.0.0.1 ip=127.0.0.1 ansible_connection=local 14 | -------------------------------------------------------------------------------- /autocomplete-worker/src/main/java/codes/showme/autocomplete/common/Configuration.java: -------------------------------------------------------------------------------- 1 | package codes.showme.autocomplete.common; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by jack on 11/13/16. 7 | */ 8 | public interface Configuration { 9 | String getRedisIP(); 10 | int getRedisPort(); 11 | } 12 | -------------------------------------------------------------------------------- /ansible/roles/front-app/files/lua-templates/include/header.lsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | {{title}} 7 | 8 | 9 | -------------------------------------------------------------------------------- /ansible/roles/openresty/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | ## openresty 4 | 5 | openresty_vhosts_path: "{{openresty_home}}/nginx/conf/servers" 6 | business_lua_path: "{{openresty_home}}/business-lua" 7 | business_html_template: "{{openresty_home}}/nginx/html/templates" 8 | public_static_resource_dir: "{{openresty_home}}/nginx/html" 9 | nginx_log_path: /var/log/nginx 10 | -------------------------------------------------------------------------------- /autocomplete-worker/src/main/java/codes/showme/autocomplete/common/LoadConfigFromPropertiesException.java: -------------------------------------------------------------------------------- 1 | package codes.showme.autocomplete.common; 2 | 3 | /** 4 | * Created by jack on 12/30/16. 5 | */ 6 | public class LoadConfigFromPropertiesException extends RuntimeException { 7 | public LoadConfigFromPropertiesException(Exception e) { 8 | super(e); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ansible/playbook.yml: -------------------------------------------------------------------------------- 1 | - hosts: all 2 | sudo: yes 3 | vars_files: 4 | - ./vars/base-env.yml 5 | roles: 6 | - common 7 | 8 | - hosts: hotnode1 9 | sudo: yes 10 | vars_files: 11 | - ./vars/base-env.yml 12 | roles: 13 | - openresty 14 | - front-app 15 | 16 | - hosts: hotnode2 17 | sudo: yes 18 | vars_files: 19 | - ./vars/base-env.yml 20 | roles: 21 | - redis 22 | - jdk8 23 | -------------------------------------------------------------------------------- /ansible/roles/front-app/files/lua-templates/index.lsp: -------------------------------------------------------------------------------- 1 | {% 2 | layout = "layouts/default.lsp" 3 | local title = "index" 4 | %} 5 | 15 | -------------------------------------------------------------------------------- /ansible/roles/common/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # - name: copy hosts 3 | # copy: src="hosts" dest="/etc/hosts" force=yes 4 | 5 | - name: copy sources.list 6 | copy: src="sources.list" dest="/etc/apt/" force=yes 7 | 8 | - apt: update_cache=yes cache_valid_time=7200 9 | ignore_errors: yes 10 | 11 | - name: add /sbin to path 12 | lineinfile: dest="/etc/profile" line="PATH=/sbin/:$PATH" 13 | 14 | - name: install softwares 15 | apt: name={{item}} state=present allow_unauthenticated=yes 16 | with_items: 17 | - wget 18 | - unzip 19 | - openssh-server 20 | -------------------------------------------------------------------------------- /ansible/roles/front-app/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: copy template html 3 | copy: src="lua-templates/" dest="{{business_html_template}}" owner=root group=root force=true 4 | tags: 5 | - app 6 | 7 | - name: copy public resource 8 | copy: src="public" dest="{{public_static_resource_dir}}" owner=root group=root force=true 9 | tags: 10 | - app 11 | 12 | - name: copy app.conf to nginx servers 13 | template: src="app.conf" dest="{{openresty_vhosts_path}}" owner=root group=root force=true 14 | tags: 15 | - app 16 | 17 | 18 | - name: restart nginx 19 | service: name="nginx" state="restarted" 20 | tags: 21 | - app 22 | -------------------------------------------------------------------------------- /ansible/roles/redis/templates/redis.server.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Redis Datastore Server 3 | After=network.target 4 | 5 | [Service] 6 | Type=forking 7 | PIDFile={{redis_pidfile}} 8 | User={{redis_user}} 9 | Group={{redis_group}} 10 | 11 | Environment=statedir={{redis_dir}} 12 | PermissionsStartOnly=true 13 | ExecStartPre=/bin/mkdir -p ${statedir} 14 | ExecStartPre=/bin/chown -R {{redis_user}}:{{redis_group}} ${statedir} 15 | ExecStart={{redis_dir}}/bin/redis-server {{redis_conf_path}} 16 | ExecReload=/bin/kill -USR2 $MAINPID 17 | ExecStop={{redis_dir}}/bin/redis-cli shutdown 18 | Restart=always 19 | 20 | [Install] 21 | WantedBy=multi-user.target 22 | -------------------------------------------------------------------------------- /ansible/vars/base-env.yml: -------------------------------------------------------------------------------- 1 | --- 2 | user: hotword 3 | user_group: hotword 4 | 5 | places_txt_path: /tmp/places.txt 6 | 7 | ### JVM 8 | JAVA_HOME: /usr/lib/jvm/java 9 | JAVA_PATH: "{{JAVA_HOME}}/bin/java" 10 | 11 | ## openresty 12 | openresty_version: openresty-1.9.15.1 13 | openresty_download_url: "http://openresty.org/download/{{openresty_version}}.tar.gz" 14 | openresty_home: /usr/local/openresty 15 | openresty_vhosts_path: "{{openresty_home}}/nginx/conf/servers" 16 | business_html_template: "{{openresty_home}}/nginx/html/templates" 17 | public_static_resource_dir: "{{openresty_home}}/nginx/html" 18 | 19 | 20 | ### redis 21 | redis_host: 192.168.10.12 22 | redis_port: 6392 23 | -------------------------------------------------------------------------------- /ansible/roles/front-app/.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: python 3 | python: "2.7" 4 | 5 | # Use the new container infrastructure 6 | sudo: false 7 | 8 | # Install ansible 9 | addons: 10 | apt: 11 | packages: 12 | - python-pip 13 | 14 | install: 15 | # Install ansible 16 | - pip install ansible 17 | 18 | # Check ansible version 19 | - ansible --version 20 | 21 | # Create ansible.cfg with correct roles_path 22 | - printf '[defaults]\nroles_path=../' >ansible.cfg 23 | 24 | script: 25 | # Basic role syntax check 26 | - ansible-playbook tests/test.yml -i tests/inventory --syntax-check 27 | 28 | notifications: 29 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ -------------------------------------------------------------------------------- /ansible/deploy-worker.yml: -------------------------------------------------------------------------------- 1 | - hosts: hotnode2 2 | sudo: yes 3 | vars: 4 | worker_path: /tmp/worker.jar 5 | vars_files: 6 | - ./vars/base-env.yml 7 | tasks: 8 | 9 | - name: copy places.zip 10 | unarchive: 11 | src: "../files/places.txt.zip" 12 | dest: "/tmp" 13 | 14 | - name: copy jar to remote machine 15 | copy: 16 | src: "../autocomplete-worker/target/autocomplete-worker-1.0-SNAPSHOT-jar-with-dependencies.jar" 17 | dest: "{{worker_path}}" 18 | 19 | - name: run java 20 | shell: "{{JAVA_PATH}} -jar {{worker_path}} {{places_txt_path}} flushall" 21 | args: 22 | chdir: "/tmp" 23 | # - name: run java 24 | # command: "{{JAVA_PATH}} --jar {{worker_path}} {{places_txt_path}} flushall" 25 | -------------------------------------------------------------------------------- /ansible/deploy-local.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: local 3 | connection: local 4 | vars_files: 5 | - ./vars/base-env.yml 6 | vars: 7 | openresty_vhosts_path: /usr/local/openresty/nginx/conf/servers 8 | tasks: 9 | - name: copy template html 10 | copy: src="./roles/front-app/files/lua-templates/" dest="{{business_html_template}}" force=true 11 | tags: 12 | - app 13 | 14 | - name: copy public resource 15 | copy: src="./roles/front-app/files/public" dest="{{public_static_resource_dir}}" force=true 16 | tags: 17 | - app 18 | 19 | - name: copy app.conf to nginx servers 20 | template: src="./roles/front-app/templates/app.conf" dest="/usr/local/openresty/nginx/conf/servers/app.conf" force=true 21 | tags: 22 | - app 23 | -------------------------------------------------------------------------------- /autocomplete-worker/src/main/java/codes/showme/autocomplete/common/Pair.java: -------------------------------------------------------------------------------- 1 | package codes.showme.autocomplete.common; 2 | 3 | /** 4 | * Created by jack on 12/30/16. 5 | */ 6 | public class Pair { 7 | private final L left; 8 | private final R right; 9 | 10 | public Pair(L left, R right) { 11 | this.left = left; 12 | this.right = right; 13 | } 14 | 15 | public L getLeft() { return left; } 16 | public R getRight() { return right; } 17 | 18 | @Override 19 | public int hashCode() { return left.hashCode() ^ right.hashCode(); } 20 | 21 | @Override 22 | public boolean equals(Object o) { 23 | if (!(o instanceof Pair)) return false; 24 | Pair anotherPair = (Pair) o; 25 | return this.left.equals(anotherPair.getLeft()) && 26 | this.right.equals(anotherPair.getRight()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/kafka_*.tgz 2 | **/zookeeper-*.tar.gz 3 | **/jdk*.tar.gz 4 | **/scala-*.tgz 5 | provision/files/kafka-monitor/*.jar 6 | files/places.txt 7 | 8 | .gradle 9 | build/ 10 | .vagrant 11 | target/ 12 | 13 | .vagrant/ 14 | *.retry 15 | *~ 16 | \#*\# 17 | /.emacs.desktop 18 | /.emacs.desktop.lock 19 | *.elc 20 | auto-save-list 21 | tramp 22 | .\#* 23 | 24 | *.tar.gz 25 | 26 | # Org-mode 27 | .org-id-locations 28 | *_archive 29 | 30 | # flymake-mode 31 | *_flymake.* 32 | 33 | # eshell files 34 | /eshell/history 35 | /eshell/lastdir 36 | 37 | # elpa packages 38 | /elpa/ 39 | 40 | # reftex files 41 | *.rel 42 | 43 | # AUCTeX auto folder 44 | /auto/ 45 | 46 | # cask packages 47 | .cask/ 48 | dist/ 49 | 50 | # Flycheck 51 | flycheck_*.el 52 | 53 | # server auth directory 54 | /server/ 55 | 56 | # projectiles files 57 | .projectile 58 | 59 | 60 | # swap 61 | [._]*.s[a-w][a-z] 62 | [._]s[a-w][a-z] 63 | # session 64 | Session.vim 65 | # temporary 66 | .netrwhist 67 | *~ 68 | # auto-generated tag files 69 | -------------------------------------------------------------------------------- /ansible/roles/common/files/sources.list: -------------------------------------------------------------------------------- 1 | deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse 2 | deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse 3 | deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse 4 | deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse 5 | ##测试版源 6 | deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse 7 | # 源码 8 | deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse 9 | deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse 10 | deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse 11 | deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse 12 | ##测试版源 13 | deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse 14 | # Canonical 合作伙伴和附加 15 | #deb http://archive.canonical.com/ubuntu/ xenial partner 16 | #deb http://extras.ubuntu.com/ubuntu/ xenial main 17 | -------------------------------------------------------------------------------- /ansible/roles/openresty/templates/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes {{ansible_processor_count}}; 2 | 3 | error_log logs/error.log; 4 | events { 5 | worker_connections 1024; 6 | } 7 | 8 | http { 9 | include mime.types; 10 | default_type application/octet-stream; 11 | log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host ' 12 | '$status $body_bytes_sent "$http_referer" ' 13 | '"$http_user_agent" "$http_x_forwarded_for" ' 14 | '$upstream_addr $upstream_status $upstream_cache_status "$upstream_http_content_type" $upstream_response_time > $request_time'; 15 | 16 | access_log logs/access.log main; 17 | 18 | 19 | sendfile on; 20 | server_tokens off; 21 | 22 | keepalive_timeout 65; 23 | 24 | 25 | init_by_lua ' 26 | require "resty.core" 27 | redis = require "resty.redis" 28 | cjson = require "cjson" 29 | template = require "resty.template" 30 | template.caching(false); -- you may remove this on production 31 | '; 32 | 33 | 34 | include servers/*; 35 | } 36 | -------------------------------------------------------------------------------- /ansible/roles/front-app/templates/app.conf: -------------------------------------------------------------------------------- 1 | server{ 2 | listen 80; 3 | server_name {{ip}}; 4 | charset utf-8; 5 | 6 | set $redis_host "{{redis_host}}"; 7 | set $redis_port "{{redis_port}}"; 8 | 9 | location /getdata { 10 | charset_types application/json; 11 | charset 'utf-8'; 12 | default_type application/json; 13 | set_unescape_uri $arg_q; 14 | content_by_lua ' 15 | 16 | local query= ngx.var.arg_q 17 | 18 | local red = redis:new() 19 | red:set_timeout(1000) 20 | local ok, err = red:connect("{{redis_host}}", {{redis_port}}) 21 | if not ok then 22 | ngx.say("failed to connect: ", err) 23 | return 24 | end 25 | 26 | local words, err = red:zrevrange(query, 0, 100, "withscores") 27 | ngx.print(cjson.encode(words)) 28 | ngx.exit(ngx.OK) 29 | 30 | '; 31 | } 32 | 33 | location ~* \.lsp$ { 34 | set $template_root "{{business_html_template}}"; 35 | default_type text/html; 36 | content_by_lua 'template.render(ngx.var.uri)'; 37 | } 38 | 39 | 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # All Vagrant configuration is done below. The "2" in Vagrant.configure 5 | # configures the configuration version (we support older styles for 6 | # backwards compatibility). Please don't change it unless you know what 7 | # you're doing. 8 | Vagrant.configure(2) do |config| 9 | 10 | ANSIBLE_RAW_SSH_ARGS = [] 11 | VAGRANT_VM_PROVIDER = "virtualbox" 12 | machine_box = "boxcutter/ubuntu1604" 13 | 14 | config.vm.define "hotnode1" do |machine| 15 | machine.vm.box = machine_box 16 | machine.vm.hostname = "hotnode1" 17 | machine.vm.network "private_network", ip: "192.168.10.11" 18 | machine.vm.provider "virtualbox" do |node| 19 | node.name = "hotnode1" 20 | node.memory = 1024 21 | node.cpus = 2 22 | end 23 | end 24 | 25 | config.vm.define "hotnode2" do |machine| 26 | machine.vm.box = machine_box 27 | machine.vm.hostname = "hotnode2" 28 | machine.vm.network "private_network", ip: "192.168.10.12" 29 | machine.vm.provider "virtualbox" do |node| 30 | node.name = "hotnode2" 31 | node.memory = 1024 32 | node.cpus = 2 33 | end 34 | end 35 | 36 | end 37 | -------------------------------------------------------------------------------- /autocomplete-worker/src/main/java/codes/showme/autocomplete/common/PropertiesConfig.java: -------------------------------------------------------------------------------- 1 | package codes.showme.autocomplete.common; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Properties; 6 | 7 | /** 8 | * Created by jack on 11/13/16. 9 | */ 10 | public class PropertiesConfig implements Configuration { 11 | private Properties properties = new Properties(); 12 | 13 | public PropertiesConfig() { 14 | InputStream in = getClass().getResourceAsStream("/env.properties"); 15 | try { 16 | properties.load(in); 17 | } catch (IOException e) { 18 | throw new LoadConfigFromPropertiesException(e); 19 | } 20 | } 21 | 22 | @Override 23 | public String getRedisIP() { 24 | return properties.getProperty("redis_ip", "127.0.0.1"); 25 | } 26 | 27 | @Override 28 | public int getRedisPort() { 29 | try { 30 | return Integer.valueOf(properties.getProperty("redis_port", "6379")); 31 | }catch (NumberFormatException e){ 32 | e.printStackTrace(); 33 | return 0; 34 | } 35 | } 36 | 37 | public Properties getProperties() { 38 | return properties; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ansible/roles/jdk8/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - stat: path="/usr/lib/jvm/{{jdk8_version_name}}/bin/java" 3 | register: jvm 4 | tags: 5 | - jdk 6 | 7 | - name: copy jdk 8 | copy: src="{{jdk8_path}}" dest="/tmp" 9 | when: jvm.stat.exists == False 10 | tags: 11 | - jdk 12 | 13 | - name: mkdir for Java 14 | file: path="/usr/lib/jvm/" state=directory mode="u=rwx,go=rx" 15 | when: jvm.stat.exists == False 16 | tags: 17 | - jdk 18 | 19 | - name: install JDK 20 | unarchive: src="/tmp/{{jdk8_path}}" dest="/usr/lib/jvm/" mode="go-w" copy=no 21 | when: jvm.stat.exists == False 22 | tags: 23 | - jdk 24 | 25 | - file: src="/usr/lib/jvm/{{jdk8_version_name}}" dest="{{JAVA_HOME}}" state=link 26 | when: jvm.stat.exists == False 27 | tags: 28 | - jdk 29 | 30 | - name: set JAVA_HOME 31 | lineinfile: dest='/etc/profile' line='export JAVA_HOME={{JAVA_HOME}}' state=present 32 | when: jvm.stat.exists == False 33 | tags: 34 | - jdk 35 | 36 | - lineinfile: dest='/etc/profile' line='export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' state=present 37 | when: jvm.stat.exists == False 38 | tags: 39 | - jdk 40 | 41 | - lineinfile: dest='/etc/profile' line='export PATH="$PATH:$JAVA_HOME/bin"' state=present 42 | when: jvm.stat.exists == False 43 | tags: 44 | - jdk 45 | -------------------------------------------------------------------------------- /ansible/roles/front-app/files/lua-templates/include/footer.lsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 42 | -------------------------------------------------------------------------------- /ansible/roles/jdk8/README.md: -------------------------------------------------------------------------------- 1 | Role Name 2 | ========= 3 | 4 | A brief description of the role goes here. 5 | 6 | Requirements 7 | ------------ 8 | 9 | Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. 10 | 11 | Role Variables 12 | -------------- 13 | 14 | A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. 15 | 16 | Dependencies 17 | ------------ 18 | 19 | A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. 20 | 21 | Example Playbook 22 | ---------------- 23 | 24 | Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: 25 | 26 | - hosts: servers 27 | roles: 28 | - { role: username.rolename, x: 42 } 29 | 30 | License 31 | ------- 32 | 33 | BSD 34 | 35 | Author Information 36 | ------------------ 37 | 38 | An optional section for the role authors to include contact information, or a website (HTML is not allowed). 39 | -------------------------------------------------------------------------------- /ansible/roles/redis/README.md: -------------------------------------------------------------------------------- 1 | Role Name 2 | ========= 3 | 4 | A brief description of the role goes here. 5 | 6 | Requirements 7 | ------------ 8 | 9 | Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. 10 | 11 | Role Variables 12 | -------------- 13 | 14 | A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. 15 | 16 | Dependencies 17 | ------------ 18 | 19 | A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. 20 | 21 | Example Playbook 22 | ---------------- 23 | 24 | Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: 25 | 26 | - hosts: servers 27 | roles: 28 | - { role: username.rolename, x: 42 } 29 | 30 | License 31 | ------- 32 | 33 | BSD 34 | 35 | Author Information 36 | ------------------ 37 | 38 | An optional section for the role authors to include contact information, or a website (HTML is not allowed). 39 | -------------------------------------------------------------------------------- /ansible/roles/front-app/README.md: -------------------------------------------------------------------------------- 1 | Role Name 2 | ========= 3 | 4 | A brief description of the role goes here. 5 | 6 | Requirements 7 | ------------ 8 | 9 | Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. 10 | 11 | Role Variables 12 | -------------- 13 | 14 | A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. 15 | 16 | Dependencies 17 | ------------ 18 | 19 | A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. 20 | 21 | Example Playbook 22 | ---------------- 23 | 24 | Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: 25 | 26 | - hosts: servers 27 | roles: 28 | - { role: username.rolename, x: 42 } 29 | 30 | License 31 | ------- 32 | 33 | BSD 34 | 35 | Author Information 36 | ------------------ 37 | 38 | An optional section for the role authors to include contact information, or a website (HTML is not allowed). 39 | -------------------------------------------------------------------------------- /ansible/roles/openresty/README.md: -------------------------------------------------------------------------------- 1 | Role Name 2 | ========= 3 | 4 | A brief description of the role goes here. 5 | 6 | Requirements 7 | ------------ 8 | 9 | Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. 10 | 11 | Role Variables 12 | -------------- 13 | 14 | A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. 15 | 16 | Dependencies 17 | ------------ 18 | 19 | A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. 20 | 21 | Example Playbook 22 | ---------------- 23 | 24 | Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: 25 | 26 | - hosts: servers 27 | roles: 28 | - { role: username.rolename, x: 42 } 29 | 30 | License 31 | ------- 32 | 33 | BSD 34 | 35 | Author Information 36 | ------------------ 37 | 38 | An optional section for the role authors to include contact information, or a website (HTML is not allowed). 39 | -------------------------------------------------------------------------------- /ansible/roles/openresty/files/resty-template-1.9/template/html.lua: -------------------------------------------------------------------------------- 1 | local template = require "resty.template" 2 | local setmetatable = setmetatable 3 | local escape = template.escape 4 | local concat = table.concat 5 | local pairs = pairs 6 | local type = type 7 | 8 | local function tag(name, content, attr) 9 | local r, a, content = {}, {}, content or attr 10 | r[#r + 1] = "<" 11 | r[#r + 1] = name 12 | if attr then 13 | for k, v in pairs(attr) do 14 | if type(k) == "number" then 15 | a[#a + 1] = escape(v) 16 | else 17 | a[#a + 1] = k .. '="' .. escape(v) .. '"' 18 | end 19 | end 20 | if #a > 0 then 21 | r[#r + 1] = " " 22 | r[#r + 1] = concat(a, " ") 23 | end 24 | end 25 | if type(content) == "string" then 26 | r[#r + 1] = ">" 27 | r[#r + 1] = escape(content) 28 | r[#r + 1] = "" 31 | else 32 | r[#r + 1] = " />" 33 | end 34 | return concat(r) 35 | end 36 | 37 | local html = { __index = function(_, name) 38 | return function(attr) 39 | if type(attr) == "table" then 40 | return function(content) 41 | return tag(name, content, attr) 42 | end 43 | else 44 | return tag(name, attr) 45 | end 46 | end 47 | end } 48 | 49 | template.html = setmetatable(html, html) 50 | 51 | return template.html 52 | -------------------------------------------------------------------------------- /autocomplete-worker/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | codes.showme 6 | autocomplete-worker 7 | 1.0-SNAPSHOT 8 | 9 | 10 | 11 | central 12 | http://maven.aliyun.com/nexus/content/groups/public/ 13 | 14 | 15 | 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | redis.clients 24 | jedis 25 | 2.9.0 26 | jar 27 | compile 28 | 29 | 30 | commons-io 31 | commons-io 32 | 2.5 33 | 34 | 35 | junit 36 | junit 37 | 4.12 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | maven-compiler-plugin 47 | 3.5.1 48 | 49 | 1.8 50 | 1.8 51 | UTF-8 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.apache.maven.plugins 59 | maven-assembly-plugin 60 | 2.5.5 61 | 62 | 63 | 64 | codes.showme.autocomplete.InitWorker 65 | 66 | 67 | 68 | jar-with-dependencies 69 | 70 | 71 | 72 | 73 | make-assembly 74 | package 75 | 76 | single 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /ansible/roles/openresty/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # 安装openresty,不作配置 3 | - name: install prepare software 4 | apt: name="{{item}}" state=present 5 | sudo: true 6 | with_items: 7 | - "libreadline-dev" 8 | - "libpcre3-dev" 9 | - "libssl-dev" 10 | - "perl" 11 | - "build-essential" 12 | tags: 13 | - openresty 14 | 15 | 16 | - stat: path="/usr/local/openresty" 17 | register: openresty 18 | tags: 19 | - openresty 20 | 21 | - name: download openresty 22 | get_url: url="{{openresty_download_url}}" dest="/tmp" owner=root group=root 23 | when: openresty.stat.exists == False 24 | tags: 25 | - openresty 26 | 27 | - name: install nginx 28 | unarchive: src="/tmp/{{openresty_version}}.tar.gz" dest="/tmp" mode="go-w" copy=no 29 | when: openresty.stat.exists == False 30 | tags: 31 | - openresty 32 | 33 | 34 | 35 | - name: ./configure --with-luajit 36 | shell: "cd /tmp/{{openresty_version}}/ && sudo ./configure --with-luajit " 37 | when: openresty.stat.exists == False 38 | tags: 39 | - openresty 40 | 41 | - name: make openresty 42 | shell: "cd /tmp/{{openresty_version}}/ && make && sudo make install" 43 | when: openresty.stat.exists == False 44 | tags: 45 | - openresty 46 | 47 | 48 | - name: link openresty home "{{openresty_home}}" 49 | file: src="{{openresty_home}}" dest="/usr/local/nginx" owner=root group=root state=link 50 | tags: 51 | - openresty 52 | 53 | - name: mv resty-template to right path 54 | copy: src="resty-template-1.9/" dest="{{openresty_home}}/lualib/resty/" owner=root group=root 55 | tags: 56 | - openresty 57 | 58 | - name: mv resty-redis to right path 59 | copy: src="lua-resty-redis-0.25/" dest="{{openresty_home}}/lualib/resty/" owner=root group=root 60 | tags: 61 | - openresty 62 | 63 | - name: mkdir log folder 64 | file: path="{{nginx_log_path}}" owner=nobody group=root state=directory mode=0755 65 | tags: 66 | - openresty 67 | 68 | - name: config nginx.conf 69 | template: src="nginx.conf" force=true dest="{{openresty_home }}/nginx/conf/" owner=root group=root 70 | tags: 71 | - configure 72 | 73 | 74 | - name: copy nginx init.d script 75 | template: src="nginx" dest="/etc/init.d" force=true 76 | tags: 77 | - openresty 78 | 79 | - file: dest="/etc/init.d/nginx" state=touch mode="u=rwx,g=rx,o=rx" force=true owner=root group=root 80 | notify: restart nginx 81 | tags: 82 | - openresty 83 | 84 | - shell: "sudo update-rc.d -f nginx defaults" 85 | notify: restart nginx 86 | tags: 87 | - openresty 88 | 89 | - name: mkdir vhosts dir 90 | file: path="{{openresty_vhosts_path}}" state=directory owner=root group=root recurse=true 91 | notify: restart nginx 92 | tags: 93 | - openresty 94 | 95 | 96 | - name: restart nginx 97 | service: name="nginx" state="started" 98 | tags: 99 | - openresty 100 | -------------------------------------------------------------------------------- /ansible/roles/redis/templates/redis.conf.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | # General 4 | daemonize {{ redis_daemonize }} 5 | pidfile {{ redis_pidfile }} 6 | dir {{ redis_dir }} 7 | port {{ redis_port }} 8 | bind {{ redis_bind }} 9 | {% if redis_socket_path -%} 10 | unixsocket {{ redis_socket_path }} 11 | unixsocketperm {{ redis_socket_perm }} 12 | {% endif -%} 13 | timeout {{ redis_timeout }} 14 | tcp-keepalive {{ redis_tcp_keepalive }} 15 | tcp-backlog {{ redis_tcp_backlog }} 16 | loglevel {{ redis_loglevel }} 17 | logfile {{ redis_logfile }} 18 | syslog-enabled {{ redis_syslog_enabled }} 19 | syslog-ident {{ redis_syslog_ident }} 20 | syslog-facility {{ redis_syslog_facility }} 21 | databases {{ redis_databases }} 22 | 23 | # Snapshotting 24 | {% for save in redis_save -%} 25 | save {{ save }} 26 | {% endfor -%} 27 | stop-writes-on-bgsave-error yes 28 | rdbcompression yes 29 | rdbchecksum yes 30 | dbfilename dump.rdb 31 | 32 | # Replication 33 | {% if redis_slaveof -%} 34 | slaveof {{ redis_slaveof }} 35 | {% endif -%} 36 | slave-serve-stale-data yes 37 | slave-read-only {{ redis_slave_read_only }} 38 | repl-disable-tcp-nodelay no 39 | {% if redis_repl_backlog_size -%} 40 | repl-backlog-size {{ redis_repl_backlog_size }} 41 | {% endif -%} 42 | slave-priority {{ redis_slave_priority }} 43 | {% if redis_min_slaves_to_write -%} 44 | min-slaves-to-write {{ redis_min_slaves_to_write }} 45 | {% endif -%} 46 | {% if redis_min_slaves_max_lag -%} 47 | min-slaves-max-lag {{ redis_min_slaves_max_lag }} 48 | {% endif -%} 49 | {% if redis_password -%} 50 | masterauth {{ redis_password }} 51 | {% endif -%} 52 | 53 | # Security 54 | {% if redis_password -%} 55 | requirepass {{ redis_password }} 56 | {% endif -%} 57 | {% for command in redis_rename_commands -%} 58 | rename-command {{ command }} 59 | {% endfor -%} 60 | 61 | # Limits 62 | maxclients {{ redis_maxclients }} 63 | {% if redis_maxmemory -%} 64 | maxmemory {{ redis_maxmemory }} 65 | {% endif -%} 66 | maxmemory-policy {{ redis_maxmemory_policy }} 67 | 68 | # Append Only Mode 69 | appendonly {{ redis_appendonly }} 70 | appendfilename "{{ redis_appendfilename }}" 71 | appendfsync {{ redis_appendfsync }} 72 | no-appendfsync-on-rewrite {{ redis_no_appendfsync_on_rewrite }} 73 | auto-aof-rewrite-percentage {{ redis_auto_aof_rewrite_percentage }} 74 | auto-aof-rewrite-min-size {{ redis_auto_aof_rewrite_min_size }} 75 | 76 | # Lua 77 | lua-time-limit 5000 78 | 79 | # Slow Log 80 | slowlog-log-slower-than {{ redis_slowlog_log_slower_than }} 81 | slowlog-max-len {{ redis_slowlog_max_len }} 82 | 83 | # Event Notification 84 | notify-keyspace-events "" 85 | 86 | # Advanced 87 | hash-max-ziplist-entries 512 88 | hash-max-ziplist-value 64 89 | list-max-ziplist-entries 512 90 | list-max-ziplist-value 64 91 | set-max-intset-entries 512 92 | zset-max-ziplist-entries 128 93 | zset-max-ziplist-value 64 94 | activerehashing yes 95 | client-output-buffer-limit normal 0 0 0 96 | client-output-buffer-limit slave 256mb 64mb 60 97 | client-output-buffer-limit pubsub 32mb 8mb 60 98 | hz 10 99 | aof-rewrite-incremental-fsync yes -------------------------------------------------------------------------------- /ansible/roles/redis/templates/redis.init.conf.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | # General 4 | daemonize {{ redis_daemonize }} 5 | pidfile {{ redis_pidfile }} 6 | dir {{ redis_dir }} 7 | port {{ redis_port }} 8 | bind {{ redis_bind }} 9 | {% if redis_socket_path -%} 10 | unixsocket {{ redis_socket_path }} 11 | unixsocketperm {{ redis_socket_perm }} 12 | {% endif -%} 13 | timeout {{ redis_timeout }} 14 | tcp-keepalive {{ redis_tcp_keepalive }} 15 | tcp-backlog {{ redis_tcp_backlog }} 16 | loglevel {{ redis_loglevel }} 17 | logfile {{ redis_logfile }} 18 | syslog-enabled {{ redis_syslog_enabled }} 19 | syslog-ident {{ redis_syslog_ident }} 20 | syslog-facility {{ redis_syslog_facility }} 21 | databases {{ redis_databases }} 22 | 23 | # Snapshotting 24 | {% for save in redis_save -%} 25 | save {{ save }} 26 | {% endfor -%} 27 | stop-writes-on-bgsave-error yes 28 | rdbcompression yes 29 | rdbchecksum yes 30 | dbfilename dump.rdb 31 | 32 | # Replication 33 | {% if redis_slaveof -%} 34 | slaveof {{ redis_slaveof }} 35 | {% endif -%} 36 | slave-serve-stale-data yes 37 | slave-read-only {{ redis_slave_read_only }} 38 | repl-disable-tcp-nodelay no 39 | {% if redis_repl_backlog_size -%} 40 | repl-backlog-size {{ redis_repl_backlog_size }} 41 | {% endif -%} 42 | slave-priority {{ redis_slave_priority }} 43 | {% if redis_min_slaves_to_write -%} 44 | min-slaves-to-write {{ redis_min_slaves_to_write }} 45 | {% endif -%} 46 | {% if redis_min_slaves_max_lag -%} 47 | min-slaves-max-lag {{ redis_min_slaves_max_lag }} 48 | {% endif -%} 49 | {% if redis_password -%} 50 | masterauth {{ redis_password }} 51 | {% endif -%} 52 | 53 | # Security 54 | {% if redis_password -%} 55 | requirepass {{ redis_password }} 56 | {% endif -%} 57 | {% for command in redis_rename_commands -%} 58 | rename-command {{ command }} 59 | {% endfor -%} 60 | 61 | # Limits 62 | maxclients {{ redis_maxclients }} 63 | {% if redis_maxmemory -%} 64 | maxmemory {{ redis_maxmemory }} 65 | {% endif -%} 66 | maxmemory-policy {{ redis_maxmemory_policy }} 67 | 68 | # Append Only Mode 69 | appendonly {{ redis_appendonly }} 70 | appendfilename "{{ redis_appendfilename }}" 71 | appendfsync {{ redis_appendfsync }} 72 | no-appendfsync-on-rewrite {{ redis_no_appendfsync_on_rewrite }} 73 | auto-aof-rewrite-percentage {{ redis_auto_aof_rewrite_percentage }} 74 | auto-aof-rewrite-min-size {{ redis_auto_aof_rewrite_min_size }} 75 | 76 | # Lua 77 | lua-time-limit 5000 78 | 79 | # Slow Log 80 | slowlog-log-slower-than {{ redis_slowlog_log_slower_than }} 81 | slowlog-max-len {{ redis_slowlog_max_len }} 82 | 83 | # Event Notification 84 | notify-keyspace-events "" 85 | 86 | # Advanced 87 | hash-max-ziplist-entries 512 88 | hash-max-ziplist-value 64 89 | list-max-ziplist-entries 512 90 | list-max-ziplist-value 64 91 | set-max-intset-entries 512 92 | zset-max-ziplist-entries 128 93 | zset-max-ziplist-value 64 94 | activerehashing yes 95 | client-output-buffer-limit normal 0 0 0 96 | client-output-buffer-limit slave 256mb 64mb 60 97 | client-output-buffer-limit pubsub 32mb 8mb 60 98 | hz 10 99 | aof-rewrite-incremental-fsync yes -------------------------------------------------------------------------------- /ansible/roles/redis/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | ## Installation options 3 | redis_home: /var/lib/redis 4 | redis_conf_path: /etc/redis/redis.conf 5 | 6 | redis_version: 3.2.1 7 | redis_install_dir: "{{redis_home}}" 8 | redis_user: redis 9 | redis_group: redis 10 | redis_dir: "{{redis_home}}" 11 | redis_verify_checksum: false 12 | redis_download_url: http://download.redis.io/releases/redis-3.2.1.tar.gz 13 | redis_tar_file: redis-3.2.1.tar.gz 14 | # The open file limit for Redis/Sentinel 15 | redis_nofile_limit: 16384 16 | 17 | ## Role options 18 | # Configure Redis as a service 19 | # This creates the init scripts for Redis and ensures the process is running 20 | # Also applies for Redis Sentinel 21 | redis_as_service: true 22 | # Add local facts to /etc/ansible/facts.d for Redis 23 | redis_local_facts: true 24 | # Service name 25 | redis_service_name: "redis_{{ redis_port }}.service" 26 | 27 | ## Networking/connection options 28 | redis_bind: 0.0.0.0 29 | redis_port: 6379 30 | redis_password: false 31 | # Slave replication options 32 | redis_min_slaves_to_write: 0 33 | redis_min_slaves_max_lag: 10 34 | redis_tcp_backlog: 511 35 | redis_tcp_keepalive: 0 36 | # Max connected clients at a time 37 | redis_maxclients: 10000 38 | redis_timeout: 0 39 | # Socket options 40 | # Set socket_path to the desired path to the socket. E.g. /var/run/redis/{{ redis_port }}.sock 41 | redis_socket_path: false 42 | redis_socket_perm: 755 43 | 44 | ## Replication options 45 | # Set slaveof just as you would in redis.conf. (e.g. "redis01 6379") 46 | redis_slaveof: false 47 | # Make slaves read-only. "yes" or "no" 48 | redis_slave_read_only: "yes" 49 | redis_slave_priority: 100 50 | redis_repl_backlog_size: false 51 | 52 | ## Logging 53 | redis_logfile: '""' 54 | # Enable syslog. "yes" or "no" 55 | redis_syslog_enabled: "yes" 56 | redis_syslog_ident: "{{ redis_service_name }}" 57 | # Syslog facility. Must be USER or LOCAL0-LOCAL7 58 | redis_syslog_facility: USER 59 | 60 | ## General configuration 61 | redis_daemonize: "yes" 62 | redis_pidfile: /var/run/redis/{{ redis_port }}.pid 63 | # Number of databases to allow 64 | redis_databases: 16 65 | redis_loglevel: notice 66 | # Log queries slower than this many milliseconds. -1 to disable 67 | redis_slowlog_log_slower_than: 10000 68 | # Maximum number of slow queries to save 69 | redis_slowlog_max_len: 128 70 | # Redis memory limit (e.g. 4294967296, 4096mb, 4gb) 71 | redis_maxmemory: false 72 | redis_maxmemory_policy: noeviction 73 | redis_rename_commands: [] 74 | # How frequently to snapshot the database to disk 75 | # e.g. "900 1" => 900 seconds if at least 1 key changed 76 | redis_save: 77 | - 900 1 78 | - 300 10 79 | - 60 10000 80 | redis_appendonly: "no" 81 | redis_appendfilename: "appendonly.aof" 82 | redis_appendfsync: "everysec" 83 | redis_no_appendfsync_on_rewrite: "no" 84 | redis_auto_aof_rewrite_percentage: "100" 85 | redis_auto_aof_rewrite_min_size: "64mb" 86 | 87 | ## Redis sentinel configs 88 | # Set this to true on a host to configure it as a Sentinel 89 | redis_sentinel: false 90 | redis_sentinel_dir: /var/lib/redis/sentinel_{{ redis_sentinel_port }} 91 | redis_sentinel_bind: 0.0.0.0 92 | redis_sentinel_port: 26379 93 | redis_sentinel_pidfile: /var/run/redis/sentinel_{{ redis_sentinel_port }}.pid 94 | redis_sentinel_logfile: '""' 95 | redis_sentinel_syslog_ident: sentinel_{{ redis_sentinel_port }} 96 | redis_sentinel_monitors: 97 | - name: master01 98 | host: localhost 99 | port: 6379 100 | quorum: 2 101 | auth_pass: ant1r3z 102 | down_after_milliseconds: 30000 103 | parallel_syncs: 1 104 | failover_timeout: 180000 105 | notification_script: false 106 | client_reconfig_script: false 107 | -------------------------------------------------------------------------------- /ansible/roles/openresty/templates/nginx: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # chkconfig: 2345 55 25 4 | # Description: Nginx init.d script, put in /etc/init.d, chmod +x /etc/init.d/nginx 5 | # For Debian, run: update-rc.d -f nginx defaults 6 | # For CentOS, run: chkconfig --add nginx 7 | # 8 | ### BEGIN INIT INFO 9 | # Provides: nginx 10 | # Required-Start: $all 11 | # Required-Stop: $all 12 | # Default-Start: 2 3 4 5 13 | # Default-Stop: 0 1 6 14 | # Short-Description: nginx init.d script 15 | # Description: OpenResty (aka. ngx_openresty) is a full-fledged web application server by bundling the standard Nginx core, lots of 3rd-party Nginx modules, as well as most of their external dependencies. 16 | ### END INIT INFO 17 | # 18 | 19 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 20 | DESC="Nginx Daemon" 21 | NAME=nginx 22 | PREFIX={{openresty_home}}/nginx 23 | DAEMON=$PREFIX/sbin/$NAME 24 | CONF=$PREFIX/conf/$NAME.conf 25 | PID=$PREFIX/logs/$NAME.pid 26 | SCRIPT=/etc/init.d/$NAME 27 | 28 | if [ ! -x "$DAEMON" ] || [ ! -f "$CONF" ]; then 29 | echo -e "\033[33m $DAEMON has no permission to run. \033[0m" 30 | echo -e "\033[33m Or $CONF doesn't exist. \033[0m" 31 | sleep 1 32 | exit 1 33 | fi 34 | 35 | do_start() { 36 | if [ -f $PID ]; then 37 | echo -e "\033[33m $PID already exists. \033[0m" 38 | echo -e "\033[33m $DESC is already running or crashed. \033[0m" 39 | echo -e "\033[32m $DESC Reopening $CONF ... \033[0m" 40 | $DAEMON -s reopen -c $CONF 41 | sleep 1 42 | echo -e "\033[36m $DESC reopened. \033[0m" 43 | else 44 | echo -e "\033[32m $DESC Starting $CONF ... \033[0m" 45 | $DAEMON -c $CONF 46 | sleep 1 47 | echo -e "\033[36m $DESC started. \033[0m" 48 | fi 49 | } 50 | 51 | do_stop() { 52 | if [ ! -f $PID ]; then 53 | echo -e "\033[33m $PID doesn't exist. \033[0m" 54 | echo -e "\033[33m $DESC isn't running. \033[0m" 55 | else 56 | echo -e "\033[32m $DESC Stopping $CONF ... \033[0m" 57 | $DAEMON -s stop -c $CONF 58 | sleep 1 59 | echo -e "\033[36m $DESC stopped. \033[0m" 60 | fi 61 | } 62 | 63 | do_reload() { 64 | if [ ! -f $PID ]; then 65 | echo -e "\033[33m $PID doesn't exist. \033[0m" 66 | echo -e "\033[33m $DESC isn't running. \033[0m" 67 | echo -e "\033[32m $DESC Starting $CONF ... \033[0m" 68 | $DAEMON -c $CONF 69 | sleep 1 70 | echo -e "\033[36m $DESC started. \033[0m" 71 | else 72 | echo -e "\033[32m $DESC Reloading $CONF ... \033[0m" 73 | $DAEMON -s reload -c $CONF 74 | sleep 1 75 | echo -e "\033[36m $DESC reloaded. \033[0m" 76 | fi 77 | } 78 | 79 | do_quit() { 80 | if [ ! -f $PID ]; then 81 | echo -e "\033[33m $PID doesn't exist. \033[0m" 82 | echo -e "\033[33m $DESC isn't running. \033[0m" 83 | else 84 | echo -e "\033[32m $DESC Quitting $CONF ... \033[0m" 85 | $DAEMON -s quit -c $CONF 86 | sleep 1 87 | echo -e "\033[36m $DESC quitted. \033[0m" 88 | fi 89 | } 90 | 91 | do_test() { 92 | echo -e "\033[32m $DESC Testing $CONF ... \033[0m" 93 | $DAEMON -t -c $CONF 94 | } 95 | 96 | do_info() { 97 | $DAEMON -V 98 | } 99 | 100 | case "$1" in 101 | start) 102 | do_start 103 | ;; 104 | stop) 105 | do_stop 106 | ;; 107 | reload) 108 | do_reload 109 | ;; 110 | restart) 111 | do_stop 112 | do_start 113 | ;; 114 | quit) 115 | do_quit 116 | ;; 117 | test) 118 | do_test 119 | ;; 120 | info) 121 | do_info 122 | ;; 123 | *) 124 | echo "Usage: $SCRIPT {start|stop|reload|restart|quit|test|info}" 125 | exit 2 126 | ;; 127 | esac 128 | 129 | exit 0 -------------------------------------------------------------------------------- /ansible/roles/common/meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: your name 4 | description: 5 | company: your company (optional) 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | # Some suggested licenses: 10 | # - BSD (default) 11 | # - MIT 12 | # - GPLv2 13 | # - GPLv3 14 | # - Apache 15 | # - CC-BY 16 | license: license (GPLv2, CC-BY, etc) 17 | min_ansible_version: 1.2 18 | # 19 | # Below are all platforms currently available. Just uncomment 20 | # the ones that apply to your role. If you don't see your 21 | # platform on this list, let us know and we'll get it added! 22 | # 23 | #platforms: 24 | #- name: EL 25 | # versions: 26 | # - all 27 | # - 5 28 | # - 6 29 | # - 7 30 | #- name: GenericUNIX 31 | # versions: 32 | # - all 33 | # - any 34 | #- name: OpenBSD 35 | # versions: 36 | # - all 37 | # - 5.6 38 | # - 5.7 39 | # - 5.8 40 | # - 5.9 41 | # - 6.0 42 | #- name: Fedora 43 | # versions: 44 | # - all 45 | # - 16 46 | # - 17 47 | # - 18 48 | # - 19 49 | # - 20 50 | # - 21 51 | # - 22 52 | # - 23 53 | #- name: opensuse 54 | # versions: 55 | # - all 56 | # - 12.1 57 | # - 12.2 58 | # - 12.3 59 | # - 13.1 60 | # - 13.2 61 | #- name: MacOSX 62 | # versions: 63 | # - all 64 | # - 10.10 65 | # - 10.11 66 | # - 10.12 67 | # - 10.7 68 | # - 10.8 69 | # - 10.9 70 | #- name: IOS 71 | # versions: 72 | # - all 73 | # - any 74 | #- name: Solaris 75 | # versions: 76 | # - all 77 | # - 10 78 | # - 11.0 79 | # - 11.1 80 | # - 11.2 81 | # - 11.3 82 | #- name: SmartOS 83 | # versions: 84 | # - all 85 | # - any 86 | #- name: eos 87 | # versions: 88 | # - all 89 | # - Any 90 | #- name: Windows 91 | # versions: 92 | # - all 93 | # - 2012R2 94 | #- name: Amazon 95 | # versions: 96 | # - all 97 | # - 2013.03 98 | # - 2013.09 99 | #- name: GenericBSD 100 | # versions: 101 | # - all 102 | # - any 103 | #- name: Junos 104 | # versions: 105 | # - all 106 | # - any 107 | #- name: FreeBSD 108 | # versions: 109 | # - all 110 | # - 10.0 111 | # - 10.1 112 | # - 10.2 113 | # - 10.3 114 | # - 8.0 115 | # - 8.1 116 | # - 8.2 117 | # - 8.3 118 | # - 8.4 119 | # - 9.0 120 | # - 9.1 121 | # - 9.1 122 | # - 9.2 123 | # - 9.3 124 | #- name: Ubuntu 125 | # versions: 126 | # - all 127 | # - lucid 128 | # - maverick 129 | # - natty 130 | # - oneiric 131 | # - precise 132 | # - quantal 133 | # - raring 134 | # - saucy 135 | # - trusty 136 | # - utopic 137 | # - vivid 138 | # - wily 139 | # - xenial 140 | #- name: SLES 141 | # versions: 142 | # - all 143 | # - 10SP3 144 | # - 10SP4 145 | # - 11 146 | # - 11SP1 147 | # - 11SP2 148 | # - 11SP3 149 | # - 11SP4 150 | # - 12 151 | # - 12SP1 152 | #- name: GenericLinux 153 | # versions: 154 | # - all 155 | # - any 156 | #- name: NXOS 157 | # versions: 158 | # - all 159 | # - any 160 | #- name: Debian 161 | # versions: 162 | # - all 163 | # - etch 164 | # - jessie 165 | # - lenny 166 | # - sid 167 | # - squeeze 168 | # - stretch 169 | # - wheezy 170 | # 171 | # Below are all categories currently available. Just as with 172 | # the platforms above, uncomment those that apply to your role. 173 | # 174 | #categories: 175 | dependencies: [] 176 | # List your role dependencies here, one per line. 177 | # Be sure to remove the '[]' above if you add dependencies 178 | # to this list. 179 | 180 | -------------------------------------------------------------------------------- /ansible/roles/openresty/files/resty-template-1.9/template/microbenchmark.lua: -------------------------------------------------------------------------------- 1 | local template = require "resty.template" 2 | 3 | local ok, new_tab = pcall(require, "table.new") 4 | if not ok then 5 | new_tab = function() return {} end 6 | end 7 | 8 | local function run(iterations) 9 | local gc, total, print, parse, compile, iterations, clock, format = collectgarbage, 0, ngx and ngx.say or print, template.parse, template.compile, iterations or 1000, os.clock, string.format 10 | local view = [[ 11 | ]] 16 | 17 | print(format("Running %d iterations in each test", iterations)) 18 | 19 | gc() 20 | gc() 21 | 22 | local x = clock() 23 | for _ = 1, iterations do 24 | parse(view, true) 25 | end 26 | local z = clock() - x 27 | print(format(" Parsing Time: %.6f", z)) 28 | total = total + z 29 | 30 | gc() 31 | gc() 32 | 33 | x = clock() 34 | for _ = 1, iterations do 35 | compile(view, nil, true) 36 | template.cache = {} 37 | end 38 | z = clock() - x 39 | print(format("Compilation Time: %.6f (template)", z)) 40 | total = total + z 41 | 42 | compile(view, nil, true) 43 | 44 | gc() 45 | gc() 46 | 47 | x = clock() 48 | for _ = 1, iterations do 49 | compile(view, 1, true) 50 | end 51 | z = clock() - x 52 | print(format("Compilation Time: %.6f (template, cached)", z)) 53 | total = total + z 54 | 55 | local context = { "Emma", "James", "Nicholas", "Mary" } 56 | 57 | template.cache = {} 58 | 59 | gc() 60 | gc() 61 | 62 | x = clock() 63 | for _ = 1, iterations do 64 | compile(view, 1, true)(context) 65 | template.cache = {} 66 | end 67 | z = clock() - x 68 | print(format(" Execution Time: %.6f (same template)", z)) 69 | total = total + z 70 | 71 | template.cache = {} 72 | compile(view, 1, true) 73 | 74 | gc() 75 | gc() 76 | 77 | x = clock() 78 | for _ = 1, iterations do 79 | compile(view, 1, true)(context) 80 | end 81 | z = clock() - x 82 | print(format(" Execution Time: %.6f (same template, cached)", z)) 83 | total = total + z 84 | 85 | template.cache = {} 86 | 87 | local views = new_tab(iterations, 0) 88 | for i = 1, iterations do 89 | views[i] = "

Iteration " .. i .. "

\n" .. view 90 | end 91 | 92 | gc() 93 | gc() 94 | 95 | x = clock() 96 | for i = 1, iterations do 97 | compile(views[i], i, true)(context) 98 | end 99 | z = clock() - x 100 | print(format(" Execution Time: %.6f (different template)", z)) 101 | total = total + z 102 | 103 | gc() 104 | gc() 105 | 106 | x = clock() 107 | for i = 1, iterations do 108 | compile(views[i], i, true)(context) 109 | end 110 | z = clock() - x 111 | print(format(" Execution Time: %.6f (different template, cached)", z)) 112 | total = total + z 113 | 114 | local contexts = new_tab(iterations, 0) 115 | 116 | for i = 1, iterations do 117 | contexts[i] = { "Emma", "James", "Nicholas", "Mary" } 118 | end 119 | 120 | template.cache = {} 121 | 122 | gc() 123 | gc() 124 | 125 | x = clock() 126 | for i = 1, iterations do 127 | compile(views[i], i, true)(contexts[i]) 128 | end 129 | z = clock() - x 130 | print(format(" Execution Time: %.6f (different template, different context)", z)) 131 | total = total + z 132 | 133 | gc() 134 | gc() 135 | 136 | x = clock() 137 | for i = 1, iterations do 138 | compile(views[i], i, true)(contexts[i]) 139 | end 140 | z = clock() - x 141 | print(format(" Execution Time: %.6f (different template, different context, cached)", z)) 142 | total = total + z 143 | print(format(" Total Time: %.6f", total)) 144 | end 145 | 146 | return { 147 | run = run 148 | } -------------------------------------------------------------------------------- /ansible/roles/jdk8/meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: your name 4 | description: 5 | company: your company (optional) 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | # Some suggested licenses: 10 | # - BSD (default) 11 | # - MIT 12 | # - GPLv2 13 | # - GPLv3 14 | # - Apache 15 | # - CC-BY 16 | license: license (GPLv2, CC-BY, etc) 17 | min_ansible_version: 1.2 18 | # 19 | # Below are all platforms currently available. Just uncomment 20 | # the ones that apply to your role. If you don't see your 21 | # platform on this list, let us know and we'll get it added! 22 | # 23 | #platforms: 24 | #- name: EL 25 | # versions: 26 | # - all 27 | # - 5 28 | # - 6 29 | # - 7 30 | #- name: GenericUNIX 31 | # versions: 32 | # - all 33 | # - any 34 | #- name: OpenBSD 35 | # versions: 36 | # - all 37 | # - 5.6 38 | # - 5.7 39 | # - 5.8 40 | # - 5.9 41 | # - 6.0 42 | #- name: Fedora 43 | # versions: 44 | # - all 45 | # - 16 46 | # - 17 47 | # - 18 48 | # - 19 49 | # - 20 50 | # - 21 51 | # - 22 52 | # - 23 53 | #- name: opensuse 54 | # versions: 55 | # - all 56 | # - 12.1 57 | # - 12.2 58 | # - 12.3 59 | # - 13.1 60 | # - 13.2 61 | #- name: MacOSX 62 | # versions: 63 | # - all 64 | # - 10.10 65 | # - 10.11 66 | # - 10.12 67 | # - 10.7 68 | # - 10.8 69 | # - 10.9 70 | #- name: IOS 71 | # versions: 72 | # - all 73 | # - any 74 | #- name: Solaris 75 | # versions: 76 | # - all 77 | # - 10 78 | # - 11.0 79 | # - 11.1 80 | # - 11.2 81 | # - 11.3 82 | #- name: SmartOS 83 | # versions: 84 | # - all 85 | # - any 86 | #- name: eos 87 | # versions: 88 | # - all 89 | # - Any 90 | #- name: Windows 91 | # versions: 92 | # - all 93 | # - 2012R2 94 | #- name: Amazon 95 | # versions: 96 | # - all 97 | # - 2013.03 98 | # - 2013.09 99 | #- name: GenericBSD 100 | # versions: 101 | # - all 102 | # - any 103 | #- name: Junos 104 | # versions: 105 | # - all 106 | # - any 107 | #- name: FreeBSD 108 | # versions: 109 | # - all 110 | # - 10.0 111 | # - 10.1 112 | # - 10.2 113 | # - 10.3 114 | # - 8.0 115 | # - 8.1 116 | # - 8.2 117 | # - 8.3 118 | # - 8.4 119 | # - 9.0 120 | # - 9.1 121 | # - 9.1 122 | # - 9.2 123 | # - 9.3 124 | #- name: Ubuntu 125 | # versions: 126 | # - all 127 | # - lucid 128 | # - maverick 129 | # - natty 130 | # - oneiric 131 | # - precise 132 | # - quantal 133 | # - raring 134 | # - saucy 135 | # - trusty 136 | # - utopic 137 | # - vivid 138 | # - wily 139 | # - xenial 140 | #- name: SLES 141 | # versions: 142 | # - all 143 | # - 10SP3 144 | # - 10SP4 145 | # - 11 146 | # - 11SP1 147 | # - 11SP2 148 | # - 11SP3 149 | # - 11SP4 150 | # - 12 151 | # - 12SP1 152 | #- name: GenericLinux 153 | # versions: 154 | # - all 155 | # - any 156 | #- name: NXOS 157 | # versions: 158 | # - all 159 | # - any 160 | #- name: Debian 161 | # versions: 162 | # - all 163 | # - etch 164 | # - jessie 165 | # - lenny 166 | # - sid 167 | # - squeeze 168 | # - stretch 169 | # - wheezy 170 | # 171 | # Below are all categories currently available. Just as with 172 | # the platforms above, uncomment those that apply to your role. 173 | # 174 | #categories: 175 | #- cloud 176 | #- cloud:ec2 177 | #- cloud:gce 178 | #- cloud:rax 179 | #- clustering 180 | #- database 181 | #- database:nosql 182 | #- database:sql 183 | #- development 184 | #- monitoring 185 | #- networking 186 | #- packaging 187 | #- system 188 | #- web 189 | dependencies: [] 190 | # List your role dependencies here, one per line. 191 | # Be sure to remove the '[]' above if you add dependencies 192 | # to this list. 193 | 194 | -------------------------------------------------------------------------------- /ansible/roles/redis/meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: your name 4 | description: 5 | company: your company (optional) 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | # Some suggested licenses: 10 | # - BSD (default) 11 | # - MIT 12 | # - GPLv2 13 | # - GPLv3 14 | # - Apache 15 | # - CC-BY 16 | license: license (GPLv2, CC-BY, etc) 17 | min_ansible_version: 1.2 18 | # 19 | # Below are all platforms currently available. Just uncomment 20 | # the ones that apply to your role. If you don't see your 21 | # platform on this list, let us know and we'll get it added! 22 | # 23 | #platforms: 24 | #- name: EL 25 | # versions: 26 | # - all 27 | # - 5 28 | # - 6 29 | # - 7 30 | #- name: GenericUNIX 31 | # versions: 32 | # - all 33 | # - any 34 | #- name: OpenBSD 35 | # versions: 36 | # - all 37 | # - 5.6 38 | # - 5.7 39 | # - 5.8 40 | # - 5.9 41 | # - 6.0 42 | #- name: Fedora 43 | # versions: 44 | # - all 45 | # - 16 46 | # - 17 47 | # - 18 48 | # - 19 49 | # - 20 50 | # - 21 51 | # - 22 52 | # - 23 53 | #- name: opensuse 54 | # versions: 55 | # - all 56 | # - 12.1 57 | # - 12.2 58 | # - 12.3 59 | # - 13.1 60 | # - 13.2 61 | #- name: MacOSX 62 | # versions: 63 | # - all 64 | # - 10.10 65 | # - 10.11 66 | # - 10.12 67 | # - 10.7 68 | # - 10.8 69 | # - 10.9 70 | #- name: IOS 71 | # versions: 72 | # - all 73 | # - any 74 | #- name: Solaris 75 | # versions: 76 | # - all 77 | # - 10 78 | # - 11.0 79 | # - 11.1 80 | # - 11.2 81 | # - 11.3 82 | #- name: SmartOS 83 | # versions: 84 | # - all 85 | # - any 86 | #- name: eos 87 | # versions: 88 | # - all 89 | # - Any 90 | #- name: Windows 91 | # versions: 92 | # - all 93 | # - 2012R2 94 | #- name: Amazon 95 | # versions: 96 | # - all 97 | # - 2013.03 98 | # - 2013.09 99 | #- name: GenericBSD 100 | # versions: 101 | # - all 102 | # - any 103 | #- name: Junos 104 | # versions: 105 | # - all 106 | # - any 107 | #- name: FreeBSD 108 | # versions: 109 | # - all 110 | # - 10.0 111 | # - 10.1 112 | # - 10.2 113 | # - 10.3 114 | # - 8.0 115 | # - 8.1 116 | # - 8.2 117 | # - 8.3 118 | # - 8.4 119 | # - 9.0 120 | # - 9.1 121 | # - 9.1 122 | # - 9.2 123 | # - 9.3 124 | #- name: Ubuntu 125 | # versions: 126 | # - all 127 | # - lucid 128 | # - maverick 129 | # - natty 130 | # - oneiric 131 | # - precise 132 | # - quantal 133 | # - raring 134 | # - saucy 135 | # - trusty 136 | # - utopic 137 | # - vivid 138 | # - wily 139 | # - xenial 140 | #- name: SLES 141 | # versions: 142 | # - all 143 | # - 10SP3 144 | # - 10SP4 145 | # - 11 146 | # - 11SP1 147 | # - 11SP2 148 | # - 11SP3 149 | # - 11SP4 150 | # - 12 151 | # - 12SP1 152 | #- name: GenericLinux 153 | # versions: 154 | # - all 155 | # - any 156 | #- name: NXOS 157 | # versions: 158 | # - all 159 | # - any 160 | #- name: Debian 161 | # versions: 162 | # - all 163 | # - etch 164 | # - jessie 165 | # - lenny 166 | # - sid 167 | # - squeeze 168 | # - stretch 169 | # - wheezy 170 | # 171 | # Below are all categories currently available. Just as with 172 | # the platforms above, uncomment those that apply to your role. 173 | # 174 | #categories: 175 | #- cloud 176 | #- cloud:ec2 177 | #- cloud:gce 178 | #- cloud:rax 179 | #- clustering 180 | #- database 181 | #- database:nosql 182 | #- database:sql 183 | #- development 184 | #- monitoring 185 | #- networking 186 | #- packaging 187 | #- system 188 | #- web 189 | dependencies: [] 190 | # List your role dependencies here, one per line. 191 | # Be sure to remove the '[]' above if you add dependencies 192 | # to this list. 193 | 194 | -------------------------------------------------------------------------------- /ansible/roles/openresty/meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: your name 4 | description: 5 | company: your company (optional) 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | # Some suggested licenses: 10 | # - BSD (default) 11 | # - MIT 12 | # - GPLv2 13 | # - GPLv3 14 | # - Apache 15 | # - CC-BY 16 | license: license (GPLv2, CC-BY, etc) 17 | min_ansible_version: 1.2 18 | # 19 | # Below are all platforms currently available. Just uncomment 20 | # the ones that apply to your role. If you don't see your 21 | # platform on this list, let us know and we'll get it added! 22 | # 23 | #platforms: 24 | #- name: EL 25 | # versions: 26 | # - all 27 | # - 5 28 | # - 6 29 | # - 7 30 | #- name: GenericUNIX 31 | # versions: 32 | # - all 33 | # - any 34 | #- name: OpenBSD 35 | # versions: 36 | # - all 37 | # - 5.6 38 | # - 5.7 39 | # - 5.8 40 | # - 5.9 41 | # - 6.0 42 | #- name: Fedora 43 | # versions: 44 | # - all 45 | # - 16 46 | # - 17 47 | # - 18 48 | # - 19 49 | # - 20 50 | # - 21 51 | # - 22 52 | # - 23 53 | #- name: opensuse 54 | # versions: 55 | # - all 56 | # - 12.1 57 | # - 12.2 58 | # - 12.3 59 | # - 13.1 60 | # - 13.2 61 | #- name: MacOSX 62 | # versions: 63 | # - all 64 | # - 10.10 65 | # - 10.11 66 | # - 10.12 67 | # - 10.7 68 | # - 10.8 69 | # - 10.9 70 | #- name: IOS 71 | # versions: 72 | # - all 73 | # - any 74 | #- name: Solaris 75 | # versions: 76 | # - all 77 | # - 10 78 | # - 11.0 79 | # - 11.1 80 | # - 11.2 81 | # - 11.3 82 | #- name: SmartOS 83 | # versions: 84 | # - all 85 | # - any 86 | #- name: eos 87 | # versions: 88 | # - all 89 | # - Any 90 | #- name: Windows 91 | # versions: 92 | # - all 93 | # - 2012R2 94 | #- name: Amazon 95 | # versions: 96 | # - all 97 | # - 2013.03 98 | # - 2013.09 99 | #- name: GenericBSD 100 | # versions: 101 | # - all 102 | # - any 103 | #- name: Junos 104 | # versions: 105 | # - all 106 | # - any 107 | #- name: FreeBSD 108 | # versions: 109 | # - all 110 | # - 10.0 111 | # - 10.1 112 | # - 10.2 113 | # - 10.3 114 | # - 8.0 115 | # - 8.1 116 | # - 8.2 117 | # - 8.3 118 | # - 8.4 119 | # - 9.0 120 | # - 9.1 121 | # - 9.1 122 | # - 9.2 123 | # - 9.3 124 | #- name: Ubuntu 125 | # versions: 126 | # - all 127 | # - lucid 128 | # - maverick 129 | # - natty 130 | # - oneiric 131 | # - precise 132 | # - quantal 133 | # - raring 134 | # - saucy 135 | # - trusty 136 | # - utopic 137 | # - vivid 138 | # - wily 139 | # - xenial 140 | #- name: SLES 141 | # versions: 142 | # - all 143 | # - 10SP3 144 | # - 10SP4 145 | # - 11 146 | # - 11SP1 147 | # - 11SP2 148 | # - 11SP3 149 | # - 11SP4 150 | # - 12 151 | # - 12SP1 152 | #- name: GenericLinux 153 | # versions: 154 | # - all 155 | # - any 156 | #- name: NXOS 157 | # versions: 158 | # - all 159 | # - any 160 | #- name: Debian 161 | # versions: 162 | # - all 163 | # - etch 164 | # - jessie 165 | # - lenny 166 | # - sid 167 | # - squeeze 168 | # - stretch 169 | # - wheezy 170 | # 171 | # Below are all categories currently available. Just as with 172 | # the platforms above, uncomment those that apply to your role. 173 | # 174 | #categories: 175 | #- cloud 176 | #- cloud:ec2 177 | #- cloud:gce 178 | #- cloud:rax 179 | #- clustering 180 | #- database 181 | #- database:nosql 182 | #- database:sql 183 | #- development 184 | #- monitoring 185 | #- networking 186 | #- packaging 187 | #- system 188 | #- web 189 | dependencies: [] 190 | # List your role dependencies here, one per line. 191 | # Be sure to remove the '[]' above if you add dependencies 192 | # to this list. 193 | 194 | -------------------------------------------------------------------------------- /ansible/roles/redis/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: create redis_user 3 | group: name={{ redis_group }} state=present 4 | when: redis_group is defined 5 | 6 | - name: create user 7 | user: name={{ redis_user }} createhome=yes groups={{ redis_group }} append=yes 8 | when: redis_user is defined 9 | 10 | - stat: path={{ redis_dir }} 11 | register: redis 12 | 13 | - name: install dependencies 14 | apt: 15 | pkg: "{{ item }}" 16 | update_cache: yes 17 | cache_valid_time: 186400 18 | state: present 19 | with_items: 20 | - gcc 21 | - make 22 | - libc6-dev 23 | 24 | - name: download redis {{redis_download_url}} 25 | get_url: url="{{redis_download_url}}" dest="/usr/local/src/" owner={{ redis_user }} group={{ redis_group }} 26 | when: redis.stat.exists == False 27 | tags: 28 | - install 29 | 30 | 31 | - name: extract redis tarball 32 | unarchive: 33 | src: /usr/local/src/redis-{{ redis_version }}.tar.gz 34 | dest: /usr/local/src 35 | creates: /usr/local/src/redis-{{ redis_version }}/Makefile 36 | copy: no 37 | when: redis.stat.exists == False 38 | 39 | 40 | 41 | - name: compile redis 42 | command: make -j{{ ansible_processor_cores + 1 }} 43 | args: 44 | chdir: /usr/local/src/redis-{{ redis_version }} 45 | creates: /usr/local/src/redis-{{ redis_version }}/src/redis-server 46 | when: redis.stat.exists == False 47 | 48 | 49 | 50 | - name: create redis install directory 51 | file: 52 | path: "{{ redis_install_dir }}" 53 | state: directory 54 | when: redis.stat.exists == False 55 | 56 | 57 | - name: add redis user 58 | user: 59 | name: "{{ redis_user }}" 60 | comment: "Redis" 61 | home: "{{ redis_install_dir }}" 62 | shell: /bin/false 63 | system: yes 64 | 65 | 66 | - name: create /var/run/redis 67 | file: 68 | path: /var/run/redis 69 | state: directory 70 | owner: "{{ redis_user }}" 71 | when: redis.stat.exists == False 72 | 73 | 74 | - name: install redis 75 | command: make PREFIX={{ redis_install_dir }} install 76 | args: 77 | chdir: /usr/local/src/redis-{{ redis_version }} 78 | creates: "{{ redis_install_dir }}/bin/redis-server" 79 | when: redis.stat.exists == False 80 | 81 | 82 | - name: create redis working directory 83 | file: 84 | path: "{{ redis_dir }}" 85 | state: directory 86 | when: redis.stat.exists == False 87 | 88 | 89 | - name: create redis working directory 90 | file: 91 | path: "{{ redis_dir }}" 92 | state: directory 93 | recurse: yes 94 | owner: "{{ redis_user }}" 95 | when: redis.stat.exists == False 96 | 97 | - name: create log directory if it does not exist 98 | file: 99 | state: directory 100 | path: "/var/log/redis" 101 | owner: "{{ redis_user }}" 102 | group: "{{ redis_group }}" 103 | # when: 104 | # - redis_logfile != '""' 105 | # - not logdir.stat.exists 106 | 107 | 108 | - name: check if pid directory exists 109 | stat: 110 | path: "{{ redis_pidfile|dirname }}" 111 | register: piddir 112 | changed_when: false 113 | when: redis_pidfile != '""' 114 | 115 | - name: create pid directory if it does not exist 116 | file: 117 | state: directory 118 | path: "{{ redis_pidfile|dirname }}" 119 | owner: "{{ redis_user }}" 120 | group: "{{ redis_group }}" 121 | when: 122 | - redis_pidfile != '""' 123 | - not piddir.stat.exists 124 | 125 | 126 | - file: state=directory path="/etc/redis" owner="{{ redis_user }}" group="{{ redis_group }}" 127 | 128 | - name: create redis config file 129 | template: 130 | src: redis.conf.j2 131 | dest: /etc/redis/redis.conf 132 | owner: "{{ redis_user }}" 133 | force: true 134 | notify: restart redis 135 | 136 | 137 | - name: copy systemd conf 138 | template: 139 | dest: /etc/systemd/system/{{ redis_service_name }} 140 | src: redis.server.service 141 | mode: 0700 142 | force: true 143 | notify: restart redis 144 | 145 | 146 | - name: ensure redis is running 147 | service: 148 | name: "{{ redis_service_name }}" 149 | state: restarted 150 | # enable: true 151 | when: redis_as_service 152 | -------------------------------------------------------------------------------- /ansible/roles/front-app/meta/main.yml: -------------------------------------------------------------------------------- 1 | galaxy_info: 2 | author: your name 3 | description: your description 4 | company: your company (optional) 5 | 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | 10 | # Some suggested licenses: 11 | # - BSD (default) 12 | # - MIT 13 | # - GPLv2 14 | # - GPLv3 15 | # - Apache 16 | # - CC-BY 17 | license: license (GPLv2, CC-BY, etc) 18 | 19 | min_ansible_version: 1.2 20 | 21 | # Optionally specify the branch Galaxy will use when accessing the GitHub 22 | # repo for this role. During role install, if no tags are available, 23 | # Galaxy will use this branch. During import Galaxy will access files on 24 | # this branch. If travis integration is cofigured, only notification for this 25 | # branch will be accepted. Otherwise, in all cases, the repo's default branch 26 | # (usually master) will be used. 27 | #github_branch: 28 | 29 | # 30 | # Below are all platforms currently available. Just uncomment 31 | # the ones that apply to your role. If you don't see your 32 | # platform on this list, let us know and we'll get it added! 33 | # 34 | #platforms: 35 | #- name: OpenBSD 36 | # versions: 37 | # - all 38 | # - 5.6 39 | # - 5.7 40 | # - 5.8 41 | # - 5.9 42 | # - 6.0 43 | #- name: Fedora 44 | # versions: 45 | # - all 46 | # - 16 47 | # - 17 48 | # - 18 49 | # - 19 50 | # - 20 51 | # - 21 52 | # - 22 53 | # - 23 54 | # - 24 55 | #- name: DellOS 56 | # versions: 57 | # - all 58 | # - any 59 | #- name: MacOSX 60 | # versions: 61 | # - all 62 | # - 10.10 63 | # - 10.11 64 | # - 10.12 65 | # - 10.7 66 | # - 10.8 67 | # - 10.9 68 | #- name: Junos 69 | # versions: 70 | # - all 71 | # - any 72 | #- name: GenericBSD 73 | # versions: 74 | # - all 75 | # - any 76 | #- name: Void Linux 77 | # versions: 78 | # - all 79 | # - any 80 | #- name: GenericLinux 81 | # versions: 82 | # - all 83 | # - any 84 | #- name: NXOS 85 | # versions: 86 | # - all 87 | # - any 88 | #- name: IOS 89 | # versions: 90 | # - all 91 | # - any 92 | #- name: Amazon 93 | # versions: 94 | # - all 95 | # - 2013.03 96 | # - 2013.09 97 | # - 2016.03 98 | #- name: ArchLinux 99 | # versions: 100 | # - all 101 | # - any 102 | #- name: FreeBSD 103 | # versions: 104 | # - all 105 | # - 10.0 106 | # - 10.1 107 | # - 10.2 108 | # - 10.3 109 | # - 8.0 110 | # - 8.1 111 | # - 8.2 112 | # - 8.3 113 | # - 8.4 114 | # - 9.0 115 | # - 9.1 116 | # - 9.1 117 | # - 9.2 118 | # - 9.3 119 | #- name: Ubuntu 120 | # versions: 121 | # - all 122 | # - lucid 123 | # - maverick 124 | # - natty 125 | # - oneiric 126 | # - precise 127 | # - quantal 128 | # - raring 129 | # - saucy 130 | # - trusty 131 | # - utopic 132 | # - vivid 133 | # - wily 134 | # - xenial 135 | #- name: Debian 136 | # versions: 137 | # - all 138 | # - etch 139 | # - jessie 140 | # - lenny 141 | # - sid 142 | # - squeeze 143 | # - stretch 144 | # - wheezy 145 | #- name: EL 146 | # versions: 147 | # - all 148 | # - 5 149 | # - 6 150 | # - 7 151 | #- name: Windows 152 | # versions: 153 | # - all 154 | # - 2012R2 155 | #- name: SmartOS 156 | # versions: 157 | # - all 158 | # - any 159 | #- name: opensuse 160 | # versions: 161 | # - all 162 | # - 12.1 163 | # - 12.2 164 | # - 12.3 165 | # - 13.1 166 | # - 13.2 167 | #- name: SLES 168 | # versions: 169 | # - all 170 | # - 10SP3 171 | # - 10SP4 172 | # - 11 173 | # - 11SP1 174 | # - 11SP2 175 | # - 11SP3 176 | # - 11SP4 177 | # - 12 178 | # - 12SP1 179 | #- name: GenericUNIX 180 | # versions: 181 | # - all 182 | # - any 183 | #- name: Solaris 184 | # versions: 185 | # - all 186 | # - 10 187 | # - 11.0 188 | # - 11.1 189 | # - 11.2 190 | # - 11.3 191 | #- name: eos 192 | # versions: 193 | # - all 194 | # - Any 195 | 196 | galaxy_tags: [] 197 | # List tags for your role here, one per line. A tag is 198 | # a keyword that describes and categorizes the role. 199 | # Users find roles by searching for tags. Be sure to 200 | # remove the '[]' above if you add tags to this list. 201 | # 202 | # NOTE: A tag is limited to a single word comprised of 203 | # alphanumeric characters. Maximum 20 tags per role. 204 | 205 | dependencies: [] 206 | # List your role dependencies here, one per line. 207 | # Be sure to remove the '[]' above if you add dependencies 208 | # to this list. -------------------------------------------------------------------------------- /autocomplete-worker/src/main/java/codes/showme/autocomplete/InitWorker.java: -------------------------------------------------------------------------------- 1 | package codes.showme.autocomplete; 2 | 3 | import codes.showme.autocomplete.common.Configuration; 4 | import codes.showme.autocomplete.common.Pair; 5 | import codes.showme.autocomplete.common.PropertiesConfig; 6 | import redis.clients.jedis.Jedis; 7 | import redis.clients.jedis.JedisPool; 8 | import redis.clients.jedis.JedisPoolConfig; 9 | import redis.clients.jedis.Transaction; 10 | 11 | import java.io.*; 12 | import java.util.ArrayList; 13 | import java.util.Arrays; 14 | import java.util.List; 15 | import java.util.Objects; 16 | import java.util.function.Consumer; 17 | import java.util.stream.Collectors; 18 | 19 | /** 20 | * Created by jack on 12/28/16. 21 | */ 22 | public class InitWorker { 23 | 24 | public final static Configuration configuration = new PropertiesConfig(); 25 | 26 | public static void main(String[] args) throws IOException { 27 | 28 | if (args.length < 1) { 29 | throw new IllegalArgumentException("arg: p is required. arg p is the path of province.txt"); 30 | } 31 | 32 | String pathname = args[0]; 33 | File file = new File(pathname); 34 | if (!file.exists()) { 35 | throw new IllegalArgumentException(pathname + " is not found"); 36 | } 37 | 38 | JedisPool jedisPool = createJedisPool(); 39 | 40 | if (args.length > 1) { 41 | String flushallArg = args[1]; 42 | if (flushallArg.equals("flushall")) { 43 | Jedis jedis = jedisPool.getResource(); 44 | jedis.select(0); 45 | jedis.flushAll(); 46 | } 47 | } 48 | InitWorker initWorker = new InitWorker(); 49 | 50 | initWorker.iterateLines(file, 2, places -> { 51 | Jedis jedis = jedisPool.getResource(); 52 | jedis.select(0); 53 | Transaction multi = jedis.multi(); 54 | try { 55 | List> pairList = initWorker.convertLineToRedisRecord(places); 56 | for (Pair pair : pairList) { 57 | multi.zincrby(pair.getLeft(), 0.0, pair.getRight()); 58 | } 59 | multi.exec(); 60 | 61 | } catch (Exception e) { 62 | System.err.println(e.getMessage()); 63 | } finally { 64 | try { 65 | multi.close(); 66 | } catch (IOException e) { 67 | System.err.println(e.getMessage()); 68 | } 69 | jedis.close(); 70 | } 71 | }); 72 | } 73 | 74 | public void iterateLines(File file, int count, Consumer> consumer) throws IOException { 75 | InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file), "UTF-8"); 76 | BufferedReader reader = new BufferedReader(inputReader); 77 | List list = new ArrayList(); 78 | String line = reader.readLine(); 79 | while (line != null) { 80 | list.add(line); 81 | if (list.size() >= count) { 82 | consumer.accept(list); 83 | list.clear(); 84 | } 85 | line = reader.readLine(); 86 | } 87 | if (!list.isEmpty()) { 88 | consumer.accept(list); 89 | list = null; 90 | } 91 | 92 | closeQuietly(reader); 93 | closeQuietly(inputReader); 94 | } 95 | 96 | 97 | public List> convertLineToRedisRecord(List lines) { 98 | List> result = new ArrayList<>(); 99 | 100 | lines.stream().filter(Objects::nonNull) 101 | .map(line -> Arrays.asList(line.split("\\s"))) 102 | .filter(strings -> strings.size() > 1) 103 | .filter(strings -> !strings.get(0).trim().equals("")) 104 | .forEach((List strList) -> { 105 | String chinesePlaceName = strList.get(0); 106 | List pinyin = strList.subList(1, strList.size()); 107 | String pinyinTogether = getPinyinTogether(pinyin); 108 | 109 | result.add(new Pair<>(getEachFirstAlpha(pinyin), chinesePlaceName)); 110 | 111 | List> pairs = getSequences(pinyinTogether) 112 | .stream() 113 | .map(s -> new Pair<>(s, chinesePlaceName)) 114 | .collect(Collectors.toList()); 115 | result.addAll(pairs); 116 | 117 | List> chinesePairs = getChineseSequences(chinesePlaceName) 118 | .stream() 119 | .map(s -> new Pair<>(s, chinesePlaceName)) 120 | .collect(Collectors.toList()); 121 | result.addAll(chinesePairs); 122 | 123 | }); 124 | return result; 125 | } 126 | 127 | /** 128 | * shi shen me -> ssm 129 | * 130 | * @param pinyins 131 | * @return 132 | */ 133 | private String getEachFirstAlpha(List pinyins) { 134 | return pinyins.stream().map(a -> a.charAt(0) + "").reduce("", (a, b) -> a + b); 135 | } 136 | 137 | /** 138 | * shishenme - { 139 | * s 140 | * sh 141 | * shi 142 | * shis 143 | * shish 144 | * shishe 145 | * .... 146 | * } 147 | * 148 | * @param pinyinTogether 149 | * @return 150 | */ 151 | private List getSequences(String pinyinTogether) { 152 | List result = new ArrayList<>(); 153 | char[] pinyinChars = pinyinTogether.toCharArray(); 154 | for (int index = 0; index < pinyinChars.length; index++) { 155 | StringBuilder stringBuilder = new StringBuilder(); 156 | for (int innerIndex = 0; innerIndex <= index; innerIndex++) { 157 | stringBuilder.append(pinyinChars[innerIndex]); 158 | } 159 | result.add(stringBuilder.toString()); 160 | } 161 | return result; 162 | } 163 | 164 | /** 165 | *辽宁省大连市 -> { 166 | * 辽 167 | * 辽宁 168 | * 辽宁省 169 | * 辽宁省大 170 | * 辽宁省大连 171 | * 辽宁省大连市 172 | *} 173 | * @param chinesePlaceName 174 | * @return 175 | */ 176 | private List getChineseSequences(String chinesePlaceName) { 177 | List result = new ArrayList<>(); 178 | for (int i = 0; i < chinesePlaceName.length(); i++) { 179 | result.add(chinesePlaceName.substring(0, i+1)); 180 | } 181 | return result; 182 | } 183 | 184 | /** 185 | * shi shen me -> shishenme 186 | * 187 | * @param pinyins 188 | * @return 189 | */ 190 | private String getPinyinTogether(List pinyins) { 191 | return pinyins.stream().reduce("", (a, b) -> a + b); 192 | } 193 | 194 | private static JedisPool createJedisPool() { 195 | JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 196 | String ip = configuration.getRedisIP(); 197 | int port = configuration.getRedisPort(); 198 | int timeout = 2000; 199 | jedisPoolConfig.setMaxTotal(1024); 200 | jedisPoolConfig.setMaxIdle(100); 201 | jedisPoolConfig.setMaxWaitMillis(100); 202 | jedisPoolConfig.setTestOnBorrow(false); 203 | jedisPoolConfig.setTestOnReturn(true); 204 | // 初始化JedisPool 205 | return new JedisPool(jedisPoolConfig, ip, port, timeout); 206 | } 207 | 208 | 209 | private void closeQuietly(Closeable closeable) { 210 | try { 211 | if (closeable != null) { 212 | closeable.close(); 213 | } 214 | } catch (final IOException ioe) { 215 | // ignore 216 | } 217 | } 218 | 219 | } 220 | -------------------------------------------------------------------------------- /ansible/roles/openresty/files/lua-resty-redis-0.25/redis.lua: -------------------------------------------------------------------------------- 1 | -- Copyright (C) Yichun Zhang (agentzh) 2 | 3 | 4 | local sub = string.sub 5 | local byte = string.byte 6 | local tcp = ngx.socket.tcp 7 | local null = ngx.null 8 | local type = type 9 | local pairs = pairs 10 | local unpack = unpack 11 | local setmetatable = setmetatable 12 | local tonumber = tonumber 13 | local tostring = tostring 14 | local rawget = rawget 15 | --local error = error 16 | 17 | 18 | local ok, new_tab = pcall(require, "table.new") 19 | if not ok or type(new_tab) ~= "function" then 20 | new_tab = function (narr, nrec) return {} end 21 | end 22 | 23 | 24 | local _M = new_tab(0, 54) 25 | 26 | _M._VERSION = '0.25' 27 | 28 | 29 | local common_cmds = { 30 | "get", "set", "mget", "mset", 31 | "del", "incr", "decr", -- Strings 32 | "llen", "lindex", "lpop", "lpush", 33 | "lrange", "linsert", -- Lists 34 | "hexists", "hget", "hset", "hmget", 35 | --[[ "hmset", ]] "hdel", -- Hashes 36 | "smembers", "sismember", "sadd", "srem", 37 | "sdiff", "sinter", "sunion", -- Sets 38 | "zrange", "zrangebyscore", "zrank", "zadd", 39 | "zrem", "zincrby", -- Sorted Sets 40 | "auth", "eval", "expire", "script", 41 | "sort" -- Others 42 | } 43 | 44 | 45 | local sub_commands = { 46 | "subscribe", "psubscribe" 47 | } 48 | 49 | 50 | local unsub_commands = { 51 | "unsubscribe", "punsubscribe" 52 | } 53 | 54 | 55 | local mt = { __index = _M } 56 | 57 | 58 | function _M.new(self) 59 | local sock, err = tcp() 60 | if not sock then 61 | return nil, err 62 | end 63 | return setmetatable({ _sock = sock, _subscribed = false }, mt) 64 | end 65 | 66 | 67 | function _M.set_timeout(self, timeout) 68 | local sock = rawget(self, "_sock") 69 | if not sock then 70 | return nil, "not initialized" 71 | end 72 | 73 | return sock:settimeout(timeout) 74 | end 75 | 76 | 77 | function _M.connect(self, ...) 78 | local sock = rawget(self, "_sock") 79 | if not sock then 80 | return nil, "not initialized" 81 | end 82 | 83 | self._subscribed = false 84 | 85 | return sock:connect(...) 86 | end 87 | 88 | 89 | function _M.set_keepalive(self, ...) 90 | local sock = rawget(self, "_sock") 91 | if not sock then 92 | return nil, "not initialized" 93 | end 94 | 95 | if rawget(self, "_subscribed") then 96 | return nil, "subscribed state" 97 | end 98 | 99 | return sock:setkeepalive(...) 100 | end 101 | 102 | 103 | function _M.get_reused_times(self) 104 | local sock = rawget(self, "_sock") 105 | if not sock then 106 | return nil, "not initialized" 107 | end 108 | 109 | return sock:getreusedtimes() 110 | end 111 | 112 | 113 | local function close(self) 114 | local sock = rawget(self, "_sock") 115 | if not sock then 116 | return nil, "not initialized" 117 | end 118 | 119 | return sock:close() 120 | end 121 | _M.close = close 122 | 123 | 124 | local function _read_reply(self, sock) 125 | local line, err = sock:receive() 126 | if not line then 127 | if err == "timeout" and not rawget(self, "_subscribed") then 128 | sock:close() 129 | end 130 | return nil, err 131 | end 132 | 133 | local prefix = byte(line) 134 | 135 | if prefix == 36 then -- char '$' 136 | -- print("bulk reply") 137 | 138 | local size = tonumber(sub(line, 2)) 139 | if size < 0 then 140 | return null 141 | end 142 | 143 | local data, err = sock:receive(size) 144 | if not data then 145 | if err == "timeout" then 146 | sock:close() 147 | end 148 | return nil, err 149 | end 150 | 151 | local dummy, err = sock:receive(2) -- ignore CRLF 152 | if not dummy then 153 | return nil, err 154 | end 155 | 156 | return data 157 | 158 | elseif prefix == 43 then -- char '+' 159 | -- print("status reply") 160 | 161 | return sub(line, 2) 162 | 163 | elseif prefix == 42 then -- char '*' 164 | local n = tonumber(sub(line, 2)) 165 | 166 | -- print("multi-bulk reply: ", n) 167 | if n < 0 then 168 | return null 169 | end 170 | 171 | local vals = new_tab(n, 0) 172 | local nvals = 0 173 | for i = 1, n do 174 | local res, err = _read_reply(self, sock) 175 | if res then 176 | nvals = nvals + 1 177 | vals[nvals] = res 178 | 179 | elseif res == nil then 180 | return nil, err 181 | 182 | else 183 | -- be a valid redis error value 184 | nvals = nvals + 1 185 | vals[nvals] = {false, err} 186 | end 187 | end 188 | 189 | return vals 190 | 191 | elseif prefix == 58 then -- char ':' 192 | -- print("integer reply") 193 | return tonumber(sub(line, 2)) 194 | 195 | elseif prefix == 45 then -- char '-' 196 | -- print("error reply: ", n) 197 | 198 | return false, sub(line, 2) 199 | 200 | else 201 | -- when `line` is an empty string, `prefix` will be equal to nil. 202 | return nil, "unkown prefix: \"" .. tostring(prefix) .. "\"" 203 | end 204 | end 205 | 206 | 207 | local function _gen_req(args) 208 | local nargs = #args 209 | 210 | local req = new_tab(nargs * 5 + 1, 0) 211 | req[1] = "*" .. nargs .. "\r\n" 212 | local nbits = 2 213 | 214 | for i = 1, nargs do 215 | local arg = args[i] 216 | if type(arg) ~= "string" then 217 | arg = tostring(arg) 218 | end 219 | 220 | req[nbits] = "$" 221 | req[nbits + 1] = #arg 222 | req[nbits + 2] = "\r\n" 223 | req[nbits + 3] = arg 224 | req[nbits + 4] = "\r\n" 225 | 226 | nbits = nbits + 5 227 | end 228 | 229 | -- it is much faster to do string concatenation on the C land 230 | -- in real world (large number of strings in the Lua VM) 231 | return req 232 | end 233 | 234 | 235 | local function _do_cmd(self, ...) 236 | local args = {...} 237 | 238 | local sock = rawget(self, "_sock") 239 | if not sock then 240 | return nil, "not initialized" 241 | end 242 | 243 | local req = _gen_req(args) 244 | 245 | local reqs = rawget(self, "_reqs") 246 | if reqs then 247 | reqs[#reqs + 1] = req 248 | return 249 | end 250 | 251 | -- print("request: ", table.concat(req)) 252 | 253 | local bytes, err = sock:send(req) 254 | if not bytes then 255 | return nil, err 256 | end 257 | 258 | return _read_reply(self, sock) 259 | end 260 | 261 | 262 | local function _check_subscribed(self, res) 263 | if type(res) == "table" 264 | and (res[1] == "unsubscribe" or res[1] == "punsubscribe") 265 | and res[3] == 0 266 | then 267 | self._subscribed = false 268 | end 269 | end 270 | 271 | 272 | function _M.read_reply(self) 273 | local sock = rawget(self, "_sock") 274 | if not sock then 275 | return nil, "not initialized" 276 | end 277 | 278 | if not rawget(self, "_subscribed") then 279 | return nil, "not subscribed" 280 | end 281 | 282 | local res, err = _read_reply(self, sock) 283 | _check_subscribed(self, res) 284 | 285 | return res, err 286 | end 287 | 288 | 289 | for i = 1, #common_cmds do 290 | local cmd = common_cmds[i] 291 | 292 | _M[cmd] = 293 | function (self, ...) 294 | return _do_cmd(self, cmd, ...) 295 | end 296 | end 297 | 298 | 299 | for i = 1, #sub_commands do 300 | local cmd = sub_commands[i] 301 | 302 | _M[cmd] = 303 | function (self, ...) 304 | self._subscribed = true 305 | return _do_cmd(self, cmd, ...) 306 | end 307 | end 308 | 309 | 310 | for i = 1, #unsub_commands do 311 | local cmd = unsub_commands[i] 312 | 313 | _M[cmd] = 314 | function (self, ...) 315 | local res, err = _do_cmd(self, cmd, ...) 316 | _check_subscribed(self, res) 317 | return res, err 318 | end 319 | end 320 | 321 | 322 | function _M.hmset(self, hashname, ...) 323 | local args = {...} 324 | if #args == 1 then 325 | local t = args[1] 326 | 327 | local n = 0 328 | for k, v in pairs(t) do 329 | n = n + 2 330 | end 331 | 332 | local array = new_tab(n, 0) 333 | 334 | local i = 0 335 | for k, v in pairs(t) do 336 | array[i + 1] = k 337 | array[i + 2] = v 338 | i = i + 2 339 | end 340 | -- print("key", hashname) 341 | return _do_cmd(self, "hmset", hashname, unpack(array)) 342 | end 343 | 344 | -- backwards compatibility 345 | return _do_cmd(self, "hmset", hashname, ...) 346 | end 347 | 348 | 349 | function _M.init_pipeline(self, n) 350 | self._reqs = new_tab(n or 4, 0) 351 | end 352 | 353 | 354 | function _M.cancel_pipeline(self) 355 | self._reqs = nil 356 | end 357 | 358 | 359 | function _M.commit_pipeline(self) 360 | local reqs = rawget(self, "_reqs") 361 | if not reqs then 362 | return nil, "no pipeline" 363 | end 364 | 365 | self._reqs = nil 366 | 367 | local sock = rawget(self, "_sock") 368 | if not sock then 369 | return nil, "not initialized" 370 | end 371 | 372 | local bytes, err = sock:send(reqs) 373 | if not bytes then 374 | return nil, err 375 | end 376 | 377 | local nvals = 0 378 | local nreqs = #reqs 379 | local vals = new_tab(nreqs, 0) 380 | for i = 1, nreqs do 381 | local res, err = _read_reply(self, sock) 382 | if res then 383 | nvals = nvals + 1 384 | vals[nvals] = res 385 | 386 | elseif res == nil then 387 | if err == "timeout" then 388 | close(self) 389 | end 390 | return nil, err 391 | 392 | else 393 | -- be a valid redis error value 394 | nvals = nvals + 1 395 | vals[nvals] = {false, err} 396 | end 397 | end 398 | 399 | return vals 400 | end 401 | 402 | 403 | function _M.array_to_hash(self, t) 404 | local n = #t 405 | -- print("n = ", n) 406 | local h = new_tab(0, n / 2) 407 | for i = 1, n, 2 do 408 | h[t[i]] = t[i + 1] 409 | end 410 | return h 411 | end 412 | 413 | 414 | -- this method is deperate since we already do lazy method generation. 415 | function _M.add_commands(...) 416 | local cmds = {...} 417 | for i = 1, #cmds do 418 | local cmd = cmds[i] 419 | _M[cmd] = 420 | function (self, ...) 421 | return _do_cmd(self, cmd, ...) 422 | end 423 | end 424 | end 425 | 426 | 427 | setmetatable(_M, {__index = function(self, cmd) 428 | local method = 429 | function (self, ...) 430 | return _do_cmd(self, cmd, ...) 431 | end 432 | 433 | -- cache the lazily generated method in our 434 | -- module table 435 | _M[cmd] = method 436 | return method 437 | end}) 438 | 439 | 440 | return _M 441 | -------------------------------------------------------------------------------- /autocomplete-worker/src/test/java/codes/showme/autocomple/MainTest.java: -------------------------------------------------------------------------------- 1 | package codes.showme.autocomple; 2 | 3 | import codes.showme.autocomplete.InitWorker; 4 | import codes.showme.autocomplete.common.Pair; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | /** 12 | * Created by jack on 12/30/16. 13 | */ 14 | public class MainTest { 15 | 16 | @Test 17 | public void testName() throws Exception { 18 | InitWorker initWorker = new InitWorker(); 19 | List> pairList = initWorker.convertLineToRedisRecord(Arrays.asList( 20 | "阿坝 a ba", 21 | "东城 dong cheng", 22 | "辽宁省大连市经济技术开发区辽河西路号 liao ning sheng da lian shi jing ji ji shu kai fa qu liao he xi lu hao" 23 | )); 24 | 25 | Assert.assertEquals("ab", pairList.get(0).getLeft()); 26 | Assert.assertEquals("阿坝", pairList.get(0).getRight()); 27 | Assert.assertEquals("a", pairList.get(1).getLeft()); 28 | Assert.assertEquals("阿坝", pairList.get(1).getRight()); 29 | Assert.assertEquals("ab", pairList.get(2).getLeft()); 30 | Assert.assertEquals("阿坝", pairList.get(2).getRight()); 31 | Assert.assertEquals("aba", pairList.get(3).getLeft()); 32 | Assert.assertEquals("阿坝", pairList.get(3).getRight()); 33 | Assert.assertEquals("阿", pairList.get(4).getLeft()); 34 | Assert.assertEquals("阿坝", pairList.get(4).getRight()); 35 | Assert.assertEquals("阿坝", pairList.get(5).getLeft()); 36 | Assert.assertEquals("阿坝", pairList.get(5).getRight()); 37 | Assert.assertEquals("dc", pairList.get(6).getLeft()); 38 | Assert.assertEquals("东城", pairList.get(6).getRight()); 39 | Assert.assertEquals("d", pairList.get(7).getLeft()); 40 | Assert.assertEquals("东城", pairList.get(7).getRight()); 41 | Assert.assertEquals("do", pairList.get(8).getLeft()); 42 | Assert.assertEquals("东城", pairList.get(8).getRight()); 43 | Assert.assertEquals("don", pairList.get(9).getLeft()); 44 | Assert.assertEquals("东城", pairList.get(9).getRight()); 45 | Assert.assertEquals("dong", pairList.get(10).getLeft()); 46 | Assert.assertEquals("东城", pairList.get(10).getRight()); 47 | Assert.assertEquals("dongc", pairList.get(11).getLeft()); 48 | Assert.assertEquals("东城", pairList.get(11).getRight()); 49 | Assert.assertEquals("dongch", pairList.get(12).getLeft()); 50 | Assert.assertEquals("东城", pairList.get(12).getRight()); 51 | Assert.assertEquals("dongche", pairList.get(13).getLeft()); 52 | Assert.assertEquals("东城", pairList.get(13).getRight()); 53 | Assert.assertEquals("dongchen", pairList.get(14).getLeft()); 54 | Assert.assertEquals("东城", pairList.get(14).getRight()); 55 | Assert.assertEquals("dongcheng", pairList.get(15).getLeft()); 56 | Assert.assertEquals("东城", pairList.get(15).getRight()); 57 | Assert.assertEquals("东", pairList.get(16).getLeft()); 58 | Assert.assertEquals("东城", pairList.get(16).getRight()); 59 | Assert.assertEquals("东城", pairList.get(17).getLeft()); 60 | Assert.assertEquals("东城", pairList.get(17).getRight()); 61 | Assert.assertEquals("lnsdlsjjjskfqlhxlh", pairList.get(18).getLeft()); 62 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(18).getRight()); 63 | Assert.assertEquals("l", pairList.get(19).getLeft()); 64 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(19).getRight()); 65 | Assert.assertEquals("li", pairList.get(20).getLeft()); 66 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(20).getRight()); 67 | Assert.assertEquals("lia", pairList.get(21).getLeft()); 68 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(21).getRight()); 69 | Assert.assertEquals("liao", pairList.get(22).getLeft()); 70 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(22).getRight()); 71 | Assert.assertEquals("liaon", pairList.get(23).getLeft()); 72 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(23).getRight()); 73 | Assert.assertEquals("liaoni", pairList.get(24).getLeft()); 74 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(24).getRight()); 75 | Assert.assertEquals("liaonin", pairList.get(25).getLeft()); 76 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(25).getRight()); 77 | Assert.assertEquals("liaoning", pairList.get(26).getLeft()); 78 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(26).getRight()); 79 | Assert.assertEquals("liaonings", pairList.get(27).getLeft()); 80 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(27).getRight()); 81 | Assert.assertEquals("liaoningsh", pairList.get(28).getLeft()); 82 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(28).getRight()); 83 | Assert.assertEquals("liaoningshe", pairList.get(29).getLeft()); 84 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(29).getRight()); 85 | Assert.assertEquals("liaoningshen", pairList.get(30).getLeft()); 86 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(30).getRight()); 87 | Assert.assertEquals("liaoningsheng", pairList.get(31).getLeft()); 88 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(31).getRight()); 89 | Assert.assertEquals("liaoningshengd", pairList.get(32).getLeft()); 90 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(32).getRight()); 91 | Assert.assertEquals("liaoningshengda", pairList.get(33).getLeft()); 92 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(33).getRight()); 93 | Assert.assertEquals("liaoningshengdal", pairList.get(34).getLeft()); 94 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(34).getRight()); 95 | Assert.assertEquals("liaoningshengdali", pairList.get(35).getLeft()); 96 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(35).getRight()); 97 | Assert.assertEquals("liaoningshengdalia", pairList.get(36).getLeft()); 98 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(36).getRight()); 99 | Assert.assertEquals("liaoningshengdalian", pairList.get(37).getLeft()); 100 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(37).getRight()); 101 | Assert.assertEquals("liaoningshengdalians", pairList.get(38).getLeft()); 102 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(38).getRight()); 103 | Assert.assertEquals("liaoningshengdaliansh", pairList.get(39).getLeft()); 104 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(39).getRight()); 105 | Assert.assertEquals("liaoningshengdalianshi", pairList.get(40).getLeft()); 106 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(40).getRight()); 107 | Assert.assertEquals("liaoningshengdalianshij", pairList.get(41).getLeft()); 108 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(41).getRight()); 109 | Assert.assertEquals("liaoningshengdalianshiji", pairList.get(42).getLeft()); 110 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(42).getRight()); 111 | Assert.assertEquals("liaoningshengdalianshijin", pairList.get(43).getLeft()); 112 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(43).getRight()); 113 | Assert.assertEquals("liaoningshengdalianshijing", pairList.get(44).getLeft()); 114 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(44).getRight()); 115 | Assert.assertEquals("liaoningshengdalianshijingj", pairList.get(45).getLeft()); 116 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(45).getRight()); 117 | Assert.assertEquals("liaoningshengdalianshijingji", pairList.get(46).getLeft()); 118 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(46).getRight()); 119 | Assert.assertEquals("liaoningshengdalianshijingjij", pairList.get(47).getLeft()); 120 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(47).getRight()); 121 | Assert.assertEquals("liaoningshengdalianshijingjiji", pairList.get(48).getLeft()); 122 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(48).getRight()); 123 | Assert.assertEquals("liaoningshengdalianshijingjijis", pairList.get(49).getLeft()); 124 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(49).getRight()); 125 | Assert.assertEquals("liaoningshengdalianshijingjijish", pairList.get(50).getLeft()); 126 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(50).getRight()); 127 | Assert.assertEquals("liaoningshengdalianshijingjijishu", pairList.get(51).getLeft()); 128 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(51).getRight()); 129 | Assert.assertEquals("liaoningshengdalianshijingjijishuk", pairList.get(52).getLeft()); 130 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(52).getRight()); 131 | Assert.assertEquals("liaoningshengdalianshijingjijishuka", pairList.get(53).getLeft()); 132 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(53).getRight()); 133 | Assert.assertEquals("liaoningshengdalianshijingjijishukai", pairList.get(54).getLeft()); 134 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(54).getRight()); 135 | Assert.assertEquals("liaoningshengdalianshijingjijishukaif", pairList.get(55).getLeft()); 136 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(55).getRight()); 137 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifa", pairList.get(56).getLeft()); 138 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(56).getRight()); 139 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaq", pairList.get(57).getLeft()); 140 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(57).getRight()); 141 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaqu", pairList.get(58).getLeft()); 142 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(58).getRight()); 143 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaqul", pairList.get(59).getLeft()); 144 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(59).getRight()); 145 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquli", pairList.get(60).getLeft()); 146 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(60).getRight()); 147 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaqulia", pairList.get(61).getLeft()); 148 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(61).getRight()); 149 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliao", pairList.get(62).getLeft()); 150 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(62).getRight()); 151 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaoh", pairList.get(63).getLeft()); 152 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(63).getRight()); 153 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohe", pairList.get(64).getLeft()); 154 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(64).getRight()); 155 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohex", pairList.get(65).getLeft()); 156 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(65).getRight()); 157 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohexi", pairList.get(66).getLeft()); 158 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(66).getRight()); 159 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohexil", pairList.get(67).getLeft()); 160 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(67).getRight()); 161 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohexilu", pairList.get(68).getLeft()); 162 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(68).getRight()); 163 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohexiluh", pairList.get(69).getLeft()); 164 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(69).getRight()); 165 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohexiluha", pairList.get(70).getLeft()); 166 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(70).getRight()); 167 | Assert.assertEquals("liaoningshengdalianshijingjijishukaifaquliaohexiluhao", pairList.get(71).getLeft()); 168 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(71).getRight()); 169 | Assert.assertEquals("辽", pairList.get(72).getLeft()); 170 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(72).getRight()); 171 | Assert.assertEquals("辽宁", pairList.get(73).getLeft()); 172 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(73).getRight()); 173 | Assert.assertEquals("辽宁省", pairList.get(74).getLeft()); 174 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(74).getRight()); 175 | Assert.assertEquals("辽宁省大", pairList.get(75).getLeft()); 176 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(75).getRight()); 177 | Assert.assertEquals("辽宁省大连", pairList.get(76).getLeft()); 178 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(76).getRight()); 179 | Assert.assertEquals("辽宁省大连市", pairList.get(77).getLeft()); 180 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(77).getRight()); 181 | Assert.assertEquals("辽宁省大连市经", pairList.get(78).getLeft()); 182 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(78).getRight()); 183 | Assert.assertEquals("辽宁省大连市经济", pairList.get(79).getLeft()); 184 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(79).getRight()); 185 | Assert.assertEquals("辽宁省大连市经济技", pairList.get(80).getLeft()); 186 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(80).getRight()); 187 | Assert.assertEquals("辽宁省大连市经济技术", pairList.get(81).getLeft()); 188 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(81).getRight()); 189 | Assert.assertEquals("辽宁省大连市经济技术开", pairList.get(82).getLeft()); 190 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(82).getRight()); 191 | Assert.assertEquals("辽宁省大连市经济技术开发", pairList.get(83).getLeft()); 192 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(83).getRight()); 193 | Assert.assertEquals("辽宁省大连市经济技术开发区", pairList.get(84).getLeft()); 194 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(84).getRight()); 195 | Assert.assertEquals("辽宁省大连市经济技术开发区辽", pairList.get(85).getLeft()); 196 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(85).getRight()); 197 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河", pairList.get(86).getLeft()); 198 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(86).getRight()); 199 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西", pairList.get(87).getLeft()); 200 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(87).getRight()); 201 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路", pairList.get(88).getLeft()); 202 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(88).getRight()); 203 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(89).getLeft()); 204 | Assert.assertEquals("辽宁省大连市经济技术开发区辽河西路号", pairList.get(89).getRight()); 205 | 206 | // 用于生成测试语句 207 | for (int i = 0; i < pairList.size(); i++) { 208 | 209 | System.out.println("Assert.assertEquals(\""+ pairList.get(i).getLeft() +"\", pairList.get("+ i +").getLeft());"); 210 | System.out.println("Assert.assertEquals(\""+ pairList.get(i).getRight() +"\", pairList.get("+ i +").getRight());"); 211 | } 212 | 213 | // 214 | // for (Pair stringStringPair : pairList) { 215 | // System.out.println(stringStringPair.getLeft() + " " + stringStringPair.getRight()); 216 | // } 217 | } 218 | 219 | 220 | } 221 | -------------------------------------------------------------------------------- /ansible/roles/openresty/files/resty-template-1.9/template.lua: -------------------------------------------------------------------------------- 1 | local setmetatable = setmetatable 2 | local loadstring = loadstring 3 | local loadchunk 4 | local tostring = tostring 5 | local setfenv = setfenv 6 | local require = require 7 | local capture 8 | local concat = table.concat 9 | local assert = assert 10 | local prefix 11 | local write = io.write 12 | local pcall = pcall 13 | local phase 14 | local open = io.open 15 | local load = load 16 | local type = type 17 | local dump = string.dump 18 | local find = string.find 19 | local gsub = string.gsub 20 | local byte = string.byte 21 | local null 22 | local sub = string.sub 23 | local ngx = ngx 24 | local jit = jit 25 | local var 26 | 27 | local _VERSION = _VERSION 28 | local _ENV = _ENV 29 | local _G = _G 30 | 31 | local HTML_ENTITIES = { 32 | ["&"] = "&", 33 | ["<"] = "<", 34 | [">"] = ">", 35 | ['"'] = """, 36 | ["'"] = "'", 37 | ["/"] = "/" 38 | } 39 | 40 | local CODE_ENTITIES = { 41 | ["{"] = "{", 42 | ["}"] = "}", 43 | ["&"] = "&", 44 | ["<"] = "<", 45 | [">"] = ">", 46 | ['"'] = """, 47 | ["'"] = "'", 48 | ["/"] = "/" 49 | } 50 | 51 | local VAR_PHASES 52 | 53 | local ok, newtab = pcall(require, "table.new") 54 | if not ok then newtab = function() return {} end end 55 | 56 | local caching = true 57 | local template = newtab(0, 12) 58 | 59 | template._VERSION = "1.9" 60 | template.cache = {} 61 | 62 | local function enabled(val) 63 | if val == nil then return true end 64 | return val == true or (val == "1" or val == "true" or val == "on") 65 | end 66 | 67 | local function trim(s) 68 | return gsub(gsub(s, "^%s+", ""), "%s+$", "") 69 | end 70 | 71 | local function rpos(view, s) 72 | while s > 0 do 73 | local c = sub(view, s, s) 74 | if c == " " or c == "\t" or c == "\0" or c == "\x0B" then 75 | s = s - 1 76 | else 77 | break 78 | end 79 | end 80 | return s 81 | end 82 | 83 | local function escaped(view, s) 84 | if s > 1 and sub(view, s - 1, s - 1) == "\\" then 85 | if s > 2 and sub(view, s - 2, s - 2) == "\\" then 86 | return false, 1 87 | else 88 | return true, 1 89 | end 90 | end 91 | return false, 0 92 | end 93 | 94 | local function readfile(path) 95 | local file = open(path, "rb") 96 | if not file then return nil end 97 | local content = file:read "*a" 98 | file:close() 99 | return content 100 | end 101 | 102 | local function loadlua(path) 103 | return readfile(path) or path 104 | end 105 | 106 | local function loadngx(path) 107 | local vars = VAR_PHASES[phase()] 108 | local file, location = path, vars and var.template_location 109 | if sub(file, 1) == "/" then file = sub(file, 2) end 110 | if location and location ~= "" then 111 | if sub(location, -1) == "/" then location = sub(location, 1, -2) end 112 | local res = capture(concat{ location, '/', file}) 113 | if res.status == 200 then return res.body end 114 | end 115 | local root = vars and (var.template_root or var.document_root) or prefix 116 | if sub(root, -1) == "/" then root = sub(root, 1, -2) end 117 | return readfile(concat{ root, "/", file }) or path 118 | end 119 | 120 | do 121 | if ngx then 122 | VAR_PHASES = { 123 | set = true, 124 | rewrite = true, 125 | access = true, 126 | content = true, 127 | header_filter = true, 128 | body_filter = true, 129 | log = true 130 | } 131 | template.print = ngx.print or write 132 | template.load = loadngx 133 | prefix, var, capture, null, phase = ngx.config.prefix(), ngx.var, ngx.location.capture, ngx.null, ngx.get_phase 134 | if VAR_PHASES[phase()] then 135 | caching = enabled(var.template_cache) 136 | end 137 | else 138 | template.print = write 139 | template.load = loadlua 140 | end 141 | if _VERSION == "Lua 5.1" then 142 | local context = { __index = function(t, k) 143 | return t.context[k] or t.template[k] or _G[k] 144 | end } 145 | if jit then 146 | loadchunk = function(view) 147 | return assert(load(view, nil, nil, setmetatable({ template = template }, context))) 148 | end 149 | else 150 | loadchunk = function(view) 151 | local func = assert(loadstring(view)) 152 | setfenv(func, setmetatable({ template = template }, context)) 153 | return func 154 | end 155 | end 156 | else 157 | local context = { __index = function(t, k) 158 | return t.context[k] or t.template[k] or _ENV[k] 159 | end } 160 | loadchunk = function(view) 161 | return assert(load(view, nil, nil, setmetatable({ template = template }, context))) 162 | end 163 | end 164 | end 165 | 166 | function template.caching(enable) 167 | if enable ~= nil then caching = enable == true end 168 | return caching 169 | end 170 | 171 | function template.output(s) 172 | if s == nil or s == null then return "" end 173 | if type(s) == "function" then return template.output(s()) end 174 | return tostring(s) 175 | end 176 | 177 | function template.escape(s, c) 178 | if type(s) == "string" then 179 | if c then return gsub(s, "[}{\">/<'&]", CODE_ENTITIES) end 180 | return gsub(s, "[\">/<'&]", HTML_ENTITIES) 181 | end 182 | return template.output(s) 183 | end 184 | 185 | function template.new(view, layout) 186 | assert(view, "view was not provided for template.new(view, layout).") 187 | local render, compile = template.render, template.compile 188 | if layout then 189 | if type(layout) == "table" then 190 | return setmetatable({ render = function(self, context) 191 | local context = context or self 192 | context.blocks = context.blocks or {} 193 | context.view = compile(view)(context) 194 | layout.blocks = context.blocks or {} 195 | layout.view = context.view or "" 196 | return layout:render() 197 | end }, { __tostring = function(self) 198 | local context = self 199 | context.blocks = context.blocks or {} 200 | context.view = compile(view)(context) 201 | layout.blocks = context.blocks or {} 202 | layout.view = context.view 203 | return tostring(layout) 204 | end }) 205 | else 206 | return setmetatable({ render = function(self, context) 207 | local context = context or self 208 | context.blocks = context.blocks or {} 209 | context.view = compile(view)(context) 210 | return render(layout, context) 211 | end }, { __tostring = function(self) 212 | local context = self 213 | context.blocks = context.blocks or {} 214 | context.view = compile(view)(context) 215 | return compile(layout)(context) 216 | end }) 217 | end 218 | end 219 | return setmetatable({ render = function(self, context) 220 | return render(view, context or self) 221 | end }, { __tostring = function(self) 222 | return compile(view)(self) 223 | end }) 224 | end 225 | 226 | function template.precompile(view, path, strip) 227 | local chunk = dump(template.compile(view), strip ~= false) 228 | if path then 229 | local file = open(path, "wb") 230 | file:write(chunk) 231 | file:close() 232 | end 233 | return chunk 234 | end 235 | 236 | function template.compile(view, key, plain) 237 | assert(view, "view was not provided for template.compile(view, key, plain).") 238 | if key == "no-cache" then 239 | return loadchunk(template.parse(view, plain)), false 240 | end 241 | key = key or view 242 | local cache = template.cache 243 | if cache[key] then return cache[key], true end 244 | local func = loadchunk(template.parse(view, plain)) 245 | if caching then cache[key] = func end 246 | return func, false 247 | end 248 | 249 | function template.parse(view, plain) 250 | assert(view, "view was not provided for template.parse(view, plain).") 251 | if not plain then 252 | view = template.load(view) 253 | if byte(sub(view, 1, 1)) == 27 then return view end 254 | end 255 | local j = 2 256 | local c = {[[ 257 | context=... or {} 258 | local function include(v, c) return template.compile(v)(c or context) end 259 | local ___,blocks,layout={},blocks or {} 260 | ]] } 261 | local i, s = 1, find(view, "{", 1, true) 262 | while s do 263 | local t, p = sub(view, s + 1, s + 1), s + 2 264 | if t == "{" then 265 | local e = find(view, "}}", p, true) 266 | if e then 267 | local z, w = escaped(view, s) 268 | if i < s - w then 269 | c[j] = "___[#___+1]=[=[\n" 270 | c[j+1] = sub(view, i, s - 1 - w) 271 | c[j+2] = "]=]\n" 272 | j=j+3 273 | end 274 | if z then 275 | i = s 276 | else 277 | c[j] = "___[#___+1]=template.escape(" 278 | c[j+1] = trim(sub(view, p, e - 1)) 279 | c[j+2] = ")\n" 280 | j=j+3 281 | s, i = e + 1, e + 2 282 | end 283 | end 284 | elseif t == "*" then 285 | local e = find(view, "*}", p, true) 286 | if e then 287 | local z, w = escaped(view, s) 288 | if i < s - w then 289 | c[j] = "___[#___+1]=[=[\n" 290 | c[j+1] = sub(view, i, s - 1 - w) 291 | c[j+2] = "]=]\n" 292 | j=j+3 293 | end 294 | if z then 295 | i = s 296 | else 297 | c[j] = "___[#___+1]=template.output(" 298 | c[j+1] = trim(sub(view, p, e - 1)) 299 | c[j+2] = ")\n" 300 | j=j+3 301 | s, i = e + 1, e + 2 302 | end 303 | end 304 | elseif t == "%" then 305 | local e = find(view, "%}", p, true) 306 | if e then 307 | local z, w = escaped(view, s) 308 | if z then 309 | if i < s - w then 310 | c[j] = "___[#___+1]=[=[\n" 311 | c[j+1] = sub(view, i, s - 1 - w) 312 | c[j+2] = "]=]\n" 313 | j=j+3 314 | end 315 | i = s 316 | else 317 | local n = e + 2 318 | if sub(view, n, n) == "\n" then 319 | n = n + 1 320 | end 321 | local r = rpos(view, s - 1) 322 | if i <= r then 323 | c[j] = "___[#___+1]=[=[\n" 324 | c[j+1] = sub(view, i, r) 325 | c[j+2] = "]=]\n" 326 | j=j+3 327 | end 328 | c[j] = trim(sub(view, p, e - 1)) 329 | c[j+1] = "\n" 330 | j=j+2 331 | s, i = n - 1, n 332 | end 333 | end 334 | elseif t == "(" then 335 | local e = find(view, ")}", p, true) 336 | if e then 337 | local z, w = escaped(view, s) 338 | if i < s - w then 339 | c[j] = "___[#___+1]=[=[\n" 340 | c[j+1] = sub(view, i, s - 1 - w) 341 | c[j+2] = "]=]\n" 342 | j=j+3 343 | end 344 | if z then 345 | i = s 346 | else 347 | local f = sub(view, p, e - 1) 348 | local x = find(f, ",", 2, true) 349 | if x then 350 | c[j] = "___[#___+1]=include([=[" 351 | c[j+1] = trim(sub(f, 1, x - 1)) 352 | c[j+2] = "]=]," 353 | c[j+3] = trim(sub(f, x + 1)) 354 | c[j+4] = ")\n" 355 | j=j+5 356 | else 357 | c[j] = "___[#___+1]=include([=[" 358 | c[j+1] = trim(f) 359 | c[j+2] = "]=])\n" 360 | j=j+3 361 | end 362 | s, i = e + 1, e + 2 363 | end 364 | end 365 | elseif t == "[" then 366 | local e = find(view, "]}", p, true) 367 | if e then 368 | local z, w = escaped(view, s) 369 | if i < s - w then 370 | c[j] = "___[#___+1]=[=[\n" 371 | c[j+1] = sub(view, i, s - 1 - w) 372 | c[j+2] = "]=]\n" 373 | j=j+3 374 | end 375 | if z then 376 | i = s 377 | else 378 | c[j] = "___[#___+1]=include(" 379 | c[j+1] = trim(sub(view, p, e - 1)) 380 | c[j+2] = ")\n" 381 | j=j+3 382 | s, i = e + 1, e + 2 383 | end 384 | end 385 | elseif t == "-" then 386 | local e = find(view, "-}", p, true) 387 | if e then 388 | local x, y = find(view, sub(view, s, e + 1), e + 2, true) 389 | if x then 390 | local z, w = escaped(view, s) 391 | if z then 392 | if i < s - w then 393 | c[j] = "___[#___+1]=[=[\n" 394 | c[j+1] = sub(view, i, s - 1 - w) 395 | c[j+2] = "]=]\n" 396 | j=j+3 397 | end 398 | i = s 399 | else 400 | y = y + 1 401 | x = x - 1 402 | if sub(view, y, y) == "\n" then 403 | y = y + 1 404 | end 405 | local b = trim(sub(view, p, e - 1)) 406 | if b == "verbatim" or b == "raw" then 407 | if i < s - w then 408 | c[j] = "___[#___+1]=[=[\n" 409 | c[j+1] = sub(view, i, s - 1 - w) 410 | c[j+2] = "]=]\n" 411 | j=j+3 412 | end 413 | c[j] = "___[#___+1]=[=[" 414 | c[j+1] = sub(view, e + 2, x) 415 | c[j+2] = "]=]\n" 416 | j=j+3 417 | else 418 | if sub(view, x, x) == "\n" then 419 | x = x - 1 420 | end 421 | local r = rpos(view, s - 1) 422 | if i <= r then 423 | c[j] = "___[#___+1]=[=[\n" 424 | c[j+1] = sub(view, i, r) 425 | c[j+2] = "]=]\n" 426 | j=j+3 427 | end 428 | c[j] = 'blocks["' 429 | c[j+1] = b 430 | c[j+2] = '"]=include[=[' 431 | c[j+3] = sub(view, e + 2, x) 432 | c[j+4] = "]=]\n" 433 | j=j+5 434 | end 435 | s, i = y - 1, y 436 | end 437 | end 438 | end 439 | elseif t == "#" then 440 | local e = find(view, "#}", p, true) 441 | if e then 442 | local z, w = escaped(view, s) 443 | if i < s - w then 444 | c[j] = "___[#___+1]=[=[\n" 445 | c[j+1] = sub(view, i, s - 1 - w) 446 | c[j+2] = "]=]\n" 447 | j=j+3 448 | end 449 | if z then 450 | i = s 451 | else 452 | e = e + 2 453 | if sub(view, e, e) == "\n" then 454 | e = e + 1 455 | end 456 | s, i = e - 1, e 457 | end 458 | end 459 | end 460 | s = find(view, "{", s + 1, true) 461 | end 462 | s = sub(view, i) 463 | if s and s ~= "" then 464 | c[j] = "___[#___+1]=[=[\n" 465 | c[j+1] = s 466 | c[j+2] = "]=]\n" 467 | j=j+3 468 | end 469 | c[j] = "return layout and include(layout,setmetatable({view=table.concat(___),blocks=blocks},{__index=context})) or table.concat(___)" 470 | return concat(c) 471 | end 472 | 473 | function template.render(view, context, key, plain) 474 | assert(view, "view was not provided for template.render(view, context, key, plain).") 475 | return template.print(template.compile(view, key, plain)(context)) 476 | end 477 | 478 | return template 479 | --------------------------------------------------------------------------------