82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
// 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()
|
||
}
|