Refactor HTTP function editor with Mithril components and new API endpoints

- Add new Mithril components for editor (editor.js), response view (responseView.js), and alerts (alert.js)
- Create new API endpoints for creating, updating, and deleting HTTP functions
- Update templates to use new Mithril editor component
- Improve header navigation with more consistent styling and active state indicators
- Remove old edit form route and template
- Add new dedicated editor route and template

cursor.ai
This commit is contained in:
Peter Stockings
2025-02-14 00:40:45 +11:00
parent abf3ca15d2
commit 5ec8bba9e8
15 changed files with 1230 additions and 131 deletions

View File

@@ -23,6 +23,10 @@
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script src="/static/js/mithril/editor.js"></script>
<script src="/static/js/mithril/responseView.js"></script>
<script src="/static/js/mithril/alert.js"></script>
<script src="https://unpkg.com/ace-diff@^2"></script>
<link href="https://unpkg.com/ace-diff@^2/dist/ace-diff.min.css" rel="stylesheet">

View File

@@ -2,9 +2,14 @@
{% block page %}
{{ render_partial('dashboard/http_functions/header.html', title='Try', user_id=user_id, function_id=function_id,
name=name,
show_refresh=False, show_link=False, show_edit_form=True, show_client=True, show_logs=True, show_history=True) }}
{{ render_partial('dashboard/http_functions/header.html', user_id=user_id, function_id=function_id,
active_tab='client',
show_edit_form=True,
show_logs=True,
show_client=True,
show_history=True,
edit_url=url_for('http_function_editor', function_id=function_id),
cancel_url=url_for('dashboard_http_functions')) }}
<div class="mx-auto w-full pt-4" id="client-u{{ user_id }}-f{{ function_id }}">
</div>

View File

@@ -1,14 +0,0 @@
{% extends 'dashboard.html' %}
{% block page %}
{{ render_partial('dashboard/http_functions/header.html', title='Update', user_id=user_id, function_id=function_id,
name=name,
refresh_url=url_for('get_http_function_edit_form', function_id=function_id), show_logs=True, show_client=True,
show_history=True) }}
{{ render_partial('function_editor.html', function_id=function_id, name=name, script=script,
environment_info=environment_info,
is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number, is_edit=True) }}
{% endblock %}

View File

@@ -0,0 +1,41 @@
{% extends 'dashboard.html' %}
{% block page %}
{{ render_partial('dashboard/http_functions/header.html', user_id=user_id, function_id=function_id,
active_tab='edit',
show_edit_form=True,
show_logs=True,
show_client=True,
show_history=True,
edit_url=edit_url,
cancel_url=cancel_url) }}
<div id="app" class="p-1">
<!-- The Editor component will be mounted here -->
</div>
<script>
// Mount the component
m.mount(document.getElementById("app"), {
view: () => m(Editor, {
name: '{{ name }}',
functionId: {{ id }},
jsValue: {{ script_content | tojson | safe }},
jsonValue: {{ environment_info | tojson | safe }},
isEdit: true,
showHeader: true,
isPublic: {{ is_public | tojson }},
logRequest: {{ log_request | tojson }},
logResponse: {{ log_response | tojson }},
versionNumber: {{ version_number }},
executeUrl: "{{ url_for('execute_code', playground='true') }}",
saveUrl: "{{ url_for('api_update_http_function', function_id=id) if id else url_for('api_create_http_function') }}",
deleteUrl: "{{ url_for('api_delete_http_function', function_id=id) if id else '' }}",
showDeleteButton: true
})
})
</script>
{% endblock %}

View File

@@ -1,103 +1,108 @@
<div>
<div class="flex items-center pb-1" data-id="51">
<div class="flex items-center">
<div class="bg-white dark:bg-gray-800 border-b">
<!-- Action Buttons Row -->
<div class="flex items-center justify-between pb-2">
<!-- Action Buttons (left side) -->
<div class="flex items-center space-x-4">
<h1 class="font-semibold text-lg md:text-2xl" data-id="52">
{{ title }}{% if show_name|default(true, false) %}: <span class="font-mono">{{ name }}</span>{% endif %}
{{ title }}
</h1>
{% if show_refresh|default(true, false) %}
<div class="ml-2 cursor-pointer text-gray-500" hx-get="{{ refresh_url }}" hx-target="#container"
hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6" data-darkreader-inline-stroke=""
style="--darkreader-inline-stroke: currentColor;">
<path stroke-linecap="round" stroke-linejoin="round"
d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99">
</path>
</svg>
</div>
{% if show_edit_form|default(false, true) %}
<button
class="group flex flex-col items-center {% if active_tab == 'edit' %}text-blue-600{% else %}text-gray-500 hover:text-blue-600{% endif %}"
hx-get="{{ edit_url }}" hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
<div
class="p-2 rounded-lg {% if active_tab == 'edit' %}bg-blue-50{% else %}group-hover:bg-blue-50{% endif %}">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" data-slot="icon" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
</div>
<span class="text-xs font-medium">Edit</span>
</button>
{% endif %}
{% if show_logs|default(false, true) %}
<div class="ml-2 cursor-pointer text-gray-500"
<button
class="group flex flex-col items-center {% if active_tab == 'logs' %}text-blue-600{% else %}text-gray-500 hover:text-blue-600{% endif %}"
hx-get="{{ url_for('get_http_function_logs', function_id=function_id) }}" hx-target="#container"
hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" data-slot="icon" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
</div>
<div
class="p-2 rounded-lg {% if active_tab == 'logs' %}bg-blue-50{% else %}group-hover:bg-blue-50{% endif %}">
<svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round"
d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
</div>
<span class="text-xs font-medium">Logs</span>
</button>
{% endif %}
{% if show_client|default(false, true) %}
<div class="ml-2 cursor-pointer text-gray-500" hx-get="{{ url_for('client', function_id=function_id) }}"
hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="m3.75 13.5 10.5-11.25L12 10.5h8.25L9.75 21.75 12 13.5H3.75Z" />
</svg>
</div>
{% endif %}
{% if show_edit_form|default(false, true) %}
<div class="ml-2 cursor-pointer text-gray-500"
hx-get="{{ url_for('get_http_function_edit_form', function_id=function_id) }}" hx-target="#container"
hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" data-slot="icon" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
</div>
<button
class="group flex flex-col items-center {% if active_tab == 'client' %}text-blue-600{% else %}text-gray-500 hover:text-blue-600{% endif %}"
hx-get="{{ url_for('client', function_id=function_id) }}" hx-target="#container" hx-swap="innerHTML"
hx-push-url="true">
<div
class="p-2 rounded-lg {% if active_tab == 'client' %}bg-blue-50{% else %}group-hover:bg-blue-50{% endif %}">
<svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round"
d="m3.75 13.5 10.5-11.25L12 10.5h8.25L9.75 21.75 12 13.5H3.75Z" />
</svg>
</div>
<span class="text-xs font-medium">Client</span>
</button>
{% endif %}
{% if show_history|default(false, true) %}
<div class="ml-2 cursor-pointer text-gray-500"
<button
class="group flex flex-col items-center {% if active_tab == 'history' %}text-blue-600{% else %}text-gray-500 hover:text-blue-600{% endif %}"
hx-get="{{ url_for('get_http_function_history', function_id=function_id) }}" hx-target="#container"
hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 512 512" stroke-width="25"
stroke="currentColor" data-slot="icon" class="w-6 h-6">
<path
d="M416,160a64,64,0,1,0-96.27,55.24c-2.29,29.08-20.08,37-75,48.42-17.76,3.68-35.93,7.45-52.71,13.93V151.39a64,64,0,1,0-64,0V360.61a64,64,0,1,0,64.42.24c2.39-18,16-24.33,65.26-34.52,27.43-5.67,55.78-11.54,79.78-26.95,29-18.58,44.53-46.78,46.36-83.89A64,64,0,0,0,416,160ZM160,64a32,32,0,1,1-32,32A32,32,0,0,1,160,64Zm0,384a32,32,0,1,1,32-32A32,32,0,0,1,160,448ZM352,192a32,32,0,1,1,32-32A32,32,0,0,1,352,192Z">
</path>
</svg>
</div>
<div
class="p-2 rounded-lg {% if active_tab == 'history' %}bg-blue-50{% else %}group-hover:bg-blue-50{% endif %}">
<svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 512 512"
stroke-width="25" stroke="currentColor">
<path
d="M416,160a64,64,0,1,0-96.27,55.24c-2.29,29.08-20.08,37-75,48.42-17.76,3.68-35.93,7.45-52.71,13.93V151.39a64,64,0,1,0-64,0V360.61a64,64,0,1,0,64.42.24c2.39-18,16-24.33,65.26-34.52,27.43-5.67,55.78-11.54,79.78-26.95,29-18.58,44.53-46.78,46.36-83.89A64,64,0,0,0,416,160ZM160,64a32,32,0,1,1-32,32A32,32,0,0,1,160,64Zm0,384a32,32,0,1,1,32-32A32,32,0,0,1,160,448ZM352,192a32,32,0,1,1,32-32A32,32,0,0,1,352,192Z">
</path>
</svg>
</div>
<span class="text-xs font-medium">History</span>
</button>
{% endif %}
{% if show_new|default(false, true) %}
<button
class="group flex flex-col items-center {% if active_tab == 'new' %}text-blue-600{% else %}text-gray-500 hover:text-blue-600{% endif %}"
hx-get="{{ new_url }}" hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
<div
class="p-2 rounded-lg {% if active_tab == 'new' %}bg-blue-50{% else %}group-hover:bg-blue-50{% endif %}">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
</svg>
</div>
<span class="text-xs font-medium">New</span>
</button>
{% endif %}
</div>
<button
class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded flex mr-2 items-center ml-auto"
hx-get="{{ url_for('dashboard_http_functions') }}" hx-target="#container" hx-swap="innerHTML"
hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-5 h-5 mr-2" data-darkreader-inline-stroke=""
style="--darkreader-inline-stroke: currentColor;">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12"></path>
</svg>
<span>Cancel</span>
<!-- Cancel Button (right side) -->
<button class="group flex flex-col items-center text-gray-500 hover:text-blue-600"
hx-get="{{ cancel_url|default(url_for('dashboard_http_functions')) }}" hx-target="#container"
hx-swap="innerHTML" hx-push-url="true">
<div class="p-2 rounded-lg group-hover:bg-blue-50">
<svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12"></path>
</svg>
</div>
<span class="text-xs font-medium">Cancel</span>
</button>
</div>
{% if show_link|default(true, false) %}
<div class="flex">
<a class="text-gray-300" href="{{ url_for('execute_http_function', user_id=user_id, function=name) }}">{{
url_for('execute_http_function', user_id=user_id, function=name) }}</a>
<button
class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground px-2 text-gray-600 dark:text-gray-400"
data-id="24">
<span class="sr-only" data-id="25">Add Link</span>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="w-4 h-4"
data-id="26" data-darkreader-inline-stroke="" style="--darkreader-inline-stroke: currentColor;">
<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71">
</path>
<path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71">
</path>
</svg>
</button>
</div>
{% endif %}
</div>

View File

@@ -2,10 +2,14 @@
{% block page %}
{{ render_partial('dashboard/http_functions/header.html', title='History', user_id=user_id, function_id=function_id,
name=name,
refresh_url=url_for('get_http_function_history', function_id=function_id), show_logs=True, show_client=True,
show_edit_form=True) }}
{{ render_partial('dashboard/http_functions/header.html', user_id=user_id, function_id=function_id,
active_tab='history',
show_edit_form=True,
show_logs=True,
show_client=True,
show_history=True,
edit_url=url_for('http_function_editor', function_id=function_id),
cancel_url=url_for('dashboard_http_functions')) }}
<!-- Timeline -->
<div>
@@ -185,7 +189,6 @@ show_edit_form=True) }}
});
editor.setTheme("ace/theme/github_dark");
editor.session.setMode("ace/mode/javascript");
</script>
<!-- End Item -->

View File

@@ -2,10 +2,14 @@
{% block page %}
{{ render_partial('dashboard/http_functions/header.html', title='Logs', user_id=user_id, function_id=function_id,
name=name,
refresh_url=url_for('get_http_function_logs', function_id=function_id), show_edit_form=True, show_client=True,
show_history=True) }}
{{ render_partial('dashboard/http_functions/header.html', user_id=user_id, function_id=function_id,
active_tab='logs',
show_edit_form=True,
show_logs=True,
show_client=True,
show_history=True,
edit_url=url_for('http_function_editor', function_id=function_id),
cancel_url=url_for('dashboard_http_functions')) }}
<div class="block md:grid md:grid-cols-4 md:gap-4 p-4">
<!-- Headers -->

View File

@@ -2,12 +2,40 @@
{% block page %}
{{ render_partial('dashboard/http_functions/header.html', title='New HTTP function', user_id=user_id, show_name=False,
show_refresh=False, show_logs=False,
show_client=False, show_link=False)
{{ render_partial('dashboard/http_functions/header.html',
user_id=user_id,
show_name=False,
show_refresh=False,
show_logs=False,
show_client=False,
show_link=False,
dashboardUrl=url_for('dashboard_http_functions'),
title='New HTTP Function')
}}
{{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info,
is_public=is_public, log_request=log_request, log_response=log_response, is_add=True) }}
<div id="app" class="p-1">
<!-- The Editor component will be mounted here -->
</div>
<script>
// Mount the component
m.mount(document.getElementById("app"), {
view: () => m(Editor, {
name: '{{ name }}',
jsValue: {{ script | tojson | safe }},
jsonValue: {{ environment_info | tojson | safe }},
isEdit: false,
isAdd: true,
showHeader: true,
isPublic: {{ is_public | tojson }},
logRequest: {{ log_request | tojson }},
logResponse: {{ log_response | tojson }},
executeUrl: "{{ url_for('execute_code', playground='true') }}",
saveUrl: "{{ url_for('api_create_http_function') }}",
showDeleteButton: false,
dashboardUrl: "{{ url_for('dashboard_http_functions') }}"
})
})
</script>
{% endblock %}

View File

@@ -80,7 +80,7 @@
</button>
<button
class="inline-flex items-center justify-center text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-9 rounded-md px-3 text-gray-800"
hx-get="{{ url_for('get_http_function_edit_form', function_id=function.id) }}"
hx-get="{{ url_for('http_function_editor', function_id=function.id) }}"
hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
Edit
</button>

View File

@@ -21,6 +21,13 @@
integrity="sha512-g9yptARGYXbHR9r3kTKIAzF+vvmgEieTxuuUUcHC5tKYFpLR3DR+lsisH2KZJG2Nwaou8jjYVRdbbbBQI3Bo5w=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script src="/static/js/mithril/editor.js"></script>
<script src="/static/js/mithril/responseView.js"></script>
<script src="/static/js/mithril/alert.js"></script>
<script src="https://unpkg.com/ace-diff@^2"></script>
<link href="https://unpkg.com/ace-diff@^2/dist/ace-diff.min.css" rel="stylesheet">
<style>
@import url("https://rsms.me/inter/inter.css");
@@ -109,8 +116,24 @@
View documentation</a>
</div>
<div id="app" class="p-1">
<!-- The Editor component will be mounted here -->
</div>
{{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info) }}
<script>
// Mount the component
m.mount(document.getElementById("app"), {
view: () => m(Editor, {
name: '{{ name }}',
jsValue: {{ script | tojson | safe }},
jsonValue: {{ environment_info | tojson | safe }},
isEdit: true,
showHeader: false,
showFunctionSettings: false,
executeUrl: "{{ url_for('execute_code', playground='true') }}",
})
})
</script>
<section class="py-8">
<div class="container mx-auto flex flex-wrap pt-4 pb-12">