Initial structure for the votes
This commit is contained in:
76
server/config/config.go
Normal file
76
server/config/config.go
Normal file
@@ -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()
|
||||
}
|
||||
47
server/db/db.go
Normal file
47
server/db/db.go
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
6
server/models/members.go
Normal file
6
server/models/members.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type Members struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
11
server/models/poll.go
Normal file
11
server/models/poll.go
Normal file
@@ -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"`
|
||||
}
|
||||
7
server/models/vote.go
Normal file
7
server/models/vote.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
type Vote struct {
|
||||
PollID int `json:"poll_id"`
|
||||
OptionIndex int `json:"option_index"`
|
||||
IsMember bool `json:"is_member"`
|
||||
}
|
||||
Reference in New Issue
Block a user