Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi.staticfiles import StaticFiles
from datetime import date
import json
from typing import List

app = FastAPI()

Expand Down Expand Up @@ -56,7 +57,7 @@ def add_route(
endpoint: str = Form(...),
response_message: str = Form(...),
response_status: int = Form(...),
methods: list = Form([]),
methods: List[str] = Form(...),
):
if endpoint.startswith("/"):
endpoint = endpoint[1:]
Expand Down
61 changes: 51 additions & 10 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="/static/favicon.png" type="image/x-icon" />

<title>Mock Server Config Viewer</title>
<title>Mock Server Config</title>

<script src="/static/alpine.js" defer></script>

Expand Down Expand Up @@ -153,24 +153,57 @@ <h1>Mock Server Config Viewer</h1>
<label>Select HTTP Methods:</label><br />
<div>
<label
><input type="checkbox" value="GET" x-model="methods" /> GET</label
><input
type="checkbox"
value="GET"
name="methods"
x-model="methods"
/>
GET</label
><br />
<label
><input type="checkbox" value="POST" x-model="methods" /> POST</label
><input
type="checkbox"
value="POST"
name="methods"
x-model="methods"
/>
POST</label
><br />
<label
><input type="checkbox" value="PUT" x-model="methods" /> PUT</label
><input
type="checkbox"
value="PUT"
name="methods"
x-model="methods"
/>
PUT</label
><br />
<label
><input type="checkbox" value="DELETE" x-model="methods" />
><input
type="checkbox"
value="DELETE"
name="methods"
x-model="methods"
/>
DELETE</label
><br />
<label
><input type="checkbox" value="PATCH" x-model="methods" />
><input
type="checkbox"
value="PATCH"
name="methods"
x-model="methods"
/>
PATCH</label
><br />
<label
><input type="checkbox" value="OPTIONS" x-model="methods" />
><input
type="checkbox"
value="OPTIONS"
name="methods"
x-model="methods"
/>
OPTIONS</label
>
</div>
Expand Down Expand Up @@ -221,6 +254,7 @@ <h2>Current Route Config</h2>
<thead>
<tr>
<th>Endpoint</th>
<th>Methods</th>
<th>Response</th>
<th>Status</th>
</tr>
Expand All @@ -229,6 +263,7 @@ <h2>Current Route Config</h2>
{% for route_data in routes_data %}
<tr>
<td>{{ route_data.get('endpoint') }}</td>
<td>{{ route_data.get('methods') }}</td>
<td>{{ route_data.get('response') }}</td>
<td>{{ route_data.get('status') }}</td>
</tr>
Expand All @@ -244,17 +279,23 @@ <h2>Current Route Config</h2>
function validateJson() {
const form = document.getElementById("jsonForm");
const jsonData = form.response_message.value;
const selectedMethods = [
...form.querySelectorAll('input[name="methods"]:checked'),
].map((cb) => cb.value);

if (isValid(jsonData)) {
form.submit();
} else {
if (!isValid(jsonData)) {
alert("Invalid JSON format. Please correct it.");
} else if (selectedMethods.length === 0) {
alert("Please select at least one method.");
} else {
form.submit();
}
}

function isValid(jsonString) {
try {
JSON.parse(jsonString);

return true;
} catch {
return false;
Expand Down
22 changes: 13 additions & 9 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
routes:
- endpoint: /hello
response:
message: Hello, Mock Server!
status: 200
- endpoint: /status
response:
message: available
status: ok
status: 200
- endpoint: hello
methods:
- GET
response:
message: Hello, Mock Server!
status: 200
- endpoint: status
methods:
- GET
response:
message: available
status: ok
status: 200