🔒 add more env variables for backend
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
parent
dbd6050764
commit
369ae7c7ed
4 changed files with 26 additions and 77 deletions
|
@ -1,4 +1,10 @@
|
||||||
POSTGRES_PASSWORD=supersafepassword
|
POSTGRES_PASSWORD=supersafepassword
|
||||||
POSTGRES_USER=superman
|
POSTGRES_USER=superman
|
||||||
POSTGRES_HOST=postgres
|
POSTGRES_HOST=postgres
|
||||||
POSTGRES_DB=supercooldbname
|
POSTGRES_DB=supercooldbname
|
||||||
|
|
||||||
|
DB_HOST=postgres
|
||||||
|
DB_PORT=sickassport
|
||||||
|
DB_USER=admin
|
||||||
|
DB_PASSWORD=supersafepassword
|
||||||
|
DB_NAME=supercooldbname
|
|
@ -5,19 +5,28 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func init() {
|
||||||
host = "postgres"
|
if err := godotenv.Load(".env"); err != nil {
|
||||||
port = 5432 // This is the default port for PostgreSQL
|
log.Fatalf("Error loading .env file: %v", err)
|
||||||
user = "admin"
|
}
|
||||||
password = "helse123"
|
}
|
||||||
dbname = "helseveileder"
|
|
||||||
|
var (
|
||||||
|
host = os.Getenv("DB_HOST")
|
||||||
|
portStr = os.Getenv("DB_PORT")
|
||||||
|
user = os.Getenv("DB_USER")
|
||||||
|
password = os.Getenv("DB_PASSWORD")
|
||||||
|
dbname = os.Getenv("DB_NAME")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var port, _ = strconv.Atoi(portStr)
|
||||||
|
|
||||||
func SetupDb() {
|
func SetupDb() {
|
||||||
// Connection string
|
// Connection string
|
||||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
|
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
|
||||||
|
@ -36,73 +45,4 @@ func SetupDb() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error connecting to the database: %v\n", err)
|
log.Fatalf("Error connecting to the database: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SQL statements to create tables
|
|
||||||
createTableStatements := []string{
|
|
||||||
`CREATE TABLE IF NOT EXISTS Spørsmål (
|
|
||||||
spørsmålID SERIAL PRIMARY KEY,
|
|
||||||
tekst TEXT NOT NULL
|
|
||||||
);`,
|
|
||||||
`CREATE TABLE IF NOT EXISTS SpørsmålSvar (
|
|
||||||
svarID SERIAL PRIMARY KEY,
|
|
||||||
spørsmålID INT REFERENCES Spørsmål(spørsmålID),
|
|
||||||
chatgpt BOOL NOT NULL,
|
|
||||||
svartekst TEXT NOT NULL
|
|
||||||
);`,
|
|
||||||
`CREATE TABLE IF NOT EXISTS Respondent (
|
|
||||||
respondentID SERIAL PRIMARY KEY,
|
|
||||||
alder TEXT NOT NULL,
|
|
||||||
utdanningsgrad TEXT NOT NULL,
|
|
||||||
helsepersonell BOOL NOT NULL,
|
|
||||||
harlisens BOOL NOT NULL,
|
|
||||||
kjønn TEXT NOT NULL,
|
|
||||||
svartfør BOOL NOT NULL,
|
|
||||||
fylke TEXT NOT NULL,
|
|
||||||
dato TEXT NOT NULL
|
|
||||||
);`,
|
|
||||||
`CREATE TABLE IF NOT EXISTS SvarVurdering (
|
|
||||||
vurderingID SERIAL PRIMARY KEY,
|
|
||||||
respondentID INT REFERENCES Respondent(respondentID),
|
|
||||||
svarID INT REFERENCES SpørsmålSvar(svarID),
|
|
||||||
kunnskap INT,
|
|
||||||
empati INT,
|
|
||||||
hjelpsomhet INT
|
|
||||||
);`,
|
|
||||||
`CREATE TABLE IF NOT EXISTS Evaluering (
|
|
||||||
evalueringtekst TEXT
|
|
||||||
);`,
|
|
||||||
`CREATE TABLE IF NOT EXISTS FeilRapport (
|
|
||||||
feiltekst TEXT
|
|
||||||
);`,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute SQL statements
|
|
||||||
for _, stmt := range createTableStatements {
|
|
||||||
_, err := db.Exec(stmt)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error creating table: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
insertDb(db, "cmd/db/queries/insert_questions_sql_query.sql")
|
|
||||||
insertDb(db, "cmd/db/queries/insert_answers_sql_query.sql")
|
|
||||||
|
|
||||||
fmt.Println("Tables created successfully.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertDb(db *sql.DB, filepath string) {
|
|
||||||
file, err := os.ReadFile(filepath)
|
|
||||||
if err != nil {
|
|
||||||
// handle error
|
|
||||||
log.Fatalf("Error reading file: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
requests := strings.Split(string(file), ";")
|
|
||||||
for _, request := range requests {
|
|
||||||
_, err := db.Exec(request)
|
|
||||||
// do whatever you need with result and error
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error creating table: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -14,6 +14,7 @@ require (
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.16.0 // indirect
|
github.com/go-playground/validator/v10 v10.16.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
|
github.com/joho/godotenv v1.5.1 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
||||||
github.com/leodido/go-urn v1.2.4 // indirect
|
github.com/leodido/go-urn v1.2.4 // indirect
|
||||||
|
|
|
@ -30,6 +30,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
|
Reference in a new issue