diff --git a/backend/go.mod b/backend/go.mod index 34d4ebf..57c5c73 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -6,3 +6,5 @@ require ( github.com/jmoiron/sqlx v1.3.5 github.com/mattn/go-sqlite3 v1.14.22 ) + +require github.com/BurntSushi/toml v1.3.2 // indirect diff --git a/backend/go.sum b/backend/go.sum index ee85dd9..afab06f 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go new file mode 100644 index 0000000..aec7512 --- /dev/null +++ b/backend/internal/config/config.go @@ -0,0 +1,55 @@ +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 WriteConfigToFile(c *Config, 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", + } +} diff --git a/backend/internal/config/config_test.go b/backend/internal/config/config_test.go new file mode 100644 index 0000000..cc462ff --- /dev/null +++ b/backend/internal/config/config_test.go @@ -0,0 +1,68 @@ +package config + +import ( + "os" + "testing" +) + +func TestNewConfig(t *testing.T) { + c := NewConfig() + if c.Port != 8080 { + t.Errorf("Expected port to be 8080, got %d", c.Port) + } + if c.DbPath != "./db.sqlite3" { + t.Errorf("Expected db path to be ./db.sqlite3, got %s", c.DbPath) + } + if c.DbUser != "username" { + t.Errorf("Expected db user to be username, got %s", c.DbUser) + } + if c.DbPass != "password" { + t.Errorf("Expected db pass to be password, got %s", c.DbPass) + } + if c.DbName != "ttime" { + t.Errorf("Expected db name to be ttime, got %s", c.DbName) + } +} + +func TestWriteConfig(t *testing.T) { + c := NewConfig() + err := WriteConfigToFile(c, "test.toml") + if err != nil { + t.Errorf("Expected no error, got %s", err) + } + + // Remove the file after the test + _ = os.Remove("test.toml") +} + +func TestReadConfig(t *testing.T) { + c := NewConfig() + err := WriteConfigToFile(c, "test.toml") + if err != nil { + t.Errorf("Expected no error, got %s", err) + } + + c2, err := ReadConfigFromFile("test.toml") + if err != nil { + t.Errorf("Expected no error, got %s", err) + } + + if c.Port != c2.Port { + t.Errorf("Expected port to be %d, got %d", c.Port, c2.Port) + } + if c.DbPath != c2.DbPath { + t.Errorf("Expected db path to be %s, got %s", c.DbPath, c2.DbPath) + } + if c.DbUser != c2.DbUser { + t.Errorf("Expected db user to be %s, got %s", c.DbUser, c2.DbUser) + } + if c.DbPass != c2.DbPass { + t.Errorf("Expected db pass to be %s, got %s", c.DbPass, c2.DbPass) + } + if c.DbName != c2.DbName { + t.Errorf("Expected db name to be %s, got %s", c.DbName, c2.DbName) + } + + // Remove the file after the test + _ = os.Remove("test.toml") +}