diff --git a/.env b/.env new file mode 100644 index 0000000..29b888b --- /dev/null +++ b/.env @@ -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" diff --git a/.gitignore b/.gitignore index cb77ab3..ee46fee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ gontrol bin/* build/ build/* +pkg/* agents/agents diff --git a/go.mod b/go.mod index b16a01d..4798552 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.4 require ( github.com/go-sql-driver/mysql v1.8.1 github.com/gorilla/websocket v1.5.3 + github.com/kelseyhightower/envconfig v1.4.0 ) require filippo.io/edwards25519 v1.1.0 // indirect diff --git a/go.sum b/go.sum index 5142118..82db585 100644 --- a/go.sum +++ b/go.sum @@ -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/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= 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= diff --git a/main.go b/main.go index ce47808..a90fe1c 100644 --- a/main.go +++ b/main.go @@ -21,26 +21,39 @@ import ( "syscall" "gontrol/src/randomname" - websocketserver "gontrol/src/server/websocket" api "gontrol/src/server/api" + websocketserver "gontrol/src/server/websocket" "os/signal" _ "github.com/go-sql-driver/mysql" - // "github.com/gorilla/websocket" + "github.com/kelseyhightower/envconfig" ) var tmpl *template.Template var db *sql.DB -const ( - keyServerAddr = "serverAddr" - dbHost = "127.0.0.1" - dbUser = "mysql" - dbPort = 3306 - dbPassword = "mysql" - dbName = "gomatic" -) +type Config struct { + Database struct { + Username string `envconfig:"DB_USERNAME"` + Password string `envconfig:"DB_PASSWORD"` + Port int16 `envconfig:"DB_PORT"` + Name string `envconfig:"DB_NAME"` + 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 { AgentID int `json:"agentId"` @@ -55,7 +68,7 @@ func init() { tmpl, _ = template.ParseGlob("templates/*.html") } -func initDB () { +func initDB (dbUser string, dbPassword string, dbHost string, dbPort int16, dbName string ) { var err error dbOpen := fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbUser,dbPassword, dbHost, dbPort, dbName) db, err = sql.Open("mysql", dbOpen) @@ -140,6 +153,9 @@ func getAgentNames(w http.ResponseWriter, r *http.Request) { func main() { + var cfg Config + readEnv(&cfg) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -161,7 +177,7 @@ func main() { webMux.HandleFunc("/agentNames", getAgentNames) 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() name := randomname.GenerateRandomName() fmt.Println(name)