Switching to SQLite and creating the tables
This commit is contained in:
17
go.mod
Normal file
17
go.mod
Normal file
@@ -0,0 +1,17 @@
|
||||
module go-sjles-pta-vote/server
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require github.com/glebarez/go-sqlite v1.22.0
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.5.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/sys v0.15.0 // indirect
|
||||
modernc.org/libc v1.37.6 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
modernc.org/memory v1.7.2 // indirect
|
||||
modernc.org/sqlite v1.28.0 // indirect
|
||||
)
|
||||
23
go.sum
Normal file
23
go.sum
Normal file
@@ -0,0 +1,23 @@
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
|
||||
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw=
|
||||
modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
|
||||
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||
modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ=
|
||||
modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
|
||||
@@ -8,11 +8,7 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
DBPath string `json:"db_path"`
|
||||
RedisHost string `json:"redis_host"`
|
||||
RedisPassword string `json:"redis_password"`
|
||||
}
|
||||
@@ -49,16 +45,8 @@ func GetConfig() *Config {
|
||||
// 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
|
||||
if strings.Contains(key, "db_path") {
|
||||
conf.DBPath = value
|
||||
} else if strings.Contains(key, "redis_host") {
|
||||
conf.RedisHost = value
|
||||
} else if strings.Contains(key, "redis_password") {
|
||||
|
||||
@@ -3,43 +3,34 @@ package db
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"go-sjles-pta-vote/config"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
)
|
||||
db, err = sql.Open("sqlite", dbConfig.DBPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = db.Ping(); err != nil {
|
||||
sql_create, err := ioutil.ReadFile("./db_format.sql")
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Connected to PostgreSQL")
|
||||
_, err = db.Exec(sql_create)
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Is there a way to close automatically on app closure
|
||||
func Close() {
|
||||
if db != nil {
|
||||
_ = db.Close()
|
||||
|
||||
25
server/db/db_format.sql
Normal file
25
server/db/db_format.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE IF NOT EXISTS poll (
|
||||
id UNSIGNED INT NOT NULL AUTOINCREMENT,
|
||||
question TEXT NOT NULL,
|
||||
member_yes_votes UNSIGNED INT NOT NULL,
|
||||
member_no_votes UNSIGNED INT NOT NULL,
|
||||
non_member_yes_votes UNSIGNED INT NOT NULL,
|
||||
non_member_no_votes UNSIGNED INT NOT NULL,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME,
|
||||
expires_at DATETIME,
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS voters (
|
||||
poll_id UNSIGNED INT NOT NULL,
|
||||
voter_email TEXT NOT NULL,
|
||||
FOREIGN KEY (poll_id) poll(id),
|
||||
PRIMARY KEY (poll_id, voter_email)
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS members (
|
||||
email TEXT NOT NULL,
|
||||
member_name TEXT,
|
||||
PRIMARY KEY (email)
|
||||
)
|
||||
@@ -2,10 +2,10 @@ package models
|
||||
|
||||
type Poll struct {
|
||||
ID int `json:"id"`
|
||||
Question string `json:"Question"`
|
||||
Options []string `json:"options"`
|
||||
Question string `json:"question"`
|
||||
TotalVotes int `json:"total_votes"`
|
||||
WhoVoted []string `json:"who_voted"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
}
|
||||
Reference in New Issue
Block a user