From aa8a25ef4326a8d5099fc5f8b32541ab2d744c3e Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Fri, 27 May 2022 18:27:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=B2=BE=E7=A1=AE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- api/attach.go | 2 +- api/get_record.go | 36 ++++++++++++++++++++++++++++++++++++ dao/dao.go | 12 +++++++++--- response/response.go | 9 ++++++++- routers/routes.go | 5 +++-- util/util.go | 5 ----- 7 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 api/get_record.go diff --git a/README.md b/README.md index 085e876..b9132ed 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ ## TODO -- [ ] 添加按Key名查找接口 +- [x] 添加按Key名查找接口 - [ ] 添加模糊查询接口 \ No newline at end of file diff --git a/api/attach.go b/api/attach.go index d146fe0..c837537 100644 --- a/api/attach.go +++ b/api/attach.go @@ -8,7 +8,7 @@ import ( "zggsong.cn/sandstone/server/util" ) -func ListAttachments(ctx *gin.Context) { +func Test(ctx *gin.Context) { var datas []response.Data var data response.Data var arrayData = dao.ListAttach() diff --git a/api/get_record.go b/api/get_record.go new file mode 100644 index 0000000..091d6ca --- /dev/null +++ b/api/get_record.go @@ -0,0 +1,36 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "net/http" + "zggsong.cn/sandstone/server/dao" + "zggsong.cn/sandstone/server/response" + "zggsong.cn/sandstone/server/util" +) + +func GetRecord(ctx *gin.Context) { + keyName := ctx.GetHeader("keyName") + record := dao.GetRecord(keyName) + if record.KeyName == "" { + ctx.JSON(http.StatusOK, response.Response{ + Code: http.StatusOK, + Message: "没有数据", + Data: nil, + }) + return + } + var data response.Data + data.KeyName = record.KeyName + data.Size = record.Size + data.CreateTime = record.CreateTime + data.UploadTime = record.UploadTime + data.Line = record.Line + data.Machine = record.Machine + data.Url = util.GetUrl(record.KeyName) + data.Tags = util.GetTags(record.KeyName) + ctx.JSON(http.StatusOK, response.GetResponse{ + Code: 200, + Message: "success", + Data: data, + }) +} diff --git a/dao/dao.go b/dao/dao.go index ea0e93e..ff82567 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -3,12 +3,11 @@ package dao import ( "context" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "log" "zggsong.cn/sandstone/server/global" "zggsong.cn/sandstone/server/models" - - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" ) const ( @@ -103,3 +102,10 @@ func Find(limit, page int64) (data []models.Record, total int64, err error) { } return } + +//GetRecord 精确查询单条记录 +func GetRecord(KeyName string) (record models.Record) { + filter := bson.D{{"keyname", KeyName}} + _ = global.GLO_COLLECTION.FindOne(context.TODO(), filter).Decode(&record) + return +} diff --git a/response/response.go b/response/response.go index 2db2692..1d9c571 100644 --- a/response/response.go +++ b/response/response.go @@ -7,7 +7,15 @@ type Response struct { Total int64 `json:"total"` Page int64 `json:"page"` } + +type GetResponse struct { + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data"` +} + type Data struct { + Size string `json:"size"` KeyName string `json:"keyName"` Line string `json:"line"` Machine string `json:"machine"` @@ -15,7 +23,6 @@ type Data struct { UploadTime string `json:"uploadTime"` Url string `json:"url"` - Size string `json:"size"` Tags []Tag `json:"tags"` } type Tag struct { diff --git a/routers/routes.go b/routers/routes.go index e3a370e..afb73a4 100644 --- a/routers/routes.go +++ b/routers/routes.go @@ -6,7 +6,8 @@ import ( ) func CollectRoute(r *gin.Engine) *gin.Engine { - //r.GET("/api/v1/listAll", api.ListAttachments) - r.GET("/api/v1/records", api.ListRecords) + r.GET("/test", api.Test) + r.GET("/v1/records", api.ListRecords) + r.GET("/v1/record", api.GetRecord) return r } diff --git a/util/util.go b/util/util.go index 38e52de..8bb5651 100644 --- a/util/util.go +++ b/util/util.go @@ -28,11 +28,6 @@ func getClient() *s3.Client { cfg, err := config.LoadDefaultConfig( context.TODO(), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")), - // 已弃用 - // config.WithEndpointResolver( - // aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) { - // return aws.Endpoint{URL: endpoint}, nil - // })), config.WithEndpointResolverWithOptions( aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { return aws.Endpoint{URL: endPoint}, nil