From c56f89298969b4fc9ef9f7681d2743e99b9dd8e4 Mon Sep 17 00:00:00 2001 From: Keyemail Date: Sun, 15 Sep 2024 05:23:22 -0700 Subject: [PATCH] Added Database --- api/BasicDashboard.go | 198 ++++++++++++++++++++++++++++++++++++++---- database/Database.go | 17 ++++ database/main.db | Bin 0 -> 28672 bytes go.mod | 1 + go.sum | 2 + main.go | 1 - 6 files changed, 200 insertions(+), 19 deletions(-) create mode 100644 database/Database.go diff --git a/api/BasicDashboard.go b/api/BasicDashboard.go index b302c30..00e8210 100644 --- a/api/BasicDashboard.go +++ b/api/BasicDashboard.go @@ -1,31 +1,193 @@ package api import ( + "log" "net/http" + "router-ui-backend/database" + "github.com/gin-gonic/gin" ) +type InternetSetup struct { + Online bool + Port int +} + +type WirelessSettings struct { + FiveSSIDEnabled bool + TwoSSIDEnabled bool +} + +type DeviceList struct { + DevicesOnline int +} + +type Qos struct { + Online bool +} + +type FileSharing struct { + Online bool +} + +type GuestNetwork struct { + Online bool +} + type OverviewItem struct { - InternetMode string `json:"internetMode"` - WifiMode5Ghz string `json:"wifiMode5Ghz"` - WifiMode2Ghz string `json:"wifiMode2Ghz"` - Devices int `json:"devices"` - FileShare string `json:"fileShare"` - Qos string `json:"qos"` - GuestNetwork string `json:"guestNetwork"` + InternetMode string `json:"internetMode"` + InternetPort string `json:"internetPort"` + InternetStatus string `json:"internetStatus"` + WifiMode5Ghz string `json:"wifiMode5Ghz"` + WifiMode2Ghz string `json:"wifiMode2Ghz"` + WifiModeStatus string `json:"wifiModeStatus"` + Devices int `json:"devices"` + QosMode string `json:"qosMode"` + QosStatus string `json:"qosStatus"` + FileShareMode string `json:"fileShareMode"` + FileShareStatus string `json:"fileShareStatus"` + GuestNetworkMode string `json:"guestNetworkMode"` + GuestNetworkStatus string `json:"guestNetworkStatus"` } -var overviewItems = OverviewItem{ - InternetMode: "LAN4", - WifiMode5Ghz: "On", - WifiMode2Ghz: "Off", - Devices: 0, - FileShare: "Offline", - Qos: "Online", - GuestNetwork: "Offline", -} - - func GetOverviewItems(context *gin.Context) { + db, err := database.OpenDatabase() + if err != nil { + log.Fatal(err) + } + defer db.Close() + + query := `SELECT online, port FROM InternetSetupBasic LIMIT 1` + + var internetSetup InternetSetup + + err = db.QueryRow(query).Scan(&internetSetup.Online, &internetSetup.Port) + if err != nil { + log.Fatal(err) + } + + internetPort := "" + internetMode := "Offline" + internetStatus := "badStatus" + + if internetSetup.Online { + internetMode = "Online:" + internetStatus = "goodStatus" + + switch internetSetup.Port { + case 0: + internetPort = "ISP Port" + case 1: + internetPort = "LAN1" + case 2: + internetPort = "LAN2" + case 3: + internetPort = "LAN3" + case 4: + internetPort = "LAN4" + } + } + + query = `SELECT fiveSSIDEnabled, twoSSIDEnabled FROM WirelessSettings LIMIT 1` + + var wirelessSettings WirelessSettings + + err = db.QueryRow(query).Scan(&wirelessSettings.FiveSSIDEnabled, &wirelessSettings.TwoSSIDEnabled) + if err != nil { + log.Fatal(err) + } + + wifiMode5Ghz := "Offline" + wifiMode2Ghz := "Offline" + wifiModeStatus := "badStatus" + + if wirelessSettings.TwoSSIDEnabled && wirelessSettings.FiveSSIDEnabled { + wifiMode5Ghz = "Online" + wifiMode2Ghz = "Online" + wifiModeStatus = "goodStatus" + } else if wirelessSettings.FiveSSIDEnabled && !(wirelessSettings.TwoSSIDEnabled) { + wifiMode5Ghz = "Online" + wifiModeStatus = "warningStatus" + } else if !(wirelessSettings.FiveSSIDEnabled) && wirelessSettings.TwoSSIDEnabled { + wifiMode2Ghz = "Online" + wifiModeStatus = "warningStatus" + } + + query = `SELECT devicesOnline FROM DeviceList LIMIT 1` + + var deviceList DeviceList + + err = db.QueryRow(query).Scan(&deviceList.DevicesOnline) + if err != nil { + log.Fatal(err) + } + + query = `SELECT online FROM QoS LIMIT 1` + + var qos Qos + + err = db.QueryRow(query).Scan(&qos.Online) + if err != nil { + log.Fatal(err) + } + + qosMode := "Offline" + qosStatus := "badStatus" + + if qos.Online { + qosMode = "Online" + qosStatus = "goodStatus" + } + + query = `SELECT online FROM FileSharing LIMIT 1` + + var fileSharing FileSharing + + err = db.QueryRow(query).Scan(&fileSharing.Online) + if err != nil { + log.Fatal(err) + } + + fileSharingMode := "Offline" + fileSharingStatus := "badStatus" + + if fileSharing.Online { + fileSharingMode = "Online" + fileSharingStatus = "goodStatus" + } + + query = `SELECT online FROM GuestNetwork LIMIT 1` + + var guestNetwork GuestNetwork + + err = db.QueryRow(query).Scan(&guestNetwork.Online) + if err != nil { + log.Fatal(err) + } + + guestNetworkMode := "Offline" + guestNetworkStatus := "badStatus" + + if guestNetwork.Online { + guestNetworkMode = "Online" + guestNetworkStatus = "goodStatus" + } + + overviewItems := OverviewItem{ + InternetMode: internetMode, + InternetPort: internetPort, + InternetStatus: internetStatus, + WifiMode5Ghz: wifiMode5Ghz, + WifiMode2Ghz: wifiMode2Ghz, + WifiModeStatus: wifiModeStatus, + Devices: deviceList.DevicesOnline, + QosMode: qosMode, + QosStatus: qosStatus, + FileShareMode: fileSharingMode, + FileShareStatus: fileSharingStatus, + GuestNetworkMode: guestNetworkMode, + GuestNetworkStatus: guestNetworkStatus, + } + context.JSON(http.StatusOK, overviewItems) } diff --git a/database/Database.go b/database/Database.go new file mode 100644 index 0000000..8507af2 --- /dev/null +++ b/database/Database.go @@ -0,0 +1,17 @@ +package database + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" +) + +const database string = "database/main.db" + +func OpenDatabase() (*sql.DB, error) { + db, err := sql.Open("sqlite3", database) + if err != nil { + return nil, err + } + + return db, nil +} \ No newline at end of file diff --git a/database/main.db b/database/main.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..fbff203b469fb83859921222d458d078819c9d4c 100644 GIT binary patch literal 28672 zcmeI%&uSV$9Ki9J)vQra10MF!%Y-0QXbPbZ&}y>{!lEQ=2`z19(yTG4xL>avKy%npxKSXoZ_}Td0@an%zhlT(G2q1s}0tg_000Iag zkP5uFZKvIqUz72ti9Wc}aWc@!%{2Owk1MZ_-0sL#qi)Z0Ret7aO{r-(2|}%U!=dMP z2Wl`Jsli*%t96byY-eXj-ekGo1QYFl9!EiVo(t_l=G?^6#4pRX(`w1{Eb-ygH`hiX zkr`N;;6th6Y;Vh1Da)|0uY(iq1#$8al?(YE#?!Q?+4^`mue(1Oxd-l1uI^dOsyMs5 za`rAue;h=5qT|@tNn)yxANNazTRomlZ+I45Yv1qhyJ7m2P8X=3nKkC2En8CSe3KPt zZ%@wJ*{1qoqN7kJW>Z&}y>T3zERI-(jV;bEAx?L6IgJwaPp8&N&*rlEN0^I-00Iag zfB*srAb-1)!xdlhS>CIk>b009ILKmY** z5I_I{1lC!=vaG5(|5thczs|n`YDWM81Q0*~0R#|0009ILSOLm4009IL UKmY**5I_I{1Q1w1f%N?U7rIN?ZvX%Q literal 0 HcmV?d00001 diff --git a/go.mod b/go.mod index 58ff5d9..f8c2363 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.23 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect diff --git a/go.sum b/go.sum index 5019dd1..7e98eb9 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= +github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/main.go b/main.go index 9fe7657..020e12b 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "router-ui-backend/api" - "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" )