-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
124 lines (104 loc) · 3.04 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
"github.com/npflan/steam-gameserver-token-api/steam"
)
// App contains references to global necessities
type App struct {
Router *mux.Router
Log Log
}
// Log is a modifiable endpoint
type Log struct {
Error *log.Logger
Info *log.Logger
}
const defaultLogFormat = log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile | log.LUTC
// Run server on specific interface
func (a *App) Run(addr string) {
a.registerRoutes()
// set default log format if no custom format present
if a.Log.Info == nil {
log.New(os.Stdout, "INFO: ", defaultLogFormat)
}
if a.Log.Error == nil {
log.New(os.Stderr, "ERROR: ", defaultLogFormat)
}
log.Fatal(http.ListenAndServe(addr, a.Router))
}
func (a *App) registerRoutes() {
a.Router = mux.NewRouter().StrictSlash(true)
a.Router.HandleFunc("/", a.getHome).Methods("GET")
a.Router.HandleFunc("/token/{appID}/{memo}", a.pullToken).Methods("GET")
}
// RespondWithJSON uses a struct, for a JSON response.
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// RespondWithText returns text/plain.
func respondWithText(w http.ResponseWriter, code int, payload string) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(code)
w.Write([]byte(payload))
}
// RespondWithError standardizes error messages, through the use of RespondWithJSON.
func respondWithError(w http.ResponseWriter, code int, message string) {
fmt.Println(message)
respondWithJSON(w, code, map[string]string{"error": message})
}
func (a *App) getHome(w http.ResponseWriter, r *http.Request) {
}
func (a *App) pullToken(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if _, ok := vars["appID"]; !ok {
respondWithError(w, http.StatusBadRequest, "Missing appID")
return
}
if _, ok := vars["memo"]; !ok {
respondWithError(w, http.StatusBadRequest, "Missing memo")
return
}
appID, err := strconv.Atoi(vars["appID"])
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Sprintf("bad appID: %s", err))
return
}
accounts, err := steam.GetAccountList()
if err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to list existing tokens: %s", err))
return
}
// Check for existing account
var account steam.Account
for _, acct := range accounts {
if acct.Memo == vars["memo"] && int(acct.AppID) == appID {
account = acct
break
}
}
// Create new if not found
if account.SteamID == "" {
account, err = steam.CreateAccount(appID, vars["memo"])
}
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
// Refresh token if found and expired
if account.IsExpired == true {
account, err = steam.ResetLoginToken(account.SteamID)
}
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
}
respondWithText(w, http.StatusOK, account.LoginToken)
}