var express = require('express');
var cproc = require('child_process');
var bodyParser = require('body-parser');
var execSync = cproc.execSync;
var options = {
  inflate: true,
  limit: '100kb',
  type: 'application/octet-stream'
};

var app = express();
var admin = express(); // the sub app

app.use(bodyParser.urlencoded({ extended: false }))

app.use(bodyParser.json())


admin.get('/get_node_command/:name', function (req, res) {
  console.log(req.params.name)
 	res.setHeader("Content-Type","application/json");
	var str = execSync(`node ./get_project_config.js  -name ${req.params.name} -attr config_file/command/gitPathHead`)
	var items = String(str).replace(/\n/g,"").split("|"); 
 	var config_file = items[0].replace(/\^/g," ");
	console.log(items)
	console.log(items);
	var command  = items[1].replace(/\^/g," ");
	var gitGroup = items[2].replace(/\^/g," ");
	res.send({configPath:config_file,buildCmd:command,gitGroup:gitGroup});
});

admin.post('/add_new_proxy', function (req, res) {
	let request = req.body;
	var str = String(execSync(`cat ../ngrok/docker_env_name.sh`));
	var index = str.indexOf(request["namespace"])
	if( index != -1 ){
		res.send({msg:"已经存在的配置"});
		return
	}else{
		let newstr = str + " " + request["namespace"]
		//文件添加到这里面
		execSync(`${newstr} > ../ngrok/docker_env_name.sh}`)
	}
	
	execSync("../ngrok/make_frp_ini.sh");
	res.send({msg:"执行完成"});
});

admin.get('/get_proxy', function (req, res) {
 	res.setHeader("Content-Type","application/json");
	var str = execSync(`cat ../ngrok/docker_env_name.sh`)
	res.send({namespace:String(str)});
});

app.use('/config_server', admin)
console.log("http://192.168.4.3:10088/config_server/get_node_command") 
app.listen(10088)






