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:
		
							parent
							
								
									eb53fd5b3d
								
							
						
					
					
						commit
						68f135fb7b
					
				| 
						 | 
				
			
			@ -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"
 | 
			
		||||
| 
						 | 
				
			
			@ -2,4 +2,5 @@ gontrol
 | 
			
		|||
bin/*
 | 
			
		||||
build/
 | 
			
		||||
build/*
 | 
			
		||||
pkg/*
 | 
			
		||||
agents/agents
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										1
									
								
								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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										2
									
								
								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=
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										40
									
								
								main.go
								
								
								
								
							
							
						
						
									
										40
									
								
								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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue