From f54bc3d3e880c63f611f865c8ea739a514a7ea4d Mon Sep 17 00:00:00 2001 From: jysperm Date: Sat, 26 Jul 2014 02:06:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=91=E5=AF=BC=E6=A8=A1=E5=BC=8F=E4=B8=8E?= =?UTF-8?q?=20JSON=20=E6=A0=BC=E5=BC=8F=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WIKI/Nginx/JSON-CONFIGURE.md | 4 +- core/static/script/panel.coffee | 99 +++++++++++++++++++++++++++++++-- plugin/nginx/configure.coffee | 9 ++- plugin/nginx/view/widget.jade | 16 ++++-- 4 files changed, 114 insertions(+), 14 deletions(-) diff --git a/WIKI/Nginx/JSON-CONFIGURE.md b/WIKI/Nginx/JSON-CONFIGURE.md index 8e0bd51..c504219 100644 --- a/WIKI/Nginx/JSON-CONFIGURE.md +++ b/WIKI/Nginx/JSON-CONFIGURE.md @@ -135,8 +135,8 @@ * location - proxy_set_header * 在转发至 http 服务器时设置 HTTP 头 - * 字符串,可选指令 - * 值为一个域名或 `$host` + * 对象,可选指令 + * 键名为 `Host` 时,值为一个域名或 `$host` * location - proxy_redirect diff --git a/core/static/script/panel.coffee b/core/static/script/panel.coffee index 0e237ca..4bbe080 100644 --- a/core/static/script/panel.coffee +++ b/core/static/script/panel.coffee @@ -31,8 +31,99 @@ $ -> .success -> location.reload() + $('#nginx-type-json textarea').on 'change keyup paste', -> + try + JSON.parse($('#nginx-type-json textarea').val()) + $('.json-error').parent().addClass 'hide' + catch err + console.log err + $('.json-error').text err.toString() + $('.json-error').parent().removeClass 'hide' + + syncToJSON = -> + username = $('.nav.navbar-nav.navbar-right li:first a').text() + + try + config = JSON.parse($('#nginx-type-json textarea').val()) + catch e + config = {} + + config['is_enable'] = $('.option-is-enable input').prop('checked') + config['server_name'] = $('.option-server-name input').val().split ' ' + + switch $('.option-type :radio:checked').val() + when 'fastcgi' + config['root'] = $('.option-root input').val() or $('.option-root input').prop('placeholder') + config['location'] ?= {} + config['location']['/'] = + try_files: ['$uri', '$uri/', '/index.php?$args'] + config['location']['~ \\.php$'] = + fastcgi_pass: "unix:///home/#{username}/phpfpm.sock" + include: 'fastcgi_params' + + when 'proxy' + config['location'] ?= {} + config['location']['/'] = + proxy_pass: $('.option-proxy input').val() or $('.option-proxy input').prop('placeholder') + proxy_set_header: + Host: '$host' + + when 'uwsgi' + config['location'] ?= {} + config['location']['/'] = + uwsgi_pass: $('.option-uwsgi input').val() or $('.option-uwsgi input').prop('placeholder') + include: 'uwsgi_params' + + when 'static' + config['root'] = $('.option-root input').val() or $('.option-root input').prop('placeholder') + + json = $('#nginx-type-json textarea').val JSON.stringify(config, null, ' ') + return json + + $('#nginx-modal ul li a').click -> + switch $(@).prop('href').match(/.*#nginx-type-(.*)/)[1] + when 'json' + syncToJSON() + when 'guide' + $('.json-error').parent().addClass 'hide' + + try + config = JSON.parse($('#nginx-type-json textarea').val()) + catch e + return + + $('.option-is-enable input').prop 'checked', config['is_enable'] + $('.option-server-name input').val config['server_name']?.join ' ' + $('.option-root input').val config['root'] + + type = do -> + unless config['location']['/'] + return 'static' + + if config['location']['/']['proxy_pass'] + return 'proxy' + + if config['location']['/']['uwsgi_pass'] + return 'uwsgi' + + if config['location']['/']?['try_files'] + for item in config['location']['/']['try_files'] + if item.match(/\.php/) + return 'factcgi' + + return 'static' + + $("#nginx-modal :radio[value=#{type}]").click() + + switch type + when 'proxy' + $('.option-proxy input').val config['location']['/']['proxy_pass'] + when 'uwsgi' + $('.option-uwsgi input').val config['location']['/']['uwsgi_pass'] + when 'static', 'fastcgi' + $('.option-root input').val config['root'] + $('#nginx-modal .radio input').click -> - console.log $(@).val() options = ['root', 'proxy', 'uwsgi'] mapping_table = @@ -50,10 +141,10 @@ $ -> $("#nginx-modal .option-#{item}").addClass 'hide' $('#nginx-modal .modal-footer button.btn-success').click -> - type = $('#nginx-modal ul.config-type').find('.active a').attr('href')['#nginx-type-'.length..] + type = $('#nginx-modal ul.config-type').find('.active a').prop('href')['#nginx-type-'.length..] if type == 'guide' - config = {} + config = syncToJSON() else if type == 'json' try @@ -116,7 +207,7 @@ $ -> $.post '/plugin/nginx/update_site', JSON.stringify { action: 'delete' id: id - type: $('#nginxConfigType').find('.active a').attr('href').substr 1 + type: $('#nginxConfigType').find('.active a').prop('href').substr 1 } .success -> location.reload() diff --git a/plugin/nginx/configure.coffee b/plugin/nginx/configure.coffee index bc65b05..0911b88 100644 --- a/plugin/nginx/configure.coffee +++ b/plugin/nginx/configure.coffee @@ -63,8 +63,13 @@ exports.assert = (account, config, site_id, callback) -> return callback 'invalid_proxy_pass' when 'proxy_set_header' - unless value == '$host' or utils.rx.domain.test value - return callback 'proxy_set_header' + for header_name, header_value of value + switch header_name + when 'Host' + unless header_value == '$host' or utils.rx.domain.test header_value + return callback 'invalid_proxy_set_header' + else + return callback 'invalid_proxy_set_header' when 'proxy_redirect' config.location['proxy_redirect'] = if value then true else false diff --git a/plugin/nginx/view/widget.jade b/plugin/nginx/view/widget.jade index 7514ab5..695ae86 100644 --- a/plugin/nginx/view/widget.jade +++ b/plugin/nginx/view/widget.jade @@ -36,24 +36,24 @@ table.table.table-hover .tab-pane.active#nginx-type-guide form.form-horizontal(role='form') h3 常规 - .form-group + .form-group.option-is-enable label.col-sm-2.control-label » .checkbox.col-sm-10 label - input(type='checkbox') + input(type='checkbox', checked) | 启用站点 - .form-group + .form-group.option-server-name label.col-sm-2.control-label 域名 .col-sm-10 input.form-control(type='text', placeholder='example.com example.net') h3 类型 - .form-group + .form-group.option-type label.col-sm-2.control-label » .controls.col-sm-10 .radio label - input(type='radio', name='site-type', value='fastcgi') + input(type='radio', name='site-type', value='fastcgi', checked) | fastcgi (PHP) .radio label @@ -69,7 +69,7 @@ table.table.table-hover | static (静态文件) h3 选项 - .form-group.option-root.hide + .form-group.option-root label.col-sm-2.control-label 根目录 .col-sm-10 input.form-control(type='text', placeholder='/home/#{account.username}/web') @@ -88,5 +88,9 @@ table.table.table-hover .tab-pane#nginx-type-original p 即将支持 .modal-footer + p.pull-left.hide + span.glyphicon.glyphicon-warning-sign + |   + span.json-error.text-danger 错误信息 button.btn.btn-danger(type='button', data-dismiss='modal') 关闭 button.btn.btn-success(type='button') 保存