-
Notifications
You must be signed in to change notification settings - Fork 36
/
ide-server.py
107 lines (84 loc) · 2.82 KB
/
ide-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from flask import Flask, request, render_template, Markup
import requests
from urllib.parse import urlparse
import markdown
import os
import subprocess
import socket
app = Flask(__name__)
try:
# Looking for the IP address on the K8s
faas = "faas-netesd.default"
ip = socket.getaddrinfo(faas, 0, 0, 0, 0)
faas_port = 8080
swarm_tag = ""
except Exception:
# finds Docker swarm host IP upon no K8s
p1 = subprocess.Popen(["/sbin/ip", "route"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["awk", "/default/ { print $3 }"], stdin=p1.stdout, stdout=subprocess.PIPE)
faas = (p2.stdout).read().decode("utf-8").replace("\n", "")
faas_port = 8080
swarm_tag = "time2code_"
@app.route('/')
def time2code():
text = request.args.get('code')
lang = request.args.get('lang')
straight_text = request.args.get('straight_text')
code_text = ""
if text:
r_text = requests.get(text + "?raw=true")
code_text = r_text.text
elif straight_text:
code_text = straight_text
else:
code_text = ""
if lang:
code_lang = lang
else:
code_lang = "python3"
return render_template('index-panel.html', code_text=code_text, code_lang=code_lang)
@app.route('/code', methods=['POST'])
def code():
if request.method == 'POST':
data = request.data
lang = request.args.get('lang')
hosturl = urlparse(request.url)
host = hosturl.hostname
# url = "http://%s:%s/function/time2py" % host
url = "http://%s:%s/function/%s%s" % (faas, faas_port, swarm_tag, lang)
# print(url)
headers = {"Content-Type": "text/plain"}
code_exec = requests.post(url, data=data, headers=headers)
resp = code_exec.text
# print(resp)
return resp
@app.route('/tutorial')
def tutorial():
text = request.args.get('code')
lang = request.args.get('lang')
straight_text = request.args.get('straight_text')
get_tut = request.args.get('tut')
code_text = ""
tut_url = ""
mark = ""
if get_tut:
tut_url = get_tut + "?raw=true"
r_tut = requests.get(tut_url)
mark = r_tut.text
else:
tut_url = "https://raw.githubusercontent.com/JockDaRock/Time2Code/master/Sample.md?raw=true"
r_tut = requests.get(tut_url)
mark = r_tut.text
if text:
r_text = requests.get(text + "?raw=true")
code_text = r_text.text
elif straight_text:
code_text = straight_text
if lang:
code_lang = lang
else:
code_lang = "python3"
content = Markup(markdown.markdown(mark, extensions=['pymdownx.github', 'pymdownx.highlight']))
return render_template('index-tut.html', markdown=content, code_text=code_text, code_lang=code_lang)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5555, debug=True)