单应用-Flask

/ 默认分类 / 0 条评论 / 2310浏览
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

from flask import Flask, g, request, jsonify
import json
import os
import time
import re
from pathlib import Path

app = Flask(__name__)
app.debug = False

# 跨域支持
def after_request(resp):
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp

app.after_request(after_request)

@app.route('/hello', methods=['GET'])
def hello():
    return jsonify({"say": "hello"})

@app.route('/data/operate', methods=['POST'])
def opearte():
    data = request.json
    _dbName = data.get('dbname')
    _type = data.get('type')
    #关闭服务
    stopCmd = 'taskkill /f /t /im javaw.exe && exit 0'
    state1 = os.system(stopCmd)
    print("关闭服务完成")
    #文件处理
    fileOperate(_dbName, _type)
    time.sleep(10)
    #启动服务
    jarPath = "C:\\Users\\zhang\\Desktop\\wyyt\\"
    startCmd = 'start javaw -jar -Xmx1024m -Xms1024m '+getLastFileName(jarPath)+' --spring.config.location=C:\\Users\\zhang\\Desktop\\wyyt\\config\\  &&  exit'
    print(startCmd)
    state2=os.system(startCmd)
    print("启动服务完成")
    
    return jsonify({"stop":state1, "start":state2})

def fileOperate(_dbName, _type):
    _pfile = "C:\\Users\\zhang\\Desktop\\wyyt\\config\\application.properties.bak"
    _pfile2 = "C:\\Users\\zhang\\Desktop\\wyyt\\config\\application.properties"
    f = open (_pfile, "r+")
    with open(_pfile2, 'w') as f2:
        str1 = re.sub(r'miniwms_offline_ft2_171', _dbName, f.read())
        str2 = re.sub(r'wms.whId=171', "wms.whId="+_type, str1)
        f2.write(str2)
    f.close()

def getLastFileName(path):
    targetJar = ""
    f_list = os.listdir(path)
    p_time=0
    for i in f_list:
        m_time = (os.path.getmtime(path+i))
        if os.path.splitext(i)[1] == '.jar' and m_time>p_time:
            targetJar = path+i
        p_time = m_time
    return targetJar

 
if __name__ == '__main__':
  app.run(host='192.168.12.151',port=9010)