Added Database

This commit is contained in:
Patrick Hatsune 2024-09-15 05:23:22 -07:00
parent cce88d39c7
commit c56f892989
Signed by: keyemail
GPG key ID: 6FD1A0FDB0D914C2
6 changed files with 200 additions and 19 deletions

View file

@ -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"`
InternetPort string `json:"internetPort"`
InternetStatus string `json:"internetStatus"`
WifiMode5Ghz string `json:"wifiMode5Ghz"`
WifiMode2Ghz string `json:"wifiMode2Ghz"`
WifiModeStatus string `json:"wifiModeStatus"`
Devices int `json:"devices"`
FileShare string `json:"fileShare"`
Qos string `json:"qos"`
GuestNetwork string `json:"guestNetwork"`
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)
}

17
database/Database.go Normal file
View file

@ -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
}

Binary file not shown.

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -2,7 +2,6 @@ package main
import (
"router-ui-backend/api"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)