diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-09 11:17:40 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-09 11:17:40 -0500 |
commit | 287fb1555d504c79aa9685aa4e701321ddad9f14 (patch) | |
tree | 07d4ba78fb530f5c6e9cd9aebc939bf79ec78429 | |
parent | 65464cb62f9853a38d74f8bc310c2f4b7c19e36b (diff) |
Add database connection
-rw-r--r-- | backend/main.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/backend/main.go b/backend/main.go index 7b66e00..0ba59a8 100644 --- a/backend/main.go +++ b/backend/main.go @@ -2,6 +2,9 @@ package main import "fmt" import "net/http" +import "os" +import _ "github.com/lib/pq" +import "database/sql" func RecipeList(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { @@ -11,7 +14,7 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { } } -func HandleRecipe(w http.ResponseWriter, r *http.Request) { +func SingleRecipe(w http.ResponseWriter, r *http.Request) { name := r.URL.Path[len("/recipes/"):] if r.Method == "GET" { fmt.Printf("Return recipe \"%s\"...\n", name) @@ -22,8 +25,27 @@ func HandleRecipe(w http.ResponseWriter, r *http.Request) { } } +var DB_PASSWORD string +var DB_USER string +var db *sql.DB + +const DB_NAME = "Recipes" + +func init() { + DB_PASSWORD = os.Getenv("DATABASE_PASSWORD") + DB_USER = os.Getenv("DATABASE_USER") +} + func main() { + dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", + DB_USER, DB_PASSWORD, DB_NAME) + fmt.Println(dbinfo) + db, err := sql.Open("postgres", dbinfo) + if err != nil || db.Ping() != nil { + fmt.Println("Error connecting to database") + } + http.HandleFunc("/recipes", RecipeList) - http.HandleFunc("/recipes/", HandleRecipe) + http.HandleFunc("/recipes/", SingleRecipe) http.ListenAndServe(":8888", nil) } |