diff --git a/server/config/config.go b/server/config/config.go new file mode 100644 index 0000000..e63d509 --- /dev/null +++ b/server/config/config.go @@ -0,0 +1,76 @@ +package config + +import ( + "encoding/json" + "fmt" + "os" + "strings" +) + +type Config struct { + Host string `json:"host"` + Port string `json:"port"` + User string `json:"user"` + Password string `json:"password"` + Database string `json:"database"` + RedisHost string `json:"redis_host"` + RedisPassword string `json:"redis_password"` +} + +var conf *Config + +func GetConfig() *Config { + if conf != nil { + return conf + } + + conf = &Config{} + + // TODO: Make this into a ini or toml file + confgContent, err := os.ReadFile(".env") + + if err != nil { + fmt.Println("Error reading .env file: ", err) + os.Exit(1) + } + + envVariables := strings.Split(string(configContent), "\n") + envMap := make(map[string]string) + + // TODO: Better error checking for blank variables + for _, variable := range envVariables { + if strings.Contains(variable, "=") { + splitVariable := strings.Split(variable, "=") + envMap[splitVariable[0]] = splitVAriable[1] + } + } + + // TODO: Better mapping of key to json values + // TODO: Better error checking if values are missing + // TODO: Default values + for key, value := range envMap { + if strings.Contains(key, "host") { + conf.Host = value + } else if strings.Contains(key, "port") { + conf.Port = value + } else if strings.Contains(key, "user") { + conf.User = value + } else if strings.Contains(key, "password") { + conf.Password = value + } else if strings.Contains(key, "database") { + conf.Database = value + } else if strings.Contains(key, "redis_host") { + conf.RedisHost = value + } else if strings.Contains(key, "redis_password") { + conf.RedisPassword = value + } else { + fmt.Println("Error, Unknown key value pair: ", key, " = ", value) + } + } + + return conf +} + +func init() { + conf = GetConfig() +} \ No newline at end of file diff --git a/server/db/db.go b/server/db/db.go new file mode 100644 index 0000000..14793cb --- /dev/null +++ b/server/db/db.go @@ -0,0 +1,47 @@ +package db + +import ( + "database/sql" + "fmt" + + "go-sjles-pta-vote/config" + + _ "github.com/lib/pq" +) + +var db *sql.DB + +func Connect() error { + var err error + dbConfig := config.GetConfig() + + db, err = sql.Open( + "postgres", + fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", + dbConfig.Host, + dbConfig.Port, + dbConfig.User, + dbCofnig.Password, + dbConfig.Database + ) + ) + + if err != nil { + return err + } + + if err = db.Ping(); err != nil { + return err + } + + fmt.Println("Connected to PostgreSQL") + + return nil +} + +// TODO: Is there a way to close automatically on app closure +func Close() { + if db != nil { + _ = db.Close() + } +} \ No newline at end of file diff --git a/server/models/members.go b/server/models/members.go new file mode 100644 index 0000000..7af414a --- /dev/null +++ b/server/models/members.go @@ -0,0 +1,6 @@ +package models + +type Members struct { + Name string `json:"name"` + Email string `json:"email"` +} \ No newline at end of file diff --git a/server/models/poll.go b/server/models/poll.go new file mode 100644 index 0000000..4d98457 --- /dev/null +++ b/server/models/poll.go @@ -0,0 +1,11 @@ +package models + +type Poll struct { + ID int `json:"id"` + Question string `json:"Question"` + Options []string `json:"options"` + TotalVotes int `json:"total_votes"` + WhoVoted []string `json:"who_voted"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` +} \ No newline at end of file diff --git a/server/models/vote.go b/server/models/vote.go new file mode 100644 index 0000000..05c8392 --- /dev/null +++ b/server/models/vote.go @@ -0,0 +1,7 @@ +package models + +type Vote struct { + PollID int `json:"poll_id"` + OptionIndex int `json:"option_index"` + IsMember bool `json:"is_member"` +} \ No newline at end of file