1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Deno Deployでアクセスログ【Deno KV】

Last updated at Posted at 2023-05-04

サーバーにアクセスがあるか確認したいときにおすすめです。Webhookなどのデバッグも可能です。

curl https://acc.deno.dev/any_access_path_hogehoge

See the Pen Untitled by John Doe (@04) on CodePen.

削除するには行をクリックしてね

import { serve } from "https://deno.land/std@0.177.0/http/server.ts"

const kv = await Deno.openKv()

const init = {
    headers: {
      "content-type": "text/html",
    }
}

function escapeHtml(unsafe)
{
    return unsafe
         .replace(/&/g, "&")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
 }

const toHTML = logs => {
    const trs = logs.map(log => `<tr onclick="event.target.tagName === 'TD' && event.target.parentNode.parentNode.removeChild(event.target.parentNode) && fetch('https://a.github.day/${log.key.join('/')}')" title="クリックして削除">
        <td>${log.value.connInfo.remoteAddr.hostname}</td>
        <td nowrap>${new Date(log.key[1]).toLocaleString('sv-SE', { timeZone: 'Asia/Tokyo' })}</td>
        <td>${log.value.method}</td>
        <td nowrap>${log.value.url}</td>
        <td nowrap><details><pre>${escapeHtml(JSON.stringify(log.value, null, 2))}</pre></details></td>
      </tr>`).join('\n')
    return `<meta charset="utf-8"><table border>
    <thead>
        <tr>
            <th>Remote Host</th>
            <th>Time</th>
            <th>Method</th>
            <th>URL</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        ${trs}
    </tbody>
</table>
<style>
body {
    margin: 0
}
table {
    border-collapse: collapse
}
tr:hover {
    background: #f5f5f5
}
td {
    padding: 0 4px
}
</style>`
}

serve(async (req: Request, connInfo: ConnInfo) => {
    if (req.url.startsWith('https://a.github.day/logs/')) {
        await kv.delete(['logs', parseInt(new URL(req.url).pathname.split('/').pop(), 10)])
        return new Response('CLEARED', { headers: { 'Access-Control-Allow-Origin': '*' } })
    }
    const key = ['logs', Date.now()]
    const value = { headers: Object.fromEntries([...req.headers]), url: req.url, method: req.method, connInfo, env: Deno.env.toObject() }
    if (['POST', 'PUT'].includes(req.method)) value.body = await req.text()
    kv.set(key, value)
    if (req.url.includes('no-response')) return new Response()
    const logs = []
    for await (const log of kv.list({ prefix: ['logs'] }, { reverse: true })) logs.push(log)
    return new Response(toHTML(logs), init)
})
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?