Skip to content

Commit

Permalink
add realtime log
Browse files Browse the repository at this point in the history
  • Loading branch information
creamlike1024 committed Jun 28, 2023
1 parent 548d5b4 commit 8f623d3
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ output:
dir: output
file: index.html
template:
markdown: template.md
info: template_info.html
html_head: template_head.html
log: template_log.html
time:
format: 2006-01-02 15:04:05 MST
timezone: UTC
15 changes: 5 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type Config struct {
RedisPassword string
OutputDir string
OutputFilename string
TemplateFilename string
TemplateInfoFilename string
TemplateHeadFilename string
templateLogFilename string
TimeFormat string
Timezone string
}
Expand All @@ -38,7 +39,8 @@ func main() {
RedisPassword: viper.GetString("redis.password"),
OutputDir: viper.GetString("output.dir"),
OutputFilename: viper.GetString("output.file"),
TemplateFilename: viper.GetString("template.markdown"),
TemplateInfoFilename: viper.GetString("template.info"),
templateLogFilename: viper.GetString("template.log"),
TemplateHeadFilename: viper.GetString("template.html_head"),
TimeFormat: viper.GetString("time.format"),
Timezone: viper.GetString("time.timezone"),
Expand All @@ -52,15 +54,8 @@ func main() {
ctx := context.Background()
// 获取订阅者和关注者列表
subscribeAndFollowers := GetSubcribesAndFollowers(rdb, ctx)
// 读取模板文件
templateFile, err := os.ReadFile(config.TemplateFilename)
if err != nil {
fmt.Printf("Failed to read template file: %s\n", config.TemplateFilename)
panic(err)
}
// 生成 HTML 文件
bodyHtml := MdToHTML(FillMarkdownTemplate(templateFile, subscribeAndFollowers))
htmlBytes := FillHtml(bodyHtml)
htmlBytes := GenHtml(&subscribeAndFollowers)
// 输出HTML文件
// 检查输出目录是否存在,如果不存在则创建
if _, err := os.Stat(config.OutputDir); os.IsNotExist(err) {
Expand Down
31 changes: 23 additions & 8 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,41 @@ func MdToHTML(md []byte) []byte {
return markdown.Render(doc, renderer)
}

func FillHtml(html []byte) []byte {
htmlStr := string(html)
func GenHtml(subList *[]string) []byte {
var htmlStr string
// 添加 html 头
htmlHead, err := os.ReadFile(config.TemplateHeadFilename)
if err != nil {
fmt.Printf("Failed to read template file: %s\n", config.TemplateHeadFilename)
panic(err)
}
htmlStr = string(htmlHead) + "\n<body>\n" + "<section class=\"main\">\n" + htmlStr
// 添加页尾信息
htmlStr += "\n<footer>\n"
htmlStr = string(htmlHead) + "\n<body>\n" + "<section class=\"main\">\n"
// 添加中继信息
infoBody, err := os.ReadFile(config.TemplateInfoFilename)
if err != nil {
fmt.Printf("Failed to read template file: %s\n", config.TemplateInfoFilename)
panic(err)
}
htmlStr += "\n" + string(infoBody)
// 添加订阅列表
subListHtml := MdToHTML(GenSubMdList(*subList))
htmlStr += "\n" + string(subListHtml)
htmlStr += fmt.Sprintf("\n<p>感谢大家的支持</p>\n")
loc, _ := time.LoadLocation(config.Timezone)
htmlStr += fmt.Sprintf("\n<p>本页最后更新于 %s</p>\n", time.Now().In(loc).Format(config.TimeFormat))
htmlStr += "\n</footer>\n"
htmlStr += fmt.Sprintf("\n<p>列表最后更新于 %s</p>\n", time.Now().In(loc).Format(config.TimeFormat))
// 添加实时日志
logBody, err := os.ReadFile(config.templateLogFilename)
if err != nil {
fmt.Printf("Failed to read template file: %s\n", config.templateLogFilename)
panic(err)
}
htmlStr += "\n" + string(logBody)
// 添加 html 尾
htmlStr += "\n</section>\n</body>\n</html>"
return []byte(htmlStr)
}

func FillMarkdownTemplate(md []byte, domainList []string) []byte {
func GenSubMdList(domainList []string) []byte {
var list string
count := 0
var wg sync.WaitGroup
Expand All @@ -66,6 +80,7 @@ func FillMarkdownTemplate(md []byte, domainList []string) []byte {
}(d)
}
wg.Wait()
var md []byte
md = append(md, []byte(fmt.Sprintf("\n共 **%d** 个站点\n", count))...)
return append(md, []byte(list)...)
}
21 changes: 21 additions & 0 deletions template_head.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
max-width: 1024px;
}
}
span.sel {
color: var(--color-primary);
cursor: pointer;
}
/* 设置 h1 的行距 */
h1{
margin-top: 1.5em;
Expand Down Expand Up @@ -62,5 +66,22 @@
li{
margin-top: 0.5em;
}
/* 美化代码块 */
pre {
background-color: #f5f5f5;
border-radius: 0.5em;
padding: 1em;
overflow-x: auto;
}
/* 美化按钮,扁平样式 */
button {
border: none;
border-radius: 0.5em;
padding: 0.5em 1em;
background-color: var(--color-primary);
color: white;
cursor: pointer;
transition: .2s all ease-in-out;
}
</style>
</head>
35 changes: 35 additions & 0 deletions template_info.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h1 id="relay-isle-moe">relay.isle.moe</h1>

<h2 id="订阅">订阅</h2>

<p>欢迎~</p>

<ul>
<li>对于 Mastodon 和 Misskey 等兼容中继协议的软件可以使用这个链接订阅: <span class="sel" title="点击复制">https://relay.isle.moe/inbox</span></li>
<li>对于 Pleroma,请根据<a href="https://docs-develop.pleroma.social/backend/administration/CLI_tasks/relay/" target="_blank">官方文档</a>使用命令行订阅该地址: <span class="sel" title="点击复制">https://relay.isle.moe/actor</span></li>
<li>对于其他软件,请参考相应软件的文档订阅中继</li>
</ul>

<p>如果订阅异常可以试试移除中继再重新订阅!</p>

<p>所使用的后端为 <a href="https://github.com/yukimochi/Activity-Relay" target="_blank">yukimochi/Activity-Relay</a></p>

<p>页面生成器:<a href="https://github.com/creamlike1024/isle-activity-relay-frontend" target="_blank">isle-activity-relay-frontend</a></p>

<p>有问题可以联系 <a href="https://m.isle.moe/@claire" target="_blank">@Claire</a></p>

<h2 id="订阅的实例列表">订阅的实例列表</h2>

<p><em>每 5 分钟更新一次</em></p>

<!-- sel 点击复制 -->
<script>
let sel = document.getElementsByClassName('sel')
for (let i = 0; i < sel.length; i++) {
sel[i].onclick = () => {
navigator.clipboard.writeText(sel[i].innerText).then(() => {
alert('复制成功!')
})
}
}
</script>
41 changes: 41 additions & 0 deletions template_log.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h2 id="收信日志">收信日志</h2>
<pre id="server-logs" style="overflow-y: scroll; font-family: monospace; height: 20em"></pre>
<script>
let ws1 = new WebSocket('wss://relay.isle.moe/log-ws/server.log?tail=1')
ws1.onmessage = (event) => {
if (event.data === 'ping') {
ws1.send('pong')
return
}
document.getElementById('server-logs').innerHTML += event.data
document.getElementById('server-logs').scrollTop = document.getElementById('server-logs').scrollHeight
}
</script>
<button onclick="document.getElementById('server-logs').innerHTML = ''">清除收信日志</button>
<button onclick="copyLogs('server-logs')">复制日志</button>

<h2 id="送信日志">送信日志</h2>
<pre id="worker-logs" style="overflow-y: scroll; font-family: monospace; height: 30em"></pre>
<script>
let ws2 = new WebSocket('wss://relay.isle.moe/log-ws/worker.log?tail=1')
ws2.onmessage = (event) => {
if (event.data === 'ping') {
ws2.send('pong')
return
}
document.getElementById('worker-logs').innerHTML += event.data
document.getElementById('worker-logs').scrollTop = document.getElementById('worker-logs').scrollHeight
}
</script>
<button onclick="document.getElementById('worker-logs').innerHTML = ''">清除送信日志</button>
<button onclick="copyLogs('worker-logs')">复制日志</button>

<!-- 复制按钮 -->
<script>
function copyLogs(elementId) {
let logs = document.getElementById(elementId).innerText
navigator.clipboard.writeText(logs).then(() => {
alert('复制成功!')
})
}
</script>

0 comments on commit 8f623d3

Please sign in to comment.