-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
108 lines (99 loc) · 3.42 KB
/
server.py
File metadata and controls
108 lines (99 loc) · 3.42 KB
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
107
108
import json
import requests
import os
from flask import Flask, redirect, make_response
from threading import Lock
from random import randint
lock = Lock()
app = Flask(__name__)
GITHUB_TOKEN = os.environ.get("GH_TOKEN")
REPO = "MadAvidCoder/readmeprogrammerdebates"
def trigger_github_dispatch():
url = f"https://api.github.com/repos/{REPO}/dispatches"
headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github+json"
}
data = {"event_type": "vote_cast"}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 204:
print("Failed to trigger dispatch:", response.text)
@app.route("/")
def main():
print("Visitor")
return "Hi! This is a test!"
@app.route('/option-1')
def option_1():
with lock:
with open("votes.txt", "r+") as f:
cur_votes = f.read()
if cur_votes:
if not cur_votes.strip():
cur_votes = '{"option1": 0, "option2": 0}'
cur_votes = json.loads(cur_votes)
cur_votes["option1"] += 1
else:
cur_votes = {"option1": 1, "option2": 0}
f.seek(0)
f.write(json.dumps(cur_votes))
f.truncate()
print("option2!")
trigger_github_dispatch()
return redirect("https://github.com/MadAvidCoder?voted={}/#programmer-debate".format(randint(1, 999)))
@app.route('/option-2')
def option_2():
with lock:
with open("votes.txt", "r+") as f:
cur_votes = f.read()
if cur_votes:
if not cur_votes.strip():
cur_votes = '{"option1": 0, "option2": 0}'
cur_votes = json.loads(cur_votes)
cur_votes["option2"] += 1
else:
cur_votes = {"option1": 0, "option2": 1}
f.seek(0)
f.write(json.dumps(cur_votes))
f.truncate()
print("option1!")
trigger_github_dispatch()
return redirect("https://github.com/MadAvidCoder?voted={}/#programmer-debate".format(randint(1, 999)))
@app.route('/results-1')
def results_1():
with lock:
with open("votes.txt", "r") as f:
cur_votes = f.read()
if not cur_votes.strip():
cur_votes = '{"option1": 0, "option2": 0}'
cur_votes = json.loads(cur_votes)
print("results 1")
print(cur_votes)
resp = make_response(json.dumps({"value": cur_votes["option1"]}))
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"
return resp
@app.route('/results-2')
def results_2():
with lock:
with open("votes.txt", "r") as f:
cur_votes = f.read()
if not cur_votes.strip():
cur_votes = '{"option1": 0, "option2": 0}'
cur_votes = json.loads(cur_votes)
print("reuslts 2")
print(cur_votes)
resp = make_response(json.dumps({"value": cur_votes["option2"]}))
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"
return resp
@app.route('/reset')
def reset():
print("resetting")
with lock:
with open("votes.txt", "w") as f:
f.write('{"option1": 0, "option2": 0}')
return ''
if __name__ == '__main__':
app.run(host='0.0.0.0', port=41863)