-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
159 lines (142 loc) · 4.28 KB
/
app.go
File metadata and controls
159 lines (142 loc) · 4.28 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
)
// App struct
type App struct {
ctx context.Context
databaseHash map[string]Database
localDb SqlLiteDatabase
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
// Perform your setup here
a.ctx = ctx
a.databaseHash = make(map[string]Database)
// Connect to the SQLite database.
// Initialize SQLite db if it isn't already initialized.
appDataDir := filepath.Join(os.Getenv("APPDATA"), "dbAdmin")
_, err := os.Stat(appDataDir)
if os.IsNotExist(err) {
// 0755 means that owner an read/write/execute but all other applications can only read.
err := os.MkdirAll(appDataDir, 0755)
if err != nil {
log.Fatalf("Unable to create app data directory.\n%e\n", err)
}
}
// Create the db file if it doesn't exists.
databasePath := filepath.Join(appDataDir, "data.db")
_, err = os.Stat(databasePath)
if os.IsNotExist(err) {
_, err = os.Create(databasePath)
if err != nil {
log.Fatalf("Unable to find or create app data db.\n%e\n", err)
}
}
localDb, err := sql.Open("sqlite3", databasePath)
if err != nil {
log.Fatalf("Unable to connect to app data db.\n%e\n", err)
}
a.localDb = SqlLiteDatabase{
connection: localDb,
ctx: a.ctx,
}
}
// domReady is called after front-end resources have been loaded
func (a App) domReady(ctx context.Context) {
log.Println("######### THE DOM IS READY #########")
}
// beforeClose is called when the application is about to quit,
// either by clicking the window close button or calling runtime.Quit.
// Returning true will cause the application to continue, false will continue shutdown as normal.
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
for _, connection := range a.databaseHash {
err := connection.Disconnect()
if err != nil {
log.Println(err)
}
}
return false
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
// Perform your teardown here
}
// Registers a database connection in memory.
//
// Later on this will also register into a SQLite local DB to save credentials and connection information.
func (a *App) RegisterDatabase(server string, database string, driver string, username string, password string) string {
databaseKey := fmt.Sprintf("%s:%s", server, database)
if _, ok := a.databaseHash[databaseKey]; ok {
return fmt.Sprintf("%s has already been registered.", databaseKey)
}
var connection Database
switch driver {
case "mssql":
connection = &MsSqlDatabase{
server: server,
database: database,
username: username,
password: password,
ctx: a.ctx,
sqlite: &a.localDb,
}
case "mongo":
connection = &MongoDatabase{
server: server,
username: username,
password: password,
ctx: a.ctx,
sqlite: &a.localDb,
}
default:
return fmt.Sprintf("Invalid driver was selected: %s\n", driver)
}
if err := connection.Initialize(); err != nil {
return fmt.Sprintf("There was an error connecting to the database.\n%e\n", err)
}
a.databaseHash[databaseKey] = connection
return "Successfully connected to the database."
}
func (a *App) GetUsers(databaseKey string, target string) (string, error) {
db, ok := a.databaseHash[databaseKey]
if !ok {
return "", fmt.Errorf("%s has not been registered yet", databaseKey)
}
queryResult, err := db.FindUsers(target)
if err != nil {
return "", fmt.Errorf("an error occurred while collecting users\n%s", err)
}
output, err := json.Marshal(queryResult)
return string(output), err
}
func (a *App) GetUserPermissions(databaseKey string, user string, target string) (string, error) {
db, ok := a.databaseHash[databaseKey]
if !ok {
return "", fmt.Errorf("%s has not been registered yet", databaseKey)
}
queryResult, err := db.FindUserPermissions(user, target)
if err != nil {
return "", fmt.Errorf("an error occurred while collecting user permissions\n%s", err)
}
output, err := json.Marshal(queryResult)
return string(output), err
}
func (a *App) GetConnections() []string {
output := make([]string, 0, len(a.databaseHash))
for k := range a.databaseHash {
output = append(output, k)
}
return output
}