0.0.1-SNAPSHOT
wangbing 5 years ago
parent aa2ee4165b
commit f8dcfe3d26

@ -4,14 +4,23 @@ import com.example.frame.base.BaseResponse;
import com.example.frame.base.Error;
import com.example.frame.base.ErrorType;
import com.example.frame.base.Token;
import com.example.frame.utils.*;
import com.example.frame.utils.LocalData;
import com.example.frame.utils.LogUtil;
import com.example.frame.utils.MD5Util;
import com.example.frame.utils.MapperUtil;
import com.example.frame.utils.Message;
import com.example.frame.utils.ProcessUtil;
import com.example.frame.utils.ValidationUtil;
import com.example.module.admin.ent.Mapping;
import com.example.module.admin.ent.NginxCtrl;
import com.example.module.admin.ent.Service;
import com.example.module.admin.mgr.MappingManager;
import com.example.module.admin.req.*;
import com.example.module.admin.req.LoginRequest;
import com.example.module.admin.req.MappingCreateRequest;
import com.example.module.admin.req.MappingDeleteRequest;
import com.example.module.admin.req.MappingFindRequest;
import com.example.module.admin.req.MappingUpdateRequest;
import com.example.module.admin.rsp.LoginResponse;
import com.example.module.admin.rsp.MappingFindResponse;
import com.example.module.admin.rsp.PortLoadResponse;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
@ -22,17 +31,23 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import xyz.wbsite.wsqlite.ObjectClient;
import xyz.wbsite.wsqlite.Where;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class AjaxController {
@ -87,6 +102,9 @@ public class AjaxController {
case "nginx.reload":
baseResponse = nginxReload(jsonString, token);
break;
case "ajax.admin.mapping.find":
baseResponse = findMapping(jsonString, token);
break;
// 创建映射
case "ajax.admin.mapping.create":
baseResponse = createMapping(jsonString, token);
@ -102,9 +120,6 @@ public class AjaxController {
baseResponse = updateMapping(jsonString, token);
flushConfig();
break;
case "ajax.admin.service.load":
baseResponse = loadPort(jsonString, token);
break;
case "ajax.admin.config.flush":
flushConfig();
baseResponse = new BaseResponse();
@ -214,39 +229,31 @@ public class AjaxController {
return mappingManager.find(request, token);
}
private BaseResponse loadPort(String jsonString, Token token) {
PortLoadResponse response = new PortLoadResponse();
private synchronized void flushConfig() {
Writer wr = null;
try {
List<Service> select = objectClient.select(Service.class, Where.builder().build(), 1, 0);
File config = nginxCtrl.getConfig();
HashMap<String, Object> context = new HashMap<>();
for (Service service : select) {
MappingFindRequest mappingFindRequest = new MappingFindRequest();
mappingFindRequest.setServiceId(service.getId());
mappingFindRequest.setPageSize(0);
MappingFindResponse mappingFindResponse = mappingManager.find(mappingFindRequest, token);
if (mappingFindResponse.hasError()) {
response.addErrors(mappingFindResponse.getErrors());
return response;
}
service.setMappingList(mappingFindResponse.getResult());
}
response.setResult(select);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
response.addError(ErrorType.BUSINESS_ERROR, "配置加载错误");
return response;
mappingFindRequest.setSortKey("SERVICE_PORT");
MappingFindResponse mappingFindResponse = mappingManager.find(mappingFindRequest, LocalData.getSysToken());
Map<String, List<Mapping>> services = new HashMap<>();
for (Mapping mapping : mappingFindResponse.getResult()) {
List<Mapping> mappings = services.get(mapping.getServicePort());
if (mappings == null) {
mappings = new ArrayList<>();
services.put(mapping.getServicePort(), mappings);
}
return response;
mappings.add(mapping);
}
private synchronized void flushConfig() {
Writer wr = null;
try {
File config = nginxCtrl.getConfig();
HashMap<String, Object> context = new HashMap<>();
PortLoadResponse portLoadResponse = (PortLoadResponse) loadPort("", LocalData.getSysToken());
context.put("services", portLoadResponse.getResult());
context.put("services", services);
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("nginx.conf.ftl");
wr = new OutputStreamWriter(new FileOutputStream(config), "UTF-8");
//写入

@ -88,10 +88,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
token1.putResource("nginx.start");
token1.putResource("nginx.stop");
token1.putResource("nginx.reload");
token1.putResource("ajax.admin.mapping.find");
token1.putResource("ajax.admin.mapping.create");
token1.putResource("ajax.admin.mapping.delete");
token1.putResource("ajax.admin.mapping.update");
token1.putResource("ajax.admin.service.load");
token1.putResource("ajax.admin.config.flush");
LocalData.setToken(token1);
}

@ -2,7 +2,6 @@ package com.example.config;
import com.example.frame.utils.IDgenerator;
import com.example.module.admin.ent.Mapping;
import com.example.module.admin.ent.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
@ -37,14 +36,8 @@ public class SqliteConfig {
ArrayList<Class> objects = new ArrayList<>();
objects.add(Mapping.class);
objects.add(Service.class);
ObjectClient objectClient = new ObjectClient(new File(dbpath, "data.db"), objects);
List<Service> select = objectClient.select(Service.class, Where.builder().build(),1, 1);
if (select.size() == 0 && objectClient.insert(Service.class, new Service(IDgenerator.nextId(), "8888", "默认端口")) != 1) {
throw new RuntimeException("Nginx default port 8888 init failed.");
}
return objectClient;
} catch (ClassNotFoundException e) {
e.printStackTrace();

@ -19,12 +19,8 @@ public class Mapping {
*/
@TableField
private Long id;
/**
* ID - ID
*/
@TableField
private Long serviceId;
private String servicePort;
@TableField
private String name;
@TableField
@ -48,12 +44,12 @@ public class Mapping {
this.type = type;
}
public Long getServiceId() {
return serviceId;
public String getServicePort() {
return servicePort;
}
public void setServiceId(Long serviceId) {
this.serviceId = serviceId;
public void setServicePort(String servicePort) {
this.servicePort = servicePort;
}
public String getName() {

@ -1,77 +0,0 @@
package com.example.module.admin.ent;
import xyz.wbsite.wsqlite.anonation.TableField;
import java.util.Date;
import java.util.List;
/**
* MAPPING -
*
* @author author
* @version 0.0.1
* @since 2019-09-28
*/
public class Service {
/**
* VALUE - ID
*/
@TableField
private Long id;
/**
* VALUE -
*/
@TableField
private String value;
/**
* VALUE -
*/
@TableField
private String bz;
private List<Mapping> mappingList;
public List<Mapping> getMappingList() {
return mappingList;
}
public void setMappingList(List<Mapping> mappingList) {
this.mappingList = mappingList;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getBz() {
return bz;
}
public void setBz(String bz) {
this.bz = bz;
}
public Service() {
}
public Service(Long id, String value, String bz) {
this.id = id;
this.value = value;
this.bz = bz;
}
}

@ -1,9 +1,8 @@
package com.example.module.admin.mgr;
import com.example.frame.utils.IDgenerator;
import com.example.frame.utils.Message;
import com.example.frame.base.ErrorType;
import com.example.frame.base.Token;
import com.example.frame.utils.IDgenerator;
import com.example.frame.utils.MapperUtil;
import com.example.frame.utils.ValidationUtil;
import com.example.module.admin.ent.Mapping;
@ -52,6 +51,21 @@ public class MappingManagerImpl implements MappingManager {
return response;
}
try {
Where where = Where.builder().eq("SERVICEPORT", request.getServicePort()).eq("CONTEXT", request.getContext()).build();
List<Mapping> select = objectClient.select(Mapping.class, where, 1, 0);
if (select.size() > 0) {
response.addError(ErrorType.BUSINESS_ERROR, request.getServicePort() + ":" + request.getContext() + "已经被占用");
return response;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
long id = IDgenerator.nextId();
Mapping entity = MapperUtil.map(request, Mapping.class);
entity.setId(id);

@ -21,8 +21,9 @@ public class MappingCreateRequest extends BaseRequest {
@NotNull(message = "映射名称不能为空")
private String name;
@NotNull(message = "服务Id不能为空")
private Long serviceId;
@NotNull(message = "服务端口不能为空")
@Pattern(regexp = "^[0-9]*$", message = "服务端口必须是数字")
private String servicePort;
@NotNull(message = "映射路径不能为空")
@Pattern(regexp = "^/[a-zA-Z][a-zA-Z0-9_-]*$|^[A-Z]:/.*$|^/$", message = "映射路径须是(/)或(/+字母+字母数字及_")
@ -36,12 +37,12 @@ public class MappingCreateRequest extends BaseRequest {
private String bz;
public Long getServiceId() {
return serviceId;
public String getServicePort() {
return servicePort;
}
public void setServiceId(Long serviceId) {
this.serviceId = serviceId;
public void setServicePort(String servicePort) {
this.servicePort = servicePort;
}
public String getName() {

@ -11,16 +11,13 @@ import com.example.frame.base.BaseFindRequest;
*/
public class MappingFindRequest extends BaseFindRequest {
/**
*
*/
private Long serviceId;
private String search;
public Long getServiceId() {
return serviceId;
public String getSearch() {
return search;
}
public void setServiceId(Long serviceId) {
this.serviceId = serviceId;
public void setSearch(String search) {
this.search = search;
}
}

@ -1,8 +0,0 @@
package com.example.module.admin.rsp;
import com.example.frame.base.BaseFindResponse;
import com.example.module.admin.ent.Service;
public class PortLoadResponse extends BaseFindResponse<Service> {
}

@ -128,6 +128,12 @@ window.ajax = {
data: fd
})
},
mappingFind: function (data) {
return jsonRequest({
method:"ajax.admin.mapping.find",
data: JSON.stringify(data),
})
},
mappingCreate: function (data) {
return jsonRequest({
method:"ajax.admin.mapping.create",
@ -146,12 +152,6 @@ window.ajax = {
data: JSON.stringify(data),
})
},
serviceLoad: function(data) {
return jsonRequest({
method:"ajax.admin.service.load",
data: JSON.stringify(data),
})
},
configFlush: function(data) {
return jsonRequest({
method:"ajax.admin.config.flush",

@ -31,17 +31,17 @@ http {
keepalive_timeout 65;
#gzip on;
<#list services as item>
<#list services.keySet() as key>
server {
listen ${item.value};
listen ${key};
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
<#list item.mappingList as mapping>
<#list services[key] as mapping>
<#if mapping.type="HTTP">
location ${mapping.context} {
proxy_pass ${mapping.location};

@ -33,27 +33,10 @@
</el-col>
</el-row>
</el-card>
<el-card class="box-card" v-for="item in services" :key="item.id">
<el-row>
<el-col :span="4">
<el-input size="small" v-model="item.value" placeholder="请输入端口" style="width: 160px;text-align: center"
suffix-icon="el-icon-edit">
<template slot="prepend">端口</template>
</el-input>
</el-col>
<el-col :span="5">
<el-input size="small" v-model="item.bz" placeholder="请输入备注" style="width: 200px;text-align: center"
suffix-icon="el-icon-collection-tag">
<template slot="prepend">备注</template>
</el-input>
</el-col>
<el-col :span="2">
<el-button type="primary" size="small" icon="el-icon-refresh">保存</el-button>
</el-col>
<el-col :span="13">
<el-col :span="24">
<span style="float: right;">
<el-button type="success" size="small" icon="el-icon-plus" @click="onAction(['create',item])">新增
<el-button type="success" size="small" icon="el-icon-plus" @click="onAction(['create',{}])">新增
</el-button>
<el-button size="small" icon="el-icon-refresh" @click="onRefresh"></el-button>
</span>
@ -61,7 +44,7 @@
</el-row>
<el-table
:data="item.mappingList.filter(data => !item.search || data.name.toLowerCase().includes(item.search.toLowerCase())|| data.value.toLowerCase().includes(item.search.toLowerCase()))"
:data="result.filter(data => !vm.search || data.name.toLowerCase().includes(vm.search.toLowerCase())|| data.value.toLowerCase().includes(vm.search.toLowerCase()))"
style="width: 100%">
<el-table-column
width="150px"
@ -78,6 +61,11 @@
</el-popover>
</template>
</el-table-column>
<el-table-column
width="130px"
label="端口"
prop="servicePort">
</el-table-column>
<el-table-column
width="130px"
label="路径"
@ -102,7 +90,7 @@
align="right">
<template slot="header" slot-scope="scope">
<el-input
v-model="item.search"
v-model="vm.search"
size="small"
placeholder="输入关键字搜索"/>
</template>
@ -119,7 +107,6 @@
</template>
</el-table-column>
</el-table>
</el-card>
<el-dialog :title="form.title" :visible.sync="form.dialog" width="550px">
<el-form :model="form" :rules="formRules" ref="form" label-width="80px"
@ -131,15 +118,21 @@
</el-col>
</el-row>
</el-form-item>
<el-form-item label="映射端口" required prop="servicePort">
<el-row>
<el-col :span="22">
<el-input v-model="form.servicePort" type="number" clearable size="small" placeholder="映射端口"></el-input>
</el-col>
</el-row>
</el-form-item>
<el-form-item label="映射路径" required prop="context">
<el-row>
<el-col :span="22">
<el-input v-model="form.context" clearable size="small" placeholder="字母数字组合">
<template slot="prepend">:{{form.port}}</template>
<template slot="prepend">http://host:{{form.servicePort}}</template>
</el-input>
</el-col>
</el-row>
</el-form-item>
<el-form-item label="映射类型" required prop="type">
<el-radio v-model="form.type" label="HTTP">HTTP</el-radio>
@ -212,6 +205,9 @@
var app = new Vue({
el: "#app",
data: {
vm: {
search: ""
},
form: {
title: "",
dialog: false,
@ -227,6 +223,10 @@
{required: true, message: '映射名称不能为空', trigger: 'blur'},
{min: 1, max: 50, message: '长度在 1 到 50 字符', trigger: 'blur'}
],
servicePort: [
{required: true, message: '映射端口不能为空', trigger: 'blur'},
{pattern: "^[0-9]*$", message: '映射端口只能为数字', trigger: 'blur'},
],
context: [
{required: true, message: '映射路径不能为空', trigger: 'blur'},
{pattern: "^/[a-zA-Z][a-zA-Z0-9_-]*$|^/$", message: '上下文须是(/)或(/+字母+字母数字及_', trigger: 'blur'},
@ -253,19 +253,16 @@
}
],
},
services: [],
result: [],
run: ${run?default('false')}
},
methods: {
onFind: function () {
ajax.serviceLoad().then(function (response) {
ajax.mappingFind(this.vm).then(function (response) {
if (response.errors.length > 0) {
nav.e(response.errors[0].message);
} else {
for (var i in response.result) {
response.result[i].search = "";
}
this.services = response.result;
this.result = response.result;
}
}.bind(this))
},
@ -274,7 +271,7 @@
switch (action) {
case "create":
this.form.title = '新增';
this.form.serviceId = arg[1].id;
this.form.servicePoert = "8080";
this.form.port = arg[1].value;
this.form.id = '';
this.form.name = '';

@ -65,7 +65,7 @@ public class Where {
public Builder like(boolean condition, String name, Object value) {
if (condition) {
sb.append(sb.length() > 0 ? " and " : " where ").append(name).append("like ?");
sb.append(sb.length() > 0 ? " and " : " where ").append(name).append(" like ?");
argsList.add(value);
}
return this;
@ -73,7 +73,7 @@ public class Where {
public Builder isNull(boolean condition, String name, Object value) {
if (condition) {
sb.append(sb.length() > 0 ? " and " : " where ").append(name).append("is null");
sb.append(sb.length() > 0 ? " and " : " where ").append(name).append(" is null");
argsList.add(value);
}
return this;
@ -81,7 +81,7 @@ public class Where {
public Builder isNotNull(boolean condition, String name, Object value) {
if (condition) {
sb.append(sb.length() > 0 ? " and " : " where ").append(name).append("is not null");
sb.append(sb.length() > 0 ? " and " : " where ").append(name).append(" is not null");
argsList.add(value);
}
return this;

Loading…
Cancel
Save

Powered by TurnKey Linux.