json接口接收日志,apikey校验

This commit is contained in:
gotoeasy 2022-07-04 17:10:57 +08:00
parent cdc1beb8ff
commit 24deb135af
6 changed files with 66 additions and 26 deletions

View File

@ -83,3 +83,7 @@ func (r *HttpRequest) RequestURI() string {
func (r *HttpRequest) RequestUrlPath() string {
return r.ginCtx.Request.URL.Path
}
func (r *HttpRequest) BindJSON(obj any) error {
return r.ginCtx.BindJSON(obj)
}

View File

@ -70,10 +70,26 @@ func (s *LogDataStorageHandle) AddTextLog(date string, logText string, system st
}
}
// // 添加日志参数是LogDataModel形式的json字符串
// func (s *LogDataStorage) AddJsonLog(logJson string) {
// s.storage.Add(logJson)
// }
// 添加日志参数LogDataModel
func (s *LogDataStorageHandle) AddLogDataModel(data *logdata.LogDataModel) {
ary := strings.Split(data.Text, "\n")
data.Text = strings.TrimSpace(ary[0])
if len(ary) > 1 {
data.Detail = data.Text
}
if s.storage.IsClose() {
s.storage = logdata.NewLogDataStorage(s.storage.StoreName(), "data")
}
err := s.storage.Add(data)
if err != nil {
log.Println("竟然失败,再来一次", s.storage.IsClose(), err)
if s.storage.IsClose() {
s.storage = logdata.NewLogDataStorage(s.storage.StoreName(), "data")
}
s.storage.Add(data)
}
}
// 取日志(文档)
func (s *LogDataStorageHandle) GetLogDataDocument(id uint32) *logdata.LogDataDocument {

View File

@ -18,7 +18,6 @@ func Run() {
contextPath := conf.GetContextPath() // ContextPath
// 过滤器
gweb.RegisterFilter(filter.ApiKeyFilter)
gweb.RegisterFilter(filter.CrossFilter)
// 控制器(跳转)
@ -34,8 +33,11 @@ func Run() {
gweb.RegisterController(method.GET, "/**/*.png", html.StaticFileController)
// 控制器
gweb.RegisterController(method.POST, contextPath+"/search", controller.LogSearchController)
gweb.RegisterController(method.POST, contextPath+"/add", controller.LogAddController)
gweb.RegisterController(method.POST, contextPath+"/search", controller.LogSearchController) // Deprecated
gweb.RegisterController(method.POST, contextPath+"/add", controller.LogAddController) // Deprecated
gweb.RegisterController(method.POST, contextPath+"/v1/log/add", controller.JsonLogAddController)
gweb.RegisterController(method.POST, contextPath+"/v1/log/search", controller.LogSearchController)
// 默认引擎空转一下,触发未建索引继续建
go ldb.NewDefaultEngine().AddTextLog("", "", "")

View File

@ -3,10 +3,22 @@ package controller
import (
"glc/gweb"
"glc/ldb"
"glc/ldb/conf"
"glc/ldb/storage/logdata"
"log"
)
// 添加日志(表单提交方式)
func LogAddController(req *gweb.HttpRequest) *gweb.HttpResult {
// 开启API秘钥校验时才检查
if conf.IsEnableSecurityKey() {
auth := req.GetHeader(conf.GetHeaderSecurityKey())
if auth != conf.GetSecurityKey() {
return gweb.Error(403, "未经授权的访问,拒绝服务")
}
}
storeNmae := req.GetFormParameter("name")
text := req.GetFormParameter("text")
date := req.GetFormParameter("date")
@ -16,3 +28,26 @@ func LogAddController(req *gweb.HttpRequest) *gweb.HttpResult {
engine.AddTextLog(date, text, system)
return gweb.Ok()
}
// 添加日志JSON提交方式
func JsonLogAddController(req *gweb.HttpRequest) *gweb.HttpResult {
// 开启API秘钥校验时才检查
if conf.IsEnableSecurityKey() {
auth := req.GetHeader(conf.GetHeaderSecurityKey())
if auth != conf.GetSecurityKey() {
return gweb.Error(403, "未经授权的访问,拒绝服务")
}
}
md := &logdata.LogDataModel{}
err := req.BindJSON(md)
if err != nil {
log.Println("请求参数有误", err)
return gweb.Error500(err.Error())
}
engine := ldb.NewDefaultEngine()
engine.AddTextLog(md.Date, md.Text, md.System)
return gweb.Ok()
}

View File

@ -2,27 +2,10 @@ package filter
import (
"glc/gweb"
"glc/ldb/conf"
"net/http"
)
// 校验HEADER的API秘钥
func ApiKeyFilter(req *gweb.HttpRequest) *gweb.HttpResult {
//log.Println("================================", req.RequestURI())
// 开启API秘钥校验时才检查
if !conf.IsEnableSecurityKey() {
return nil
}
auth := req.GetHeader(conf.GetHeaderSecurityKey())
if auth != conf.GetSecurityKey() {
return gweb.Error(403, "未经授权的访问,拒绝服务")
}
return nil // 返回nil表示正常过滤成功
}
// 校验HEADER的API秘钥
// 允许跨域
func CrossFilter(req *gweb.HttpRequest) *gweb.HttpResult {
req.SetHeader("Access-Control-Allow-Origin", "*")

View File

@ -27,7 +27,7 @@ export default {
for(let k in params){
formData.append(k, params[k]);
}
return post(`${BASE_URL}/glc/search`, formData)
return post(`${BASE_URL}/glc/v1/log/search`, formData)
},
// remove(db, id) {
// return request(`/remove?database=${db}`, 'post', { id })