1
0
Fork 0
cloudservices/src/go/storage.go

100 lines
2.6 KiB
Go
Raw Normal View History

2019-12-18 23:47:00 -05:00
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v7"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io/ioutil"
2019-12-20 12:25:35 -05:00
"time"
"strings"
2019-12-18 23:47:00 -05:00
)
// Collection the MongoDB Account collection
var Collection *mongo.Collection
// RedisClient the Redis client
var RedisClient *redis.Client
// Account represents an user's account.
type Account struct {
Username string `json:"username"`
UserID string `json:"userID"`
EmailAddress string `json:"emailAddress"`
CreatedBy string `json:"createdBy"`
CreatedAt time.Time `json:"createdAt"`
Locked bool `json:"locked"`
}
// HandleError This function panics if an error exists.
func HandleError(e error, serv int) {
if e != nil && serv == 1 {
panic(e)
} else if e != nil && serv == 0 {
fmt.Println(e)
}
}
func main() {
type Config struct {
MongoDB string `json:"mongoURL"`
}
config := &Config{}
file, err := ioutil.ReadFile("../config.json")
HandleError(err, 1)
err = json.Unmarshal(file, &config)
client, err := mongo.NewClient(options.Client().ApplyURI(config.MongoDB))
HandleError(err, 1)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
HandleError(err, 1)
2019-12-19 00:02:59 -05:00
err = client.Ping(context.TODO(), nil)
fmt.Printf("Connected to MongoDB [GO]\n")
2019-12-18 23:47:00 -05:00
HandleError(err, 1)
2019-12-20 12:25:35 -05:00
Collection = client.Database("cloudservices").Collection("accounts")
2019-12-18 23:47:00 -05:00
RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
2019-12-19 00:02:59 -05:00
_, err = RedisClient.Ping().Result()
fmt.Printf("Connected to Redis [GO]\n")
2019-12-18 23:47:00 -05:00
HandleError(err, 1)
for {
2019-12-20 15:53:59 -05:00
fmt.Printf("Calling handler func [GO]\n")
2019-12-18 23:47:00 -05:00
handler()
2019-12-20 15:53:59 -05:00
time.Sleep(1000000 * time.Millisecond)
2019-12-18 23:47:00 -05:00
}
}
func handler() {
2019-12-20 12:25:35 -05:00
cur, err := Collection.Find(context.TODO(), bson.D{})
2019-12-18 23:47:00 -05:00
HandleError(err, 0)
for cur.Next(context.TODO()) {
2019-12-19 00:02:59 -05:00
go checkAccountSizeAndUpdate(cur.Current.Lookup("username").String(), cur.Current.Lookup("id").String())
fmt.Printf("Checking account information for %s\n", cur.Current.Lookup("username").String())
2019-12-20 15:53:59 -05:00
time.Sleep(600000 * time.Millisecond)
2019-12-18 23:47:00 -05:00
}
}
func checkAccountSizeAndUpdate(username string, id string) {
2019-12-20 12:25:35 -05:00
var size float64 = 0
var userHomeDirectory string = strings.Replace(strings.Join([]string{"/home/", string(username)}, ""), "\"", "", -1)
fmt.Println(userHomeDirectory)
sizeHome := DirSize(&userHomeDirectory)
size += sizeHome
sizeMail := DirSize(&userHomeDirectory)
size += sizeMail
RedisClient.Set("storage"+"-"+string(id), size, 0)
fmt.Printf("Set Call | Username: %v, ID: %v | Bytes: %f\n", string(username), string(id), size)
2019-12-18 23:47:00 -05:00
}