55 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package config
 | |
| 
 | |
| import (
 | |
| 	"os"
 | |
| 
 | |
| 	"github.com/BurntSushi/toml"
 | |
| )
 | |
| 
 | |
| // Config is the configuration for the application
 | |
| // It defines how the TOML file should be structured
 | |
| type Config struct {
 | |
| 	// The port to listen on
 | |
| 	Port int `toml:"port"`
 | |
| 	// The path to the SQLite database
 | |
| 	DbPath string `toml:"db_path"`
 | |
| 	// The username to use for the database
 | |
| 	DbUser string `toml:"db_user"`
 | |
| 	// The password to use for the database
 | |
| 	DbPass string `toml:"db_pass"`
 | |
| 	// The name of the database
 | |
| 	DbName string `toml:"db_name"`
 | |
| }
 | |
| 
 | |
| // WriteConfigToFile writes a Config to a file
 | |
| func (c *Config) WriteConfigToFile(filename string) error {
 | |
| 	f, err := os.Create(filename)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer f.Close()
 | |
| 
 | |
| 	return toml.NewEncoder(f).Encode(c)
 | |
| }
 | |
| 
 | |
| // ReadConfigFromFile reads a Config from a file
 | |
| func ReadConfigFromFile(filename string) (*Config, error) {
 | |
| 	c := NewConfig()
 | |
| 	_, err := toml.DecodeFile(filename, c)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return c, nil
 | |
| }
 | |
| 
 | |
| // NewConfig returns a new Config
 | |
| func NewConfig() *Config {
 | |
| 	return &Config{
 | |
| 		Port:   8080,
 | |
| 		DbPath: "./db.sqlite3",
 | |
| 		DbUser: "username",
 | |
| 		DbPass: "password",
 | |
| 		DbName: "ttime",
 | |
| 	}
 | |
| }
 | 
