|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>Gateway | SQLite Cloud</title> |
| 6 | + <script src="https://cdn.tailwindcss.com"></script> |
| 7 | + </head> |
| 8 | + |
| 9 | + <body class="p-4"> |
| 10 | + <h1>SQLite Cloud Gateway</h1> |
| 11 | + <h2 id="status" class="pb-6 font-bold cursor-pointer">Disconnected</h2> |
| 12 | + |
| 13 | + <!-- Add the text field and button here --> |
| 14 | + <div class="pb-2"> |
| 15 | + <div class="text-sm w-12">connectionString:</div> |
| 16 | + <input |
| 17 | + type="text" |
| 18 | + id="connectionStringInput" |
| 19 | + placeholder="Type your connection string here" |
| 20 | + value="sqlitecloud://admin:password@host.sqlite.cloud:8860/chinook.db" |
| 21 | + class="border rounded w-full pl-2 pr-2 mb-2" |
| 22 | + /> |
| 23 | + <div class="text-sm w-12">sql:</div> |
| 24 | + <input |
| 25 | + type="text" |
| 26 | + id="messageInput" |
| 27 | + placeholder="Type your SQL query" |
| 28 | + value="select * from tracks where trackid = 1" |
| 29 | + class="border rounded w-full pl-2 pr-2 mb-2" |
| 30 | + /> |
| 31 | + </div> |
| 32 | + <button id="sendButton" class="border rounded w-32 mb-6">send</button> |
| 33 | + |
| 34 | + <h2>Messages:</h2> |
| 35 | + <ul id="messages" class="pl-4"></ul> |
| 36 | + |
| 37 | + <script type="module"> |
| 38 | + import { io } from 'https://cdn.socket.io/4.7.4/socket.io.esm.min.js' |
| 39 | + const status = document.getElementById('status') |
| 40 | + const messages = document.getElementById('messages') |
| 41 | + |
| 42 | + // connection string is stored in local storage so you don't have to type it every time |
| 43 | + let connectionString = localStorage.getItem('connectionString') |
| 44 | + if (!connectionString) { |
| 45 | + connectionString = 'sqlitecloud://admin:password@host.sqlite.cloud:8860/database.db' |
| 46 | + localStorage.setItem('connectionString', connectionString) |
| 47 | + } else connectionStringInput.value = connectionString |
| 48 | + connectionStringInput.addEventListener('change', () => { |
| 49 | + localStorage.setItem('connectionString', connectionStringInput.value) |
| 50 | + }) |
| 51 | + |
| 52 | + const appendMessage = content => { |
| 53 | + const item = document.createElement('li') |
| 54 | + item.classList.add('pb-4') |
| 55 | + item.classList.add('text-sm') |
| 56 | + item.textContent = content |
| 57 | + messages.prepend(item) |
| 58 | + } |
| 59 | + |
| 60 | + function setupSocket() { |
| 61 | + let host = window.location.host |
| 62 | + if (host.indexOf(':') !== -1) host = host.substring(0, host.indexOf(':')) |
| 63 | + console.debug(`socket/connect - connecting to '${host}'`) |
| 64 | + socket = io(`ws://${host}:4000`, { auth: { token: connectionStringInput.value } }) |
| 65 | + socket.on('connect', () => { |
| 66 | + status.innerText = 'Connected' |
| 67 | + appendMessage(`connect | session id: ${socket.id}`) |
| 68 | + }) |
| 69 | + |
| 70 | + socket.on('connect_error', err => { |
| 71 | + appendMessage(`connect_error | reason: ${err.message}`) |
| 72 | + }) |
| 73 | + |
| 74 | + socket.on('disconnect', reason => { |
| 75 | + status.innerText = 'Disconnected' |
| 76 | + appendMessage(`disconnect | reason: ${reason}`) |
| 77 | + }) |
| 78 | + |
| 79 | + return socket |
| 80 | + } |
| 81 | + |
| 82 | + // socket is connected the first time the sql button is clicked and stays connected until the page is refreshed |
| 83 | + var socket = null |
| 84 | + |
| 85 | + sendButton.addEventListener('click', () => { |
| 86 | + if (!socket) { |
| 87 | + socket = setupSocket() |
| 88 | + } |
| 89 | + |
| 90 | + // send an async request to the server |
| 91 | + const sql = messageInput.value |
| 92 | + const startTime = Date.now() |
| 93 | + socket.emit('v1/sql', { sql }, response => { |
| 94 | + console.debug(`sql - sql: ${sql}, elapsed: ${Date.now() - startTime}`, response) |
| 95 | + appendMessage(`sql | ${JSON.stringify(response)}`) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + status.addEventListener('click', () => { |
| 100 | + if (!socket) { |
| 101 | + socket = setupSocket() |
| 102 | + } |
| 103 | + socket.emit('v1/info', { }, response => { |
| 104 | + appendMessage(`info | rss: ${(response.data.rss/1024/1024).toFixed(2)}mb, ${JSON.stringify(response)}`) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | +</script> |
| 109 | + </body> |
| 110 | +</html> |
0 commit comments