-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
103 lines (84 loc) · 2.9 KB
/
setup.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
package main
import (
"context"
"log"
"os"
"io/ioutil"
"encoding/json"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
mndb "github.com/VaradBelwalkar/Compute-Services/api/database_handling/mongodb"
"github.com/VaradBelwalkar/Compute-Services/api/database_handling/redis"
jwt "github.com/VaradBelwalkar/Compute-Services/api/auth_service/jwt"
"github.com/VaradBelwalkar/Compute-Services/api/auth_service/twofa"
containers "github.com/VaradBelwalkar/Compute-Services/api/query_handling/containers"
"github.com/VaradBelwalkar/Compute-Services/api/container_apis"
"github.com/VaradBelwalkar/Compute-Services/routes"
"github.com/VaradBelwalkar/Compute-Services/api/recovery"
"github.com/docker/docker/client"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
//const url = "mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS"
var mongoURL string
type dbConfig struct {
Name string `json:"name"`
Collections []string `json:"collections"`
}
type config struct {
Emails []string `json:"emails"`
Database dbConfig `json:"database"`
Port string `json:"server_port"`
Images []string `json:"images"`
}
func Setup_Env(){
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
mongoURL = os.Getenv("MONGODB_URI")
redis.Redis_URL = os.Getenv("REDIS_URL")
redis.Redis_Password = os.Getenv("REDIS_PASSWORD")
mndb.PassHashKey = os.Getenv("PASSWORD_HASH_SECRET")
jwt.JWTSigningKey = os.Getenv("JWT_SECRET")
twofa.Official_Email = os.Getenv("OFFICIAL_EMAIL")
twofa.Official_Email_Password = os.Getenv("OFFICIAL_EMAIL_APP_PASSWORD")
}
// The main function manages all the query handling and manages the database as well
func Setup() (*mux.Router,string){
Setup_Env()
//Initiate Mongo client
mongo_client, err := mongo.NewClient(options.Client().ApplyURI(mongoURL))
if err != nil {
log.Fatal("Please start MongoDB")
}
ctx := context.Background()
err = mongo_client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
// Initiate Docker client
containers.Cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic("Failed to create Docker Client, ensure that Docker Daemon is running\n")
}
//Get handler for the "user_details" collection (creates collection if not exists)
mndb.CollectionHandler,mndb.Sys_CollectionHandler=mndb.InitiateMongoDB(mongo_client);
redis.Initiate_Redis()
recovery.UpdateContainerStatus()
//login to be handled separatly
router:=routes.NewRouter()
//Get server port from configuration
// read the config file
data, err := ioutil.ReadFile("config.json")
if err != nil {
return nil,""
}
// unmarshal the JSON data into a Config struct
var config config
if err := json.Unmarshal(data, &config); err != nil {
return nil,""
}
container_apis.ImageArray = config.Images
return router,config.Port
}