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

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Feb 23, 2022
1 parent cbde045 commit deef25f
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 1 deletion.
Empty file added .env.example
Empty file.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Environment variables
.env

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# csl-backend
# csl-backend
## Combat Surf League API
7 changes: 7 additions & 0 deletions controller/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package controller

import "net/http"

func Home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("home"))
}
37 changes: 37 additions & 0 deletions controller/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controller

import (
"encoding/json"
"net/http"

"github.com/robyzzz/csl-backend/utils/env"
"github.com/robyzzz/csl-backend/utils/store"
"github.com/solovev/steam_go"
)

func Login(w http.ResponseWriter, r *http.Request) {
opId := steam_go.NewOpenId(r)

switch opId.Mode() {
case "cancel":
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
case "":
// redirect to steam auth
http.Redirect(w, r, opId.AuthUrl(), http.StatusTemporaryRedirect)
default:
// login success

// get user
user, err := opId.ValidateAndGetUser(env.STEAM_API_KEY)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

store.CreateSessionID(w, r, user.SteamId)

// TODO: store user info in database

w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(user)
}
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/robyzzz/csl-backend

go 1.17

require (
github.com/gorilla/mux v1.8.0
github.com/gorilla/sessions v1.2.1
github.com/solovev/steam_go v0.0.0-20170222182106-48eb5aae6c50

)

require (
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/joho/godotenv v1.4.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
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/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=
22 changes: 22 additions & 0 deletions middleware/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package middleware

import (
"net/http"

"github.com/robyzzz/csl-backend/utils/store"
)

// Authentication middleware called on routes that need to know if user is logged in.
// Returns to root page if session already exists.
// i.e usage: /login
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) {
http.Redirect(w, r, "/", http.StatusFound)
return
}

next.ServeHTTP(w, r)
})
}
29 changes: 29 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/gorilla/mux"
"github.com/robyzzz/csl-backend/controller"
"github.com/robyzzz/csl-backend/middleware"
"github.com/robyzzz/csl-backend/utils/env"
)

var router *mux.Router

func main() {
env.GetEnvVariables()

setupRouter()

fmt.Printf("router initialized and listening on %s\n", env.PORT)
log.Fatal(http.ListenAndServe(":"+env.PORT, router))
}

func setupRouter() {
router = mux.NewRouter()
router.HandleFunc("/", controller.Home)
router.Handle("/login", middleware.IsAuthenticated(controller.Login))
}
25 changes: 25 additions & 0 deletions utils/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package env

import (
"log"
"os"

"github.com/joho/godotenv"
)

var SESSION_SECRET_KEY string
var SESSION_NAME string
var STEAM_API_KEY string
var PORT string

func GetEnvVariables() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}

SESSION_SECRET_KEY = os.Getenv("SESSION_SECRET_KEY")
SESSION_NAME = os.Getenv("SESSION_NAME")
STEAM_API_KEY = os.Getenv("STEAM_API_KEY")
PORT = os.Getenv("PORT")
}
21 changes: 21 additions & 0 deletions utils/store/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package store

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)
return !session.IsNew
}

func CreateSessionID(w http.ResponseWriter, r *http.Request, value string) error {
session, _ := Store.Get(r, env.SESSION_NAME)
session.Values["session-id"] = value
return session.Save(r, w)
}

0 comments on commit deef25f

Please sign in to comment.