1
Fork 0

Update env files and move to postgres

This commit is contained in:
viktorstrate 2020-01-30 15:10:01 +01:00
parent 494d880771
commit 3a730fea36
7 changed files with 18 additions and 19 deletions

2
.gitignore vendored
View File

@ -3,6 +3,8 @@
cache/
/photos_path
.env
# docker
docker-compose.yml

View File

@ -1,3 +1,5 @@
# Copy this file to .env
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=letmein

View File

@ -1,6 +0,0 @@
MYSQL_HOST=127.0.0.1:3306
MYSQL_DATABASE=photoview
MYSQL_USERNAME=photoview
MYSQL_PASSWORD=
API_PORT=4001

View File

@ -2,30 +2,23 @@ package database
import (
"database/sql"
"fmt"
"log"
"os"
// Load mysql driver
_ "github.com/go-sql-driver/mysql"
// _ "github.com/go-sql-driver/mysql"
// Load postgres driver
_ "github.com/lib/pq"
)
// SetupDatabase connects to the database using environment variables
func SetupDatabase() *sql.DB {
host := os.Getenv("MYSQL_HOST")
database := os.Getenv("MYSQL_DATABASE")
username := os.Getenv("MYSQL_USERNAME")
password := os.Getenv("MYSQL_PASSWORD")
if host == "" || database == "" || username == "" {
log.Fatalln("Database host, name and username are required")
}
address := fmt.Sprintf("%s:%s@tcp(%s)/%s", username, password, host, database)
address := os.Getenv("POSTGRES_URL")
log.Printf("Connecting to database: %s", address)
db, err := sql.Open("mysql", address)
db, err := sql.Open("postgres", address)
if err != nil {
log.Fatalf("Could not connect to database: %s\n", err.Error())
}

5
api/example.env Normal file
View File

@ -0,0 +1,5 @@
# Copy this file to .env
POSTGRES_URL=postgres://username:password@localhost/database?sslmode=disable
API_PORT=4001

View File

@ -6,5 +6,6 @@ require (
github.com/99designs/gqlgen v0.10.2
github.com/go-sql-driver/mysql v1.5.0
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.3.0
github.com/vektah/gqlparser v1.2.0
)

View File

@ -20,6 +20,8 @@ github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqx
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=