changed hardcoded database configuration to environment variables. Other configuration can also be added to the configuration struct for further usage inside the app. Added an environment file to source for development and usage outside the cluster.

This commit is contained in:
Stefan Friese 2025-01-17 09:04:20 +00:00
parent eb53fd5b3d
commit 68f135fb7b
5 changed files with 39 additions and 12 deletions

7
.env Normal file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
export DB_HOST="127.0.0.1"
export DB_PORT=3306
export DB_USERNAME="mysql"
export DB_PASSWORD="mysql"
export DB_NAME="gomatic"

1
.gitignore vendored
View File

@ -2,4 +2,5 @@ gontrol
bin/* bin/*
build/ build/
build/* build/*
pkg/*
agents/agents agents/agents

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.23.4
require ( require (
github.com/go-sql-driver/mysql v1.8.1 github.com/go-sql-driver/mysql v1.8.1
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/kelseyhightower/envconfig v1.4.0
) )
require filippo.io/edwards25519 v1.1.0 // indirect require filippo.io/edwards25519 v1.1.0 // indirect

2
go.sum
View File

@ -4,3 +4,5 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=

40
main.go
View File

@ -21,26 +21,39 @@ import (
"syscall" "syscall"
"gontrol/src/randomname" "gontrol/src/randomname"
websocketserver "gontrol/src/server/websocket"
api "gontrol/src/server/api" api "gontrol/src/server/api"
websocketserver "gontrol/src/server/websocket"
"os/signal" "os/signal"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
// "github.com/gorilla/websocket" "github.com/kelseyhightower/envconfig"
) )
var tmpl *template.Template var tmpl *template.Template
var db *sql.DB var db *sql.DB
const ( type Config struct {
keyServerAddr = "serverAddr" Database struct {
dbHost = "127.0.0.1" Username string `envconfig:"DB_USERNAME"`
dbUser = "mysql" Password string `envconfig:"DB_PASSWORD"`
dbPort = 3306 Port int16 `envconfig:"DB_PORT"`
dbPassword = "mysql" Name string `envconfig:"DB_NAME"`
dbName = "gomatic" Host string `envconfig:"DB_HOST"`
) }
}
func readEnv(cfg *Config) {
err := envconfig.Process("", cfg)
if err != nil {
processError(err)
}
}
func processError(err error) {
fmt.Println(err)
os.Exit(2)
}
type Agent struct { type Agent struct {
AgentID int `json:"agentId"` AgentID int `json:"agentId"`
@ -55,7 +68,7 @@ func init() {
tmpl, _ = template.ParseGlob("templates/*.html") tmpl, _ = template.ParseGlob("templates/*.html")
} }
func initDB () { func initDB (dbUser string, dbPassword string, dbHost string, dbPort int16, dbName string ) {
var err error var err error
dbOpen := fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbUser,dbPassword, dbHost, dbPort, dbName) dbOpen := fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbUser,dbPassword, dbHost, dbPort, dbName)
db, err = sql.Open("mysql", dbOpen) db, err = sql.Open("mysql", dbOpen)
@ -140,6 +153,9 @@ func getAgentNames(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
var cfg Config
readEnv(&cfg)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -161,7 +177,7 @@ func main() {
webMux.HandleFunc("/agentNames", getAgentNames) webMux.HandleFunc("/agentNames", getAgentNames)
webMux.HandleFunc("/agents/{agentId}", agentsHandler) webMux.HandleFunc("/agents/{agentId}", agentsHandler)
initDB() initDB (cfg.Database.Username, cfg.Database.Password, cfg.Database.Host, cfg.Database.Port, cfg.Database.Name)
defer db.Close() defer db.Close()
name := randomname.GenerateRandomName() name := randomname.GenerateRandomName()
fmt.Println(name) fmt.Println(name)