Added database module with exportable functions
parent
28cb163f6d
commit
375bb03dfe
|
@ -18,3 +18,6 @@ main
|
|||
# Build
|
||||
|
||||
/bin
|
||||
|
||||
# Go Sum files
|
||||
*.sum
|
||||
|
|
14
Makefile
14
Makefile
|
@ -5,6 +5,11 @@ COMMIT := $(shell git rev-parse --short HEAD)
|
|||
BUILD_DATE := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
|
||||
BUILD_DIR = bin
|
||||
|
||||
DBHOST = 127.0.0.1:3306
|
||||
DBNAME = docker
|
||||
DBUSER = root
|
||||
DBPASS = tiger
|
||||
|
||||
build-all:
|
||||
GOOS=linux GOARCH=amd64 make build
|
||||
GOOS=linux GOARCH=arm make build
|
||||
|
@ -14,7 +19,14 @@ build:
|
|||
GOOS=${GOOS} GOARCH=${GOARCH} go build \
|
||||
-trimpath -ldflags "-s -w -X ${PROJECT}/internal/diagnostics.Version=${RELEASE} \
|
||||
-X ${PROJECT}/internal/diagnostics.Commit=${COMMIT} \
|
||||
-X ${PROJECT}/internal/diagnostics.BuildDate=${BUILD_DATE}" \
|
||||
-X ${PROJECT}/internal/diagnostics.BuildDate=${BUILD_DATE} \
|
||||
-X ${PROJECT}/internal/database.Version=${RELEASE} \
|
||||
-X ${PROJECT}/internal/database.Commit=${COMMIT} \
|
||||
-X ${PROJECT}/internal/database.BuildDate=${BUILD_DATE} \
|
||||
-X ${PROJECT}/internal/database.Dbhost=${DBHOST} \
|
||||
-X ${PROJECT}/internal/database.Dbname=${DBNAME} \
|
||||
-X ${PROJECT}/internal/database.Dbuser=${DBUSER} \
|
||||
-X ${PROJECT}/internal/database.Dbpass=${DBPASS}" \
|
||||
-o bin/sysctl-${GOOS}-${GOARCH} ${PROJECT}/cmd/sysctl
|
||||
cp ./bin/sysctl-${GOOS}-${GOARCH} ./bin/sysctl
|
||||
|
||||
|
|
|
@ -6,9 +6,10 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/edgebox-iot/sysctl/internal/database"
|
||||
"github.com/edgebox-iot/sysctl/internal/diagnostics"
|
||||
)
|
||||
|
||||
|
@ -45,6 +46,11 @@ func main() {
|
|||
os.Exit(1)
|
||||
}()
|
||||
|
||||
printVersion()
|
||||
printDbDetails()
|
||||
dbQueryResult := database.PerformQuery()
|
||||
log.Printf("Query result: %s", dbQueryResult)
|
||||
|
||||
// infinite loop
|
||||
for {
|
||||
|
||||
|
@ -61,11 +67,18 @@ func appCleanup() {
|
|||
|
||||
func printVersion() {
|
||||
fmt.Printf(
|
||||
"version: %s\ncommit: %s\nbuild time: %s",
|
||||
"\nversion: %s\ncommit: %s\nbuild time: %s\n",
|
||||
diagnostics.Version, diagnostics.Commit, diagnostics.BuildDate,
|
||||
)
|
||||
}
|
||||
|
||||
func printDbDetails() {
|
||||
fmt.Printf(
|
||||
"\n\nDatabase Connection Information:\nHost: %s\nuser: %s\npassword: %s\n\n",
|
||||
database.Dbhost, database.Dbuser, database.Dbpass,
|
||||
)
|
||||
}
|
||||
|
||||
// IsSystemReady : Checks hability of the service to execute commands (Only after "edgebox --build" is ran at least once via SSH, or if built for distribution)
|
||||
func isSystemReady() bool {
|
||||
_, err := os.Stat("/home/system/components/ws")
|
||||
|
|
10
go.mod
10
go.mod
|
@ -1,4 +1,10 @@
|
|||
|
||||
module github.com/edgebox-iot/sysctl
|
||||
|
||||
go 1.15
|
||||
go 1.15
|
||||
|
||||
replace github.com/edgebox-iot/sysctl/internal/database => ./internal/database
|
||||
|
||||
require (
|
||||
github.com/edgebox-iot/sysctl/internal/database v0.0.0-00010101000000-000000000000
|
||||
github.com/go-sql-driver/mysql v1.5.0 // indirect
|
||||
)
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
var Dbhost string
|
||||
var Dbname string
|
||||
var Dbuser string
|
||||
var Dbpass string
|
||||
|
||||
// PerformQuery : Performs a MySQL query over the device's Edgebox API
|
||||
func PerformQuery() string {
|
||||
|
||||
// Will try to connect to API database, which should be running locally under WS.
|
||||
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
|
||||
|
||||
// if there is an error opening the connection, handle it
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// defer the close till after the main function has finished executing
|
||||
defer db.Close()
|
||||
|
||||
// perform a db.Query insert
|
||||
insert, err := db.Query("INSERT INTO options (name, value) VALUES ( 'TEST_OPTION_SYSCTL', 'TEST' );")
|
||||
|
||||
// if there is an error inserting, handle it
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// be careful deferring Queries if you are using transactions
|
||||
defer insert.Close()
|
||||
|
||||
return "OK"
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module github.com/edgebox-iot/sysctl/internal/database
|
||||
|
||||
go 1.15
|
Loading…
Reference in New Issue