Commit 78fa6d17 by 孙香冬

no message

parent 9a0f9fdb
......@@ -4,5 +4,6 @@ go 1.16
require (
github.com/gin-gonic/gin v1.8.1 // indirect
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 // indirect
go.mongodb.org/mongo-driver v1.11.0 // indirect
)
......@@ -5,6 +5,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
......
[ERROR]2022/11/17 13:54:26 logger.go:54: ------ErrorLog
[ERROR]2022/11/17 15:08:01 logger.go:54: ------ErrorLog
[ERROR]2022/11/17 15:20:13 logger.go:54: ------ErrorLog
[ERROR]2022/11/17 15:20:55 logger.go:54: ------ErrorLog
[ERROR]2022/11/17 15:22:39 logger.go:54: ------ErrorLog
package main
import (
"gaoqiaoCJY/src/config"
"gaoqiaoCJY/src/server"
"gaoqiaoCJY/src/db"
)
func main() {
config.InitConfig()
//xml do
server.CreateHttpServer()
//http
//异常
//log
// logger.ErrorLog("------ErrorLog") //log
// config.InitConfig() //xml do
// server.CreateHttpServer() //http do
// errors.Wrap(err, "自定义文本") //异常
// errors.New("自定义文本")
db.Init()
// db.GetList(bson.M{"level": 55})
//mongodb
//excel
//中间件
......
<config>
<port>30088</port>
<port>:30088</port>
<dbConfig>
<mongodbConnectionStr>
http://sfsdfasdfasfasd
......
......@@ -31,7 +31,7 @@ type Objs interface {
}
var (
url = "mongodb://root:root@127.0.0.1:27017" // 带账号名的链接
url = "mongodb://root:root@127.0.0.1:27017/?connect=direct;authSource=admin" // 带账号名的链接
DB_NAME = "gaoqiaoDB" // 数据库名
maxTime = time.Duration(5) // 链接超时时间
DB_CLIENT_MAX_NUM uint64 = 100 // 链接数
......@@ -72,7 +72,7 @@ func ConnectToDB(uri, name string, timeout time.Duration, num uint64) (*mongo.Da
return client.Database(name), nil
}
func init() {
func Init() {
var err error
collection_map = make(map[string]*mongo.Collection)
......
......@@ -9,7 +9,7 @@ import (
)
type Entrepreneurship struct {
id int32 `json:"id"`
_id int32 `json:"_id"`
typeName string `json:"typeName"`
question string `json:"question"`
A string `json:"A"`
......
package db
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mgoCli *mongo.Client
func initDb() {
var err error
credential := options.Credential{
AuthMechanism: "SCRAM-SHA-1", // 阿里云服务的
Username: "root", // mongodb用户名
Password: "root", //mongodb 密码
AuthSource: "admin", //默认admin不需要改
PasswordSet: true,
}
clientOpts := options.Client().ApplyURI("mongodb://localhost:3733").
SetAuth(credential)
//连接到MongoDB
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // 设置5s超时
defer cancel()
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
log.Fatal(err)
}
//检查连接
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
}
func MgoCli() *mongo.Client {
if mgoCli == nil {
initDb()
}
return mgoCli
}
// import (
// "context"
// "fmt"
// "log"
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
// )
// var client *mongo.Client
// func InitMongoDB() {
// // Rest of the code will go here
// // Set client options 设置连接参数
// clientOptions := options.Client().ApplyURI("mongodb://root:root@127.0.0.1:27017/?connect=direct;authSource=admin")
// // 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!")
// databases, err := client.ListDatabases(context.TODO(), bson.M{})
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(databases.TotalSize / 1024 / 1024 / 1024)
// err = client.Disconnect(context.TODO())
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println("Connection to MongoDB closed.")
// }
......@@ -20,7 +20,7 @@ var (
infoLogger *log.Logger
warningLogger *log.Logger
errorLogger *log.Logger
defaultLogFile = "./logs/web.log"
defaultLogFile = "./logs/error.log"
)
func init() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment