Dependency injection, example .env, and config handling
This commit is contained in:
		
							
								
								
									
										12
									
								
								app/app.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								app/app.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
package app
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"GoWeb/config"
 | 
			
		||||
	"database/sql"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// App contains and supplies available configurations and connections
 | 
			
		||||
type App struct {
 | 
			
		||||
	Config config.Configuration
 | 
			
		||||
	Db     *sql.DB
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										49
									
								
								config/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								config/config.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Configuration struct {
 | 
			
		||||
	Db struct {
 | 
			
		||||
		Ip       string `json:"DbIp"`
 | 
			
		||||
		Port     string `json:"DbPort"`
 | 
			
		||||
		Name     string `json:"DbName"`
 | 
			
		||||
		User     string `json:"DbUser"`
 | 
			
		||||
		Password string `json:"DbPassword"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Listen struct {
 | 
			
		||||
		Ip   string `json:"HttpIp"`
 | 
			
		||||
		Port string `json:"HttpPort"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Template struct {
 | 
			
		||||
		BaseName string `json:"BaseTemplateName"`
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LoadConfig loads and returns a configuration struct
 | 
			
		||||
func LoadConfig() Configuration {
 | 
			
		||||
	c := flag.String("c", "env.json", "Path to the json configuration file")
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
	file, err := os.Open(*c)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal("Unable to open config JSON file: ", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer file.Close()
 | 
			
		||||
 | 
			
		||||
	// Decode json config file to Configuration struct named config
 | 
			
		||||
	decoder := json.NewDecoder(file)
 | 
			
		||||
	Config := Configuration{}
 | 
			
		||||
	err = decoder.Decode(&Config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal("Unable to decode config JSON file: ", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return Config
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								env_example.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								env_example.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
{
 | 
			
		||||
  "Db": {
 | 
			
		||||
    "DbIp": "127.0.0.1",
 | 
			
		||||
    "DbPort": "5432",
 | 
			
		||||
    "DbName": "database",
 | 
			
		||||
    "DbUser": "user",
 | 
			
		||||
    "DbPassword": "password"
 | 
			
		||||
  },
 | 
			
		||||
  "Listen": {
 | 
			
		||||
    "HttpIp": "127.0.0.1",
 | 
			
		||||
    "HttpPort": "8090"
 | 
			
		||||
  },
 | 
			
		||||
  "Template": {
 | 
			
		||||
    "BaseTemplateName": "templates/base.html"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user