1
Fork 0

Start on mysql

This commit is contained in:
viktorstrate 2020-01-30 14:49:39 +01:00
parent 9d95b7bead
commit 494d880771
5 changed files with 63 additions and 2 deletions

6
api/.env Normal file
View File

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

38
api/database/mysql.go Normal file
View File

@ -0,0 +1,38 @@
package database
import (
"database/sql"
"fmt"
"log"
"os"
// Load mysql driver
_ "github.com/go-sql-driver/mysql"
)
// 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)
log.Printf("Connecting to database: %s", address)
db, err := sql.Open("mysql", address)
if err != nil {
log.Fatalf("Could not connect to database: %s\n", err.Error())
}
if err := db.Ping(); err != nil {
log.Fatalf("Could not connect to database: %s\n", err.Error())
}
return db
}

View File

@ -4,5 +4,7 @@ go 1.13
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/vektah/gqlparser v1.2.0
)

View File

@ -6,6 +6,8 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v3.3.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gogo/protobuf v1.0.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
@ -13,6 +15,8 @@ github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTM
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
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=

View File

@ -5,18 +5,29 @@ import (
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/viktorstrate/photoview/api/database"
"github.com/99designs/gqlgen/handler"
photoview_graphql "github.com/viktorstrate/photoview/api/graphql"
)
const defaultPort = "8080"
const defaultPort = "4001"
func main() {
port := os.Getenv("PORT")
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
port := os.Getenv("API_PORT")
if port == "" {
port = defaultPort
}
database.SetupDatabase()
http.Handle("/", handler.Playground("GraphQL playground", "/query"))
http.Handle("/query", handler.GraphQL(photoview_graphql.NewExecutableSchema(photoview_graphql.Config{Resolvers: &photoview_graphql.Resolver{}})))