-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
122 lines (103 loc) · 2.64 KB
/
utils.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package utils
import (
models "github.com/AthanatiusC/godir/models"
"github.com/tomasen/realip"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"context"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
// "strings"
)
func WriteLog(req *http.Request, message string) {
// ip, Port := GetIPAdress(req)
ip := realip.FromRequest(req)
f, err := os.OpenFile("GoDir.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 777)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Println(" [ " + ip + "] " + message)
// log.Println(" [ " + ip + ":" + Port + " ] " + message)
}
func ConnectMongoDB() *mongo.Database {
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
iserror := ErrorHandler(err)
if iserror {
return nil
}
// Check theconnection
err = client.Ping(context.TODO(), nil)
iserror = ErrorHandler(err)
if iserror {
return nil
}
return client.Database("GoDir")
}
func ErrorHandler(err error) bool {
if err != nil {
fmt.Println(err)
return true
} else {
return false
}
}
func IsExists(path string) bool {
_, err := os.Stat(path)
ErrorHandler(err)
if os.IsNotExist(err) {
return false
}
return true
}
func VerifyOwnership(id primitive.ObjectID, auth_key string) bool {
var model models.Users
db := ConnectMongoDB()
db.Collection("users").FindOne(context.TODO(), bson.M{"_id": id}).Decode(&model)
if auth_key != "" {
if auth_key != model.Auth {
log.Println(auth_key + " " + model.Auth)
return false
} else if auth_key == model.Auth {
return true
}
}
return false
}
type Payload struct {
Message string `json:"message"`
Data interface{} `json:"returned_data"`
}
func WriteResult(req *http.Request, res http.ResponseWriter, data interface{}, message string) {
res.Header().Add("Access-Control-Allow-Origin", "*")
(res).Header().Set("Access-Control-Allow-Headers", "*")
(res).Header().Set("Access-Control-Allow-Methods", "*")
res.Header().Set("Content-Type", "Application/JSON")
var payload Payload
payload.Message = message
payload.Data = data
result, _ := json.Marshal(payload)
res.WriteHeader(http.StatusAccepted)
res.Write([]byte(result))
fmt.Println(message)
WriteLog(req, message)
}
func GetIPAdress(req *http.Request) (ip string, port string) {
ip, port, err := net.SplitHostPort(req.RemoteAddr)
ErrorHandler(err)
userIP := net.ParseIP(ip)
if userIP == nil {
return "Unknown", "0000"
} else if ip == "::1" {
ip = "localhost"
}
return ip, port
}