Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Commit

Permalink
db connection, code format
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Feb 23, 2022
1 parent 1fd7514 commit c17aa61
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 17 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
SESSION_SECRET_KEY=
SESSION_NAME=
STEAM_API_KEY=
PORT=
PORT=

POSTGRES_URL=
POSTGRES_PORT=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
14 changes: 13 additions & 1 deletion utils/env/env.go → config/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package env
package config

import (
"log"
Expand All @@ -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 {
Expand All @@ -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")
}
7 changes: 3 additions & 4 deletions utils/store/store.go → config/store.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 3 additions & 4 deletions controller/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
4 changes: 2 additions & 2 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions model/db.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 6 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit c17aa61

Please sign in to comment.