Files
waf-platform/EdgeAdmin/internal/web/actions/default/servers/accesslogs/test.go
2026-02-04 20:27:13 +08:00

82 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package accesslogs
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/accesslogs/policyutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
"strconv"
"time"
)
type TestAction struct {
actionutils.ParentAction
}
func (this *TestAction) Init() {
this.Nav("", "", "test")
}
func (this *TestAction) RunGet(params struct {
PolicyId int64
}) {
err := policyutils.InitPolicy(this.Parent(), params.PolicyId)
if err != nil {
this.ErrorPage(err)
return
}
var now = time.Now()
var uri = "/doc.html"
testJSON, err := json.MarshalIndent(&pb.HTTPAccessLog{
RequestId: types.String(time.Now().UnixMilli()) + "1" + strconv.Itoa(1),
UserAgent: this.Request.UserAgent(),
Request: "GET " + uri + " HTTP/1.1",
RequestPath: uri,
Referer: "https://example.com/",
RemoteAddr: "8.8.8.8",
Timestamp: now.Unix(),
TimeISO8601: now.Format("2006-01-02T15:04:05.000Z07:00"),
TimeLocal: now.Format("2/Jan/2006:15:04:05 -0700"),
}, "", " ")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["testJSON"] = string(testJSON)
this.Show()
}
func (this *TestAction) RunPost(params struct {
PolicyId int64
BodyJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo(codes.HTTPAccessLogPolicy_LogTestHTTPAccessLogPolicy, params.PolicyId)
var accessLog = &pb.HTTPAccessLog{}
err := json.Unmarshal(params.BodyJSON, accessLog)
if err != nil {
this.Fail("发送内容不是有效的JSON" + err.Error())
}
_, err = this.RPC().HTTPAccessLogPolicyRPC().WriteHTTPAccessLogPolicy(this.AdminContext(), &pb.WriteHTTPAccessLogPolicyRequest{
HttpAccessLogPolicyId: params.PolicyId,
HttpAccessLog: accessLog,
})
if err != nil {
this.Fail("发送失败:" + err.Error())
return
}
this.Success()
}