Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b6ba3486d6 | |||
| f59e909d30 | |||
| 3bb47de45a | |||
| c71973850e | |||
| 65f062b860 |
+11
-7
@@ -357,14 +357,18 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
//是否展示日志
|
//是否展示日志
|
||||||
if that.WebConnectLog != nil {
|
if that.WebConnectLog != nil {
|
||||||
ipStr := Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))
|
|
||||||
//负载均衡优化
|
//负载均衡优化
|
||||||
if ipStr == "127.0.0.1" {
|
ipStr := ""
|
||||||
if req.Header.Get("X-Forwarded-For") != "" {
|
if req.Header.Get("X-Forwarded-For") != "" {
|
||||||
ipStr = req.Header.Get("X-Forwarded-For")
|
ipStr = req.Header.Get("X-Forwarded-For")
|
||||||
} else if req.Header.Get("X-Real-IP") != "" {
|
} else if req.Header.Get("X-Real-IP") != "" {
|
||||||
ipStr = req.Header.Get("X-Real-IP")
|
ipStr = req.Header.Get("X-Real-IP")
|
||||||
}
|
}
|
||||||
|
//负载均衡优化
|
||||||
|
if ipStr == "" {
|
||||||
|
//RemoteAddr := that.Req.RemoteAddr
|
||||||
|
ipStr = Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))
|
||||||
}
|
}
|
||||||
|
|
||||||
that.WebConnectLog.Infoln(ipStr, context.Req.Method,
|
that.WebConnectLog.Infoln(ipStr, context.Req.Method,
|
||||||
|
|||||||
+10
-7
@@ -75,14 +75,17 @@ func (that *Context) View() {
|
|||||||
if that.Session("user_id").Data != nil {
|
if that.Session("user_id").Data != nil {
|
||||||
that.Log["user_id"] = that.Session("user_id").ToCeilInt()
|
that.Log["user_id"] = that.Session("user_id").ToCeilInt()
|
||||||
}
|
}
|
||||||
ipStr := Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":"))
|
|
||||||
//负载均衡优化
|
//负载均衡优化
|
||||||
if ipStr == "127.0.0.1" {
|
ipStr := ""
|
||||||
if that.Req.Header.Get("X-Forwarded-For") != "" {
|
if that.Req.Header.Get("X-Forwarded-For") != "" {
|
||||||
ipStr = that.Req.Header.Get("X-Forwarded-For")
|
ipStr = that.Req.Header.Get("X-Forwarded-For")
|
||||||
} else if that.Req.Header.Get("X-Real-IP") != "" {
|
} else if that.Req.Header.Get("X-Real-IP") != "" {
|
||||||
ipStr = that.Req.Header.Get("X-Real-IP")
|
ipStr = that.Req.Header.Get("X-Real-IP")
|
||||||
}
|
}
|
||||||
|
//负载均衡优化
|
||||||
|
if ipStr == "" {
|
||||||
|
//RemoteAddr := that.Req.RemoteAddr
|
||||||
|
ipStr = Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":"))
|
||||||
}
|
}
|
||||||
that.Log["ip"] = ipStr
|
that.Log["ip"] = ipStr
|
||||||
that.Db.Insert("logs", that.Log)
|
that.Db.Insert("logs", that.Log)
|
||||||
|
|||||||
+3
-12
@@ -165,11 +165,8 @@ func (that *HotimeDBBuilder) And(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if that.lastWhere != nil {
|
if that.lastWhere != nil {
|
||||||
if that.lastWhere["AND"] != nil {
|
|
||||||
where["AND"] = that.lastWhere["AND"]
|
|
||||||
}
|
|
||||||
that.lastWhere["AND"] = where
|
that.lastWhere["AND"] = where
|
||||||
//that.lastWhere = where
|
that.lastWhere = where
|
||||||
return that
|
return that
|
||||||
}
|
}
|
||||||
that.lastWhere = where
|
that.lastWhere = where
|
||||||
@@ -199,11 +196,8 @@ func (that *HotimeDBBuilder) Or(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if that.lastWhere != nil {
|
if that.lastWhere != nil {
|
||||||
if that.lastWhere["OR"] != nil {
|
|
||||||
where["OR"] = that.lastWhere["OR"]
|
|
||||||
}
|
|
||||||
that.lastWhere["OR"] = where
|
that.lastWhere["OR"] = where
|
||||||
//that.lastWhere = where
|
that.lastWhere = where
|
||||||
return that
|
return that
|
||||||
}
|
}
|
||||||
that.lastWhere = where
|
that.lastWhere = where
|
||||||
@@ -234,11 +228,8 @@ func (that *HotimeDBBuilder) Where(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if that.lastWhere != nil {
|
if that.lastWhere != nil {
|
||||||
if that.lastWhere["AND"] != nil {
|
|
||||||
where["AND"] = that.lastWhere["AND"]
|
|
||||||
}
|
|
||||||
that.lastWhere["AND"] = where
|
that.lastWhere["AND"] = where
|
||||||
//that.lastWhere = where
|
that.lastWhere = where
|
||||||
return that
|
return that
|
||||||
}
|
}
|
||||||
that.lastWhere = where
|
that.lastWhere = where
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ func Down(url, path, name string, e ...*Error) bool {
|
|||||||
}
|
}
|
||||||
out, err := os.Create(path + name)
|
out, err := os.Create(path + name)
|
||||||
|
|
||||||
if err != nil && e[0] != nil {
|
if err != nil && len(e) != 0 {
|
||||||
e[0].SetError(err)
|
e[0].SetError(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil && e[0] != nil {
|
if err != nil && len(e) != 0 {
|
||||||
e[0].SetError(err)
|
e[0].SetError(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ func Down(url, path, name string, e ...*Error) bool {
|
|||||||
|
|
||||||
pix, err := ioutil.ReadAll(resp.Body)
|
pix, err := ioutil.ReadAll(resp.Body)
|
||||||
_, err = io.Copy(out, bytes.NewReader(pix))
|
_, err = io.Copy(out, bytes.NewReader(pix))
|
||||||
if err != nil && e[0] != nil {
|
if err != nil && len(e) != 0 {
|
||||||
e[0].SetError(err)
|
e[0].SetError(err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-11
@@ -8,7 +8,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mongoDb struct {
|
type MongoDb struct {
|
||||||
Client *mongo.Client
|
Client *mongo.Client
|
||||||
Ctx context.Context
|
Ctx context.Context
|
||||||
DataBase *mongo.Database
|
DataBase *mongo.Database
|
||||||
@@ -16,12 +16,12 @@ type mongoDb struct {
|
|||||||
LastErr error
|
LastErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(database, url string) (*mongoDb, error) {
|
func GetMongoDb(database, url string) (*MongoDb, error) {
|
||||||
db := mongoDb{}
|
db := MongoDb{}
|
||||||
clientOptions := options.Client().ApplyURI(url)
|
clientOptions := options.Client().ApplyURI(url)
|
||||||
|
|
||||||
db.Ctx = context.TODO()
|
db.Ctx = context.TODO()
|
||||||
// Connect to MongoDB
|
// Connect to MongoDb
|
||||||
var err error
|
var err error
|
||||||
db.Client, err = mongo.Connect(db.Ctx, clientOptions)
|
db.Client, err = mongo.Connect(db.Ctx, clientOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -32,7 +32,7 @@ func Init(database, url string) (*mongoDb, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fmt.Println("Connected to MongoDB!")
|
fmt.Println("Connected to MongoDb!")
|
||||||
//databases, err := db.Client.ListDatabaseNames(db.Ctx, bson.M{})
|
//databases, err := db.Client.ListDatabaseNames(db.Ctx, bson.M{})
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// return nil, err
|
// return nil, err
|
||||||
@@ -43,7 +43,7 @@ func Init(database, url string) (*mongoDb, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that *mongoDb) Insert(table string, data interface{}) string {
|
func (that *MongoDb) Insert(table string, data interface{}) string {
|
||||||
collection := that.DataBase.Collection(table)
|
collection := that.DataBase.Collection(table)
|
||||||
re, err := collection.InsertOne(that.Ctx, data)
|
re, err := collection.InsertOne(that.Ctx, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -53,7 +53,7 @@ func (that *mongoDb) Insert(table string, data interface{}) string {
|
|||||||
return ObjToStr(re.InsertedID)
|
return ObjToStr(re.InsertedID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that *mongoDb) InsertMany(table string, data ...interface{}) Slice {
|
func (that *MongoDb) InsertMany(table string, data ...interface{}) Slice {
|
||||||
collection := that.DataBase.Collection(table)
|
collection := that.DataBase.Collection(table)
|
||||||
re, err := collection.InsertMany(that.Ctx, data)
|
re, err := collection.InsertMany(that.Ctx, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,7 +65,7 @@ func (that *mongoDb) InsertMany(table string, data ...interface{}) Slice {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that *mongoDb) Update(table string, data Map, where Map) int64 {
|
func (that *MongoDb) Update(table string, data Map, where Map) int64 {
|
||||||
collection := that.DataBase.Collection(table)
|
collection := that.DataBase.Collection(table)
|
||||||
re, err := collection.UpdateMany(that.Ctx, where, data)
|
re, err := collection.UpdateMany(that.Ctx, where, data)
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ func (that *mongoDb) Update(table string, data Map, where Map) int64 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that *mongoDb) Delete(table string, where Map) int64 {
|
func (that *MongoDb) Delete(table string, where Map) int64 {
|
||||||
collection := that.DataBase.Collection(table)
|
collection := that.DataBase.Collection(table)
|
||||||
re, err := collection.DeleteMany(that.Ctx, where)
|
re, err := collection.DeleteMany(that.Ctx, where)
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ func (that *mongoDb) Delete(table string, where Map) int64 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that *mongoDb) Get(table string, where Map) Map {
|
func (that *MongoDb) Get(table string, where Map) Map {
|
||||||
results := []Map{}
|
results := []Map{}
|
||||||
var cursor *mongo.Cursor
|
var cursor *mongo.Cursor
|
||||||
var err error
|
var err error
|
||||||
@@ -119,7 +119,7 @@ func (that *mongoDb) Get(table string, where Map) Map {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that mongoDb) Select(table string, where Map, page, pageRow int64) []Map {
|
func (that MongoDb) Select(table string, where Map, page, pageRow int64) []Map {
|
||||||
page = (page - 1) * pageRow
|
page = (page - 1) * pageRow
|
||||||
if page < 0 {
|
if page < 0 {
|
||||||
page = 0
|
page = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user