diff --git a/.env.example b/.env.example index 3f05dc1..18f1d40 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,10 @@ SESSION_SECRET_KEY= SESSION_NAME= STEAM_API_KEY= -PORT= \ No newline at end of file +PORT= + +POSTGRES_URL= +POSTGRES_PORT= +POSTGRES_USER= +POSTGRES_PASSWORD= +POSTGRES_DB= \ No newline at end of file diff --git a/utils/env/env.go b/config/env.go similarity index 54% rename from utils/env/env.go rename to config/env.go index feea62d..7ca376d 100644 --- a/utils/env/env.go +++ b/config/env.go @@ -1,4 +1,4 @@ -package env +package config import ( "log" @@ -12,6 +12,12 @@ var SESSION_NAME string var STEAM_API_KEY string var PORT string +var POSTGRES_URL string +var POSTGRES_PORT string +var POSTGRES_USER string +var POSTGRES_PASSWORD string +var POSTGRES_DB string + func GetEnvVariables() { err := godotenv.Load(".env") if err != nil { @@ -22,4 +28,10 @@ func GetEnvVariables() { SESSION_NAME = os.Getenv("SESSION_NAME") STEAM_API_KEY = os.Getenv("STEAM_API_KEY") PORT = os.Getenv("PORT") + + POSTGRES_URL = os.Getenv("POSTGRES_URL") + POSTGRES_PORT = os.Getenv("POSTGRES_PORT") + POSTGRES_USER = os.Getenv("POSTGRES_USER") + POSTGRES_PASSWORD = os.Getenv("POSTGRES_PASSWORD") + POSTGRES_DB = os.Getenv("POSTGRES_DB") } diff --git a/utils/store/store.go b/config/store.go similarity index 69% rename from utils/store/store.go rename to config/store.go index 48725b1..7510b3a 100644 --- a/utils/store/store.go +++ b/config/store.go @@ -1,21 +1,20 @@ -package store +package config import ( "net/http" "github.com/gorilla/sessions" - "github.com/robyzzz/csl-backend/utils/env" ) var Store = sessions.NewCookieStore([]byte("test")) func SessionAlreadyExists(r *http.Request) bool { - session, _ := Store.Get(r, env.SESSION_NAME) + session, _ := Store.Get(r, SESSION_NAME) return !session.IsNew } func CreateSessionID(w http.ResponseWriter, r *http.Request, value string) error { - session, _ := Store.Get(r, env.SESSION_NAME) + session, _ := Store.Get(r, SESSION_NAME) session.Values["session-id"] = value return session.Save(r, w) } diff --git a/controller/login.go b/controller/login.go index bd980c1..2a5d7ce 100644 --- a/controller/login.go +++ b/controller/login.go @@ -4,8 +4,7 @@ import ( "encoding/json" "net/http" - "github.com/robyzzz/csl-backend/utils/env" - "github.com/robyzzz/csl-backend/utils/store" + "github.com/robyzzz/csl-backend/config" "github.com/solovev/steam_go" ) @@ -22,12 +21,12 @@ func Login(w http.ResponseWriter, r *http.Request) { // login success // get user - user, err := opId.ValidateAndGetUser(env.STEAM_API_KEY) + user, err := opId.ValidateAndGetUser(config.STEAM_API_KEY) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } - store.CreateSessionID(w, r, user.SteamId) + config.CreateSessionID(w, r, user.SteamId) // TODO: store user info in database diff --git a/go.mod b/go.mod index 84816cf..5380f31 100644 --- a/go.mod +++ b/go.mod @@ -12,4 +12,5 @@ require ( require ( github.com/gorilla/securecookie v1.1.1 // indirect github.com/joho/godotenv v1.4.0 // indirect + github.com/lib/pq v1.10.4 // indirect ) diff --git a/go.sum b/go.sum index 0243420..29b70f5 100644 --- a/go.sum +++ b/go.sum @@ -6,5 +6,7 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= +github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/solovev/steam_go v0.0.0-20170222182106-48eb5aae6c50 h1:5wu+B07+rk5rr6KYxYK+5fRr+m8ikSblPSgDTdrFUE4= github.com/solovev/steam_go v0.0.0-20170222182106-48eb5aae6c50/go.mod h1:wDBDgAJlQWhdrpQeJcw6+FZwMddaNWFUo8u8bSfzA50= diff --git a/middleware/auth.go b/middleware/auth.go index eedf4d0..4114d57 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -3,7 +3,7 @@ package middleware import ( "net/http" - "github.com/robyzzz/csl-backend/utils/store" + "github.com/robyzzz/csl-backend/config" ) // Authentication middleware called on routes that need to know if user is logged in. @@ -12,7 +12,7 @@ import ( func IsAuthenticated(h func(w http.ResponseWriter, r *http.Request)) http.Handler { next := http.HandlerFunc(h) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if store.SessionAlreadyExists(r) { + if config.SessionAlreadyExists(r) { http.Redirect(w, r, "/", http.StatusFound) return } diff --git a/model/db.go b/model/db.go new file mode 100644 index 0000000..22a5d10 --- /dev/null +++ b/model/db.go @@ -0,0 +1,31 @@ +package model + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/lib/pq" + env "github.com/robyzzz/csl-backend/config" +) + +var db *sql.DB + +func Connect() { + db, err := sql.Open("postgres", connToString()) + if err != nil { + log.Fatalf("Error connecting to the DB: %s\n", err.Error()) + } + + if err = db.Ping(); err != nil { + log.Fatalf("Error: Could not ping database: %s\n", err.Error()) + } + + log.Printf("Database connection done successfully\n") +} + +// Return a string for our db connection info +func connToString() string { + return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", + env.POSTGRES_URL, env.POSTGRES_PORT, env.POSTGRES_USER, env.POSTGRES_PASSWORD, env.POSTGRES_DB) +} diff --git a/server.go b/server.go index b3b682b..3ea2f83 100644 --- a/server.go +++ b/server.go @@ -1,25 +1,26 @@ package main import ( - "fmt" "log" "net/http" "github.com/gorilla/mux" + "github.com/robyzzz/csl-backend/config" "github.com/robyzzz/csl-backend/controller" "github.com/robyzzz/csl-backend/middleware" - "github.com/robyzzz/csl-backend/utils/env" + "github.com/robyzzz/csl-backend/model" ) var router *mux.Router func main() { - env.GetEnvVariables() + config.GetEnvVariables() + model.Connect() setupRouter() - fmt.Printf("router initialized and listening on %s\n", env.PORT) - log.Fatal(http.ListenAndServe(":"+env.PORT, router)) + log.Printf("Server initialized and listening on %s\n", config.PORT) + log.Fatal(http.ListenAndServe(":"+config.PORT, router)) } func setupRouter() {