main2.go
1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
)
var (
client *mongo.Client
err error
result *mongo.InsertOneResult
collection *mongo.Collection
)
// @APIVersion 1.0.0
// @APITitle 乐游图后端接口文档
// @BasePath 正式 leyoutu.st-i.com.cn; 测试 letu.api.imagchina.com
func main() {
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
clientOptions.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的
clientOptions.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数
clientOptions.SetMaxPoolSize(4096) //使用最大的连接数
// Connect to MongoDB
client, err = mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
collection = client.Database("LeYouTu").Collection("LogRecord")
r := gin.Default()
r.GET("/AllScenic", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
//var aItems = DB.SItem{}
//if err = collection.FindOne(context.TODO(), bson.D{{}}).Decode(&aItems); err != nil {
// println(err)
//}
record := &LogRecord{
JobName: "job10",
Command: "echo hello",
Err: "",
Content: "hello",
}
if result, err = collection.InsertOne(context.TODO(), record); err != nil {
fmt.Println(err)
return
}
c.JSON(200, "ok")
})
r.Run(":8080")
}
type LogRecord struct {
JobName string `bson:"jobName"` // 任务名
Command string `bson:"command"` // shell命令
Err string `bson:"err"` // 脚本错误
Content string `bson:"content"` // 脚本输出
}