mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-20 15:33:46 +08:00
* refactor: 重构前端代码 几乎完全重构前端js代码 小幅度重构后端代码(主要为了适配前端) * fix: 修复主题色无法自适应的bug 还原了主题色修改函数的write参数,去除该参数会导致自适应修改主题时也会写入localStorage * feat: 自动修改标签的for属性 自动修改”获取 IP 方式“标签的for属性,保证其始终指向当前可编辑的元素 * refactor: 去除JQuery * refactor: 按业务划分代码 * fix: 对空值兼容 * fix: 统一html命名 * feat: 默认选择最后一个配置 * fix: 删除冗余代码 * fix: Update writing.html
45 lines
904 B
Go
45 lines
904 B
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
// MemoryLogs 内存中的日志
|
|
type MemoryLogs struct {
|
|
MaxNum int // 保存最大条数
|
|
Logs []string // 日志
|
|
}
|
|
|
|
func (mlogs *MemoryLogs) Write(p []byte) (n int, err error) {
|
|
mlogs.Logs = append(mlogs.Logs, string(p))
|
|
// 处理日志数量
|
|
if len(mlogs.Logs) > mlogs.MaxNum {
|
|
mlogs.Logs = mlogs.Logs[len(mlogs.Logs)-mlogs.MaxNum:]
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
var mlogs = &MemoryLogs{MaxNum: 50}
|
|
|
|
// 初始化日志
|
|
func init() {
|
|
log.SetOutput(io.MultiWriter(mlogs, os.Stdout))
|
|
// log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
|
}
|
|
|
|
// Logs web
|
|
func Logs(writer http.ResponseWriter, request *http.Request) {
|
|
// mlogs.Logs数组转为json
|
|
logs, _ := json.Marshal(mlogs.Logs)
|
|
writer.Write(logs)
|
|
}
|
|
|
|
// ClearLog
|
|
func ClearLog(writer http.ResponseWriter, request *http.Request) {
|
|
mlogs.Logs = mlogs.Logs[:0]
|
|
}
|