-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
240 lines (207 loc) · 6.03 KB
/
app.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
from flask import Flask, abort, request, jsonify
from flasgger import Swagger
import flask_monitoringdashboard as dashboard
import markdown
from config import build_config_file
from protocols import MasterServer, BeamMP, Factorio, Palworld, Scum, TheFront
from version import __version__
app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY', '')
swagger_config = {
"headers": [],
"specs": [
{
"endpoint": 'apispec_1',
"route": '/apispec_1.json',
"rule_filter": lambda rule: True, # all in
"model_filter": lambda tag: True, # all in
}
],
"title": "OpenGSQ Master Server Search API",
"version": __version__,
"description": "Powered By OpenGSQ",
"termsOfService": "/terms",
"static_url_path": "/flasgger_static",
"swagger_ui": True,
"specs_route": "/",
"favicon": "/static/favicon.ico"
}
swagger = Swagger(app, config=swagger_config)
def search(args: dict, master_server: MasterServer):
host = args.get('host')
port = args.get('port')
# Check if host and port are provided
if not host or not port:
abort(400, description="Missing parameters: 'host' and 'port' are required.")
try:
port = int(port)
except ValueError:
abort(400, description="'port' must be an integer.")
result = master_server.find(host=host, port=port)
# Check if result is found
if not result:
abort(404, description="No result found.")
return jsonify(result)
@app.route('/beammp/search', methods=['GET'])
def beammp_search():
"""
BeamMP Search
This endpoint allows you to search for a BeamMP server using its host and port.
---
tags:
- Search EndPoint
parameters:
- name: host
in: query
type: string
required: true
- name: port
in: query
type: integer
required: true
responses:
200:
description: Success
400:
description: Invalid parameters were supplied. 'host' and 'port' must be provided, and 'port' must be an integer.
404:
description: No server was found with the provided host and port.
"""
return search(request.args, BeamMP())
@app.route('/factorio/search', methods=['GET'])
def factorio_search():
"""
Factorio Search
This endpoint allows you to search for a Factorio server using its host and port.
---
tags:
- Search EndPoint
parameters:
- name: host
in: query
type: string
required: true
- name: port
in: query
type: integer
required: true
responses:
200:
description: Success
400:
description: Invalid parameters were supplied. 'host' and 'port' must be provided, and 'port' must be an integer.
404:
description: No server was found with the provided host and port.
"""
return search(request.args, Factorio())
@app.route('/palworld/search', methods=['GET'])
def palworld_search():
"""
Palworld Search
This endpoint allows you to search for a Palworld server using its host and port.
---
tags:
- Search EndPoint
parameters:
- name: host
in: query
type: string
required: true
- name: port
in: query
type: integer
required: true
responses:
200:
description: Success
400:
description: Invalid parameters were supplied. 'host' and 'port' must be provided, and 'port' must be an integer.
404:
description: No server was found with the provided host and port.
"""
return search(request.args, Palworld())
@app.route('/scum/search', methods=['GET'])
def scum_search():
"""
SCUM Search
This endpoint allows you to search for a SCUM server using its host and port.
---
tags:
- Search EndPoint
parameters:
- name: host
in: query
type: string
required: true
- name: port
in: query
type: integer
required: true
responses:
200:
description: Success
400:
description: Invalid parameters were supplied. 'host' and 'port' must be provided, and 'port' must be an integer.
404:
description: No server was found with the provided host and port.
"""
return search(request.args, Scum())
@app.route('/thefront/search', methods=['GET'])
def thefront_search():
"""
The Front Search
This endpoint allows you to search for a The Front server using its host and port.
---
tags:
- Search EndPoint
parameters:
- name: host
in: query
type: string
required: true
- name: port
in: query
type: integer
required: true
responses:
200:
description: Success
400:
description: Invalid parameters were supplied. 'host' and 'port' must be provided, and 'port' must be an integer.
404:
description: No server was found with the provided host and port.
"""
return search(request.args, TheFront())
@app.route("/terms")
def render_terms():
# Read the contents of terms.md
with open("terms.md", "r") as terms_file:
md_content = terms_file.read()
# Convert Markdown to HTML
html_content = markdown.markdown(md_content)
return html_content
@app.route("/stats")
def render_stats():
"""
Statistics Requests
This endpoint allows you to retrieve the count of servers for each server type.
---
tags:
- Statistics Endpoint
responses:
200:
description: Success
"""
db = MasterServer.get_db()
# Get a list of collection names in the database
collection_names = db.list_collection_names()
# Create a dictionary to store collection counts
collection_counts = {
name.lower(): db[name].count_documents({}) for name in collection_names}
return collection_counts
build_config_file()
dashboard.config.init_from(file='config.cfg')
dashboard.bind(app)
if __name__ == '__main__':
app.run(debug=True)