-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
83 lines (67 loc) · 1.82 KB
/
app.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/alinowrouzii/educational-management-system/routers"
"github.com/alinowrouzii/educational-management-system/token"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
type App struct {
Router *mux.Router
DB *sql.DB
wantsToDropDatabase bool
jwt *token.JWTMaker
}
func (a *App) dropAndCreateDatabase(connectionString, dbName string) {
db, err := sql.Open("mysql", connectionString)
if err != nil {
panic(err)
}
defer db.Close()
_, err = db.Exec("DROP DATABASE final_db")
if err != nil {
panic(err)
}
createDBStmt := fmt.Sprintf("CREATE DATABASE %s DEFAULT CHARACTER SET = 'utf8mb4'", dbName)
_, err = db.Exec(createDBStmt)
if err != nil {
panic(err)
}
fmt.Println("Database created successfully")
}
func (a *App) connectDB(user, password, dbName string) {
connectionString := fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/", user, password)
if a.wantsToDropDatabase {
fmt.Println("hereee")
a.dropAndCreateDatabase(connectionString, dbName)
}
connectionString += dbName + "?parseTime=true"
dbConn, err := sql.Open("mysql", connectionString)
if err != nil {
log.Fatal(err)
}
a.DB = dbConn
}
func (a *App) Initialize(user, password, dbname, secretKey string) {
a.connectDB(user, password, dbname)
var err error
a.jwt, err = token.NewJWTMaker(secretKey, a.DB)
if err != nil {
log.Fatal("error occured in jwt initialization")
log.Fatal(err)
}
a.Router = mux.NewRouter()
routers.InitRouter(a.Router, a.DB, a.jwt)
}
func (a *App) Run(addr string, wantsToWriteData, wantsToMakeMigrations bool) {
if a.wantsToDropDatabase || wantsToMakeMigrations {
MakeMigrations(a.DB)
}
if wantsToWriteData {
WriteDataToDatabsae(a.DB)
}
log.Fatal(http.ListenAndServe(addr, a.Router))
}