-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (95 loc) · 2.91 KB
/
main.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
123
124
125
126
127
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/sswastik02/GoLibraryAPI/api"
database "github.com/sswastik02/GoLibraryAPI/database"
"github.com/sswastik02/GoLibraryAPI/models"
)
func main(){
// ---------------------------------- Command Line Arguments -------------------------------------
CREATEADMIN := flag.Bool("createadmin",false,"Create Admin User")
RUNSERVER := flag.Bool("runserver",false,"Run the server")
flag.Parse()
if !*CREATEADMIN && !*RUNSERVER {
log.Fatal(
"\n\nUsage : go run main.go -[COMMAND] or ./[binary] -[COMMAND]",
"\n\nPossible Commands are : ",
"\n runserver \t- To Run the Server",
"\n createadmin \t- To enter a admin into the database",
)
}
// ----------------------------------- Load the dotenv file -----------------
err:=godotenv.Load(".env")
if(err != nil){
log.Fatal("Could not import .env file")
}
// -------------------------------- Load Configuration from env file -----------------------
pgconfig:= &database.PGConfig{ // & creates a pointer to a structure with data as declared
Host: os.Getenv("DB_HOST") ,
Port: os.Getenv("DB_PORT"),
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASS"),
DBName:os.Getenv("DB_NAME"),
SSLMode:os.Getenv("DB_SSLMODE"),
}
rdconfig := database.RDConfig{
Addr: os.Getenv("REDIS_HOST")+":"+os.Getenv("REDIS_PORT"),
Password: os.Getenv("REDIS_PASS"),
}
api.InitializeJwtSecret(os.Getenv("JWT_SECRET"))
db, err := database.NewConnection(pgconfig)
if(err != nil){
log.Fatal("Could not load database")
}
rdb,err := database.CreateClient(&rdconfig)
defer rdb.Close()
if err != nil {
log.Fatal("Could not load redis : ",err.Error())
}
// -------------------------------- Migrate the Database -----------------------
err = models.Migrate(db)
if(err != nil){
log.Fatal("Could not Migrate Database")
}
// -----------------------------Initialise a Repository containing the Database--------------------
r := api.Repository{
DB: db,
Client: rdb,
}
//------------------------------------- Run Server and Create Admin ------------------------------------
if(*CREATEADMIN) {
createadmin(&r)
}
if(*RUNSERVER) {
runserver(&r)
}
}
func runserver(r *api.Repository) {
// ------------------------- Initialise Fiber Framework and routes using the repository-------------------
app := fiber.New()
r.SetupRoutes(app)
app.Listen(":8000")
}
func createadmin(r *api.Repository) {
var username string
var password string
fmt.Print("\nEnter username : ")
fmt.Scanf("%s",&username)
fmt.Print("\nEnter password : ")
fmt.Scanf("%s",&password)
fmt.Printf("\nCreating user with\n Username : %s \n Password : %s\n",username,password)
user := models.User{
Username: username,
Password: password,
Admin: true,
}
_, err := r.CreateUser(&user)
if err != nil {
log.Fatalf("\nCould not create Admin\n Error : %s",err.Error())
}
}