switched to gin
This commit is contained in:
29
go.mod
29
go.mod
@@ -1,22 +1,23 @@
|
||||
module git.harting.dev/csgowtf/csgowtfd
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
entgo.io/ent v0.10.1
|
||||
github.com/an0nfunc/go-steam/v3 v3.0.6
|
||||
github.com/an0nfunc/go-steamapi v1.1.0
|
||||
github.com/gin-contrib/cors v1.3.1
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/go-redis/cache/v8 v8.4.3
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
|
||||
github.com/gorilla/handlers v1.5.1
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/jackc/pgx/v4 v4.15.0
|
||||
github.com/markus-wa/demoinfocs-golang/v2 v2.13.0
|
||||
github.com/markus-wa/demoinfocs-golang/v2 v2.13.2
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/wercker/journalhook v0.0.0-20180428041537-5d0a5ae867b3
|
||||
golang.org/x/text v0.3.7
|
||||
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
|
||||
golang.org/x/time v0.0.0-20220411224347-583f2d630306
|
||||
google.golang.org/protobuf v1.28.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
)
|
||||
@@ -24,15 +25,18 @@ require (
|
||||
require (
|
||||
ariga.io/atlas v0.3.7 // indirect
|
||||
github.com/agext/levenshtein v1.2.3 // indirect
|
||||
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
|
||||
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/felixge/httpsnoop v1.0.2 // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-openapi/inflect v0.19.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.10.1 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/go-cmp v0.5.7 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
|
||||
@@ -43,21 +47,28 @@ require (
|
||||
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||
github.com/jackc/pgtype v1.10.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.15.1 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/markus-wa/go-unassert v0.1.2 // indirect
|
||||
github.com/markus-wa/gobitread v0.2.3 // indirect
|
||||
github.com/markus-wa/godispatch v1.4.1 // indirect
|
||||
github.com/markus-wa/ice-cipher-go v0.0.0-20220126215401-a6adadccc817 // indirect
|
||||
github.com/markus-wa/quickhull-go/v2 v2.1.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
github.com/zclconf/go-cty v1.10.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
|
||||
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb // indirect
|
||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
||||
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 // indirect
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
301
main.go
301
main.go
@@ -16,9 +16,10 @@ import (
|
||||
"git.harting.dev/csgowtf/csgowtfd/ent/migrate"
|
||||
"git.harting.dev/csgowtf/csgowtfd/ent/player"
|
||||
"git.harting.dev/csgowtf/csgowtfd/utils"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-redis/cache/v8"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
"github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
|
||||
@@ -225,11 +226,9 @@ func housekeeping() {
|
||||
}
|
||||
}
|
||||
|
||||
func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
l := mux.Vars(r)["limit"]
|
||||
func getPlayerMeta(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
l := c.Param("limit")
|
||||
|
||||
var limit int
|
||||
var err error
|
||||
@@ -237,7 +236,7 @@ func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
limit, err = strconv.Atoi(l)
|
||||
if err != nil {
|
||||
log.Infof("[GPM] limit not an int: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -246,14 +245,14 @@ func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if limit > 10 {
|
||||
log.Infof("[GPM] limit out of bounds: %d", limit)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tPlayer, err := utils.Player(db, id, conf.Steam.APIKey, nil)
|
||||
if err != nil {
|
||||
log.Infof("[GPM] Player not found: %+v", err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -263,7 +262,7 @@ func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
metaStats, err = utils.GetMetaStats(tPlayer)
|
||||
if err != nil {
|
||||
log.Infof("[GPM] Unable to get MetaStats: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -275,7 +274,7 @@ func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("[GPM] Failure saving to cache: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Debugf("[GPM] SideMetaStats for %d saved to cache", tPlayer.ID)
|
||||
@@ -304,7 +303,7 @@ func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
tP, err := utils.Player(db, p.Player.SteamID64, conf.Steam.APIKey, nil)
|
||||
if err != nil {
|
||||
log.Warningf("[GPM] Failure getting player: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
p.Player.Avatar = tP.Avatar
|
||||
@@ -324,25 +323,20 @@ func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
err = utils.SendJSON(metaStats, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GPM] Unable to marshal JSON: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, metaStats)
|
||||
}
|
||||
|
||||
func getPlayer(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
func getPlayer(c *gin.Context) {
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
t := mux.Vars(r)["time"]
|
||||
id := c.Param("id")
|
||||
t := c.Param("time")
|
||||
|
||||
var offsetTime time.Time
|
||||
if t != "" {
|
||||
unixOffset, err := strconv.ParseInt(t, 10, 64)
|
||||
if err != nil {
|
||||
log.Infof("[GP] offset not an int: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -352,7 +346,7 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
|
||||
tPlayer, err := utils.Player(db, id, conf.Steam.APIKey, nil)
|
||||
if err != nil || tPlayer == nil {
|
||||
log.Infof("[GP] Player %s not found (%+v)", id, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -382,12 +376,7 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if err != nil || len(tMatches) == 0 {
|
||||
log.Debugf("[GP] No matches found for player %s", id)
|
||||
err := utils.SendJSON(response, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GP] Unable to marshal JSON: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -448,36 +437,23 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
|
||||
response.Matches = append(response.Matches, mResponse)
|
||||
}
|
||||
|
||||
err = utils.SendJSON(response, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GP] Unable to marshal JSON: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func deletePlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Infof("[DPT] Unable to parse form data: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
authCode := r.Form.Get("authcode")
|
||||
func deletePlayerTrack(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
authCode := c.PostForm("authcode")
|
||||
|
||||
if id == "" || authCode == "" || !utils.AuthCodeRegEx.MatchString(authCode) {
|
||||
log.Infof("[PPTM] invalid arguments: %+v, %+v", id, authCode)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tPlayer, err := utils.Player(db, id, conf.Steam.APIKey, nil)
|
||||
if err != nil {
|
||||
log.Infof("[PPT] player not found: %+v", err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -486,11 +462,11 @@ func deletePlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
switch e := err.(type) {
|
||||
case utils.AuthcodeUnauthorizedError:
|
||||
log.Infof("[DPT] authCode provided for player %s is invalid: %v", id, e)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
c.Status(http.StatusUnauthorized)
|
||||
return
|
||||
default:
|
||||
log.Infof("[DPT] Temporary Steam-API problem: %v", e)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
c.Status(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -498,37 +474,28 @@ func deletePlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background())
|
||||
if err != nil {
|
||||
log.Warningf("[PPT] update player failed: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func postPlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Infof("[PPT] Unable to parse form data: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
authCode := strings.TrimSpace(r.Form.Get("authcode"))
|
||||
shareCode := strings.TrimSpace(r.Form.Get("sharecode"))
|
||||
func postPlayerTrack(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
authCode := strings.TrimSpace(c.PostForm("authcode"))
|
||||
shareCode := strings.TrimSpace(c.PostForm("sharecode"))
|
||||
|
||||
if id == "" || authCode == "" || !utils.AuthCodeRegEx.MatchString(authCode) {
|
||||
log.Infof("[PPT] invalid arguments: %+v, %+v, %+v", id, authCode, shareCode)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tPlayer, err := utils.Player(db, id, conf.Steam.APIKey, rL)
|
||||
if err != nil {
|
||||
log.Infof("[PPT] player not found: %+v", err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -536,7 +503,7 @@ func postPlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
err := demoLoader.LoadDemo(&csgo.Demo{ShareCode: shareCode})
|
||||
if err != nil {
|
||||
log.Warningf("[PPT] unable to queue match: %v", err)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
c.Status(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -546,15 +513,15 @@ func postPlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
switch e := err.(type) {
|
||||
case utils.AuthcodeUnauthorizedError:
|
||||
log.Infof("[PPT] authCode provided for player %s is invalid: %v", id, e)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
c.Status(http.StatusUnauthorized)
|
||||
return
|
||||
case utils.SharecodeNoMatchError:
|
||||
log.Infof("[PPT] shareCode provided for player %s (%s) is invalid: %v", id, shareCode, e)
|
||||
w.WriteHeader(http.StatusPreconditionFailed)
|
||||
c.Status(http.StatusPreconditionFailed)
|
||||
return
|
||||
default:
|
||||
log.Infof("[PPT] problem with request: %v", e)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
c.Status(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -562,7 +529,7 @@ func postPlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
err = tPlayer.Update().SetAuthCode(authCode).Exec(context.Background())
|
||||
if err != nil {
|
||||
log.Warningf("[PPT] update player failed: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -570,22 +537,20 @@ func postPlayerTrack(w http.ResponseWriter, r *http.Request) {
|
||||
err := demoLoader.LoadDemo(&csgo.Demo{ShareCode: shareCode})
|
||||
if err != nil {
|
||||
log.Warningf("[PPT] unable to queue match: %v", err)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
c.Status(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
c.Status(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func getMatchParse(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
shareCode := mux.Vars(r)["sharecode"]
|
||||
func getMatchParse(c *gin.Context) {
|
||||
shareCode := c.Param("sharecode")
|
||||
|
||||
if shareCode == "" || !utils.ShareCodeRegEx.MatchString(shareCode) {
|
||||
log.Infof("[PPTM] invalid arguments: %s", shareCode)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -594,34 +559,32 @@ func getMatchParse(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
log.Warningf("[PPTM] unable to queue match: %v", err)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
c.Status(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
c.Status(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func getMatchRounds(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
func getMatchRounds(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
matchId, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
log.Infof("[GMR] Error parsing matchID %s: %v", id, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchId))).All(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GMR] match %d not found: %+v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -643,38 +606,32 @@ func getMatchRounds(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
err = utils.SendJSON(resp, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GMR] JSON: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
trans := r.FormValue("translate")
|
||||
func getMatchChat(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
trans := c.PostForm("translate")
|
||||
|
||||
if id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tag, weights, err := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
|
||||
tag, weights, err := language.ParseAcceptLanguage(c.GetHeader("Accept-Language"))
|
||||
if err != nil {
|
||||
log.Warningf("[GMC] Unable to parse Accept-Language %s: %v", r.Header.Get("Accept-Language"), err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
log.Warningf("[GMC] Unable to parse Accept-Language %s: %v", c.GetHeader("Accept-Language"), err)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
lang, _ := tag[0].Base()
|
||||
log.Debugf("[GMC] Got header %s, selected %s from %v (w: %v)", r.Header.Get("Accept-Language"), lang, tag, weights)
|
||||
log.Debugf("[GMC] Got header %s, selected %s from %v (w: %v)", c.GetHeader("Accept-Language"), lang, tag, weights)
|
||||
|
||||
var translate bool
|
||||
if trans != "" {
|
||||
translate, err = strconv.ParseBool(trans)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -682,7 +639,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
matchId, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
log.Infof("[GMC] Error parsing matchID %s: %v", id, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -693,7 +650,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GMC] match %d not found: %+v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -741,7 +698,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("[GMC] Failure saving to cache: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -749,7 +706,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GMC] match %d not found: %+v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -767,34 +724,28 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
err = utils.SendJSON(resp, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GMC] JSON: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
func getMatchWeapons(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
matchId, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
log.Infof("[GMW] Error parsing matchID %s: %v", id, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchId))).All(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GMW] match %d not found: %+v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -859,24 +810,18 @@ func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
|
||||
mResponse.Spray = append(mResponse.Spray, rSprays)
|
||||
}
|
||||
|
||||
err = utils.SendJSON(mResponse, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GMW] JSON: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, mResponse)
|
||||
}
|
||||
|
||||
func getMatches(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
t := mux.Vars(r)["time"]
|
||||
func getMatches(c *gin.Context) {
|
||||
t := c.Param("time")
|
||||
|
||||
var offsetTime time.Time
|
||||
if t != "" {
|
||||
unixOffset, err := strconv.ParseInt(t, 10, 64)
|
||||
if err != nil {
|
||||
log.Infof("[GMS] offset not an int: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -894,12 +839,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if err != nil || len(tMatches) == 0 {
|
||||
log.Debug("[GMS] No matches found")
|
||||
err := utils.SendJSON(mResponse, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GMS] Unable to marshal JSON: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, mResponse)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -934,34 +874,28 @@ func getMatches(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
err = utils.SendJSON(mResponse, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GM] JSON: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, mResponse)
|
||||
}
|
||||
|
||||
func getMatch(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
func getMatch(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
matchId, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
log.Infof("[GM] Unable to parse matchID %s: %v", id, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tMatch, err := db.Match.Query().Where(match.ID(matchId)).Only(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GM] match %d not found: %v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1001,7 +935,7 @@ func getMatch(w http.ResponseWriter, r *http.Request) {
|
||||
tStats, err := tMatch.QueryStats().WithPlayers().All(context.Background())
|
||||
if err != nil {
|
||||
log.Errorf("[GM] Unable to find stats for match %d: %v", tMatch.ID, err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1063,11 +997,7 @@ func getMatch(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
mResponse.Stats = tmpStats
|
||||
|
||||
err = utils.SendJSON(mResponse, w)
|
||||
if err != nil {
|
||||
log.Errorf("[GM] JSON: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
c.JSON(http.StatusOK, mResponse)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1174,50 +1104,25 @@ func main() {
|
||||
// start housekeeper
|
||||
go housekeeping()
|
||||
|
||||
// routes
|
||||
router = mux.NewRouter().StrictSlash(true)
|
||||
router.HandleFunc("/player/{id}", func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc("/player/{id}", getPlayer).Methods(http.MethodGet)
|
||||
r := gin.Default()
|
||||
config := cors.DefaultConfig()
|
||||
config.AllowOrigins = conf.Httpd.CORSAllowDomains
|
||||
|
||||
router.HandleFunc(`/player/{id}/next/{time:\d+}`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/player/{id}/next/{time:\d+}`, getPlayer).Methods(http.MethodGet)
|
||||
r.Use(cors.New(config))
|
||||
|
||||
router.HandleFunc(`/player/{id}/meta/{limit:\d+}`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/player/{id}/meta/{limit:\d+}`, getPlayerMeta).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc("/player/{id}/meta", func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc("/player/{id}/meta", getPlayerMeta).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc("/player/{id}/track", func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc("/player/{id}/track", postPlayerTrack).Methods(http.MethodPost)
|
||||
router.HandleFunc("/player/{id}/track", deletePlayerTrack).Methods(http.MethodDelete)
|
||||
|
||||
router.HandleFunc("/match/parse/{sharecode}", func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc(`/match/{id:\d{19}}`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/match/{id:\d{19}}`, getMatch).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc(`/match/{id:\d{19}}/weapons`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/match/{id:\d{19}}/weapons`, getMatchWeapons).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc(`/match/{id:\d{19}}/rounds`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/match/{id:\d{19}}/rounds`, getMatchRounds).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc(`/match/{id:\d{19}}/chat`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/match/{id:\d{19}}/chat`, getMatchChat).Methods(http.MethodGet)
|
||||
router.HandleFunc(`/match/{id:\d{19}}/chat`, getMatchChat).Queries("translate", "{translate}").Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc(`/matches`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/matches`, getMatches).Methods(http.MethodGet)
|
||||
|
||||
router.HandleFunc(`/matches/next/{time:\d+}`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
|
||||
router.HandleFunc(`/matches/next/{time:\d+}`, getMatches).Methods(http.MethodGet)
|
||||
|
||||
router.Use(mux.CORSMethodMiddleware(router))
|
||||
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
||||
proxyRouter := handlers.ProxyHeaders(loggedRouter)
|
||||
cors := handlers.CORS(handlers.AllowedOrigins(conf.Httpd.CORSAllowDomains))
|
||||
r.GET("/player/:id", getPlayer)
|
||||
r.GET("/player/:id/next/:time", getPlayer)
|
||||
r.GET("/player/:id/meta/:limit", getPlayerMeta)
|
||||
r.GET("/player/:id/meta", getPlayerMeta)
|
||||
r.POST("/player/:id/track", postPlayerTrack)
|
||||
r.DELETE("/player/:id/track", deletePlayerTrack)
|
||||
r.GET("/match/parse/:sharecode", getMatchParse)
|
||||
r.GET("/match/:id", getMatch)
|
||||
r.GET("/match/:id/weapons", getMatchWeapons)
|
||||
r.GET("/match/:id/rounds", getMatchRounds)
|
||||
r.GET("/match/:id/chat", getMatchChat)
|
||||
r.GET("/matches", getMatches)
|
||||
r.GET("/matches/next/:time")
|
||||
|
||||
log.Info("Start listening...")
|
||||
|
||||
@@ -1236,7 +1141,7 @@ func main() {
|
||||
ReadTimeout: time.Duration(conf.Csgowtfd.Timeout.Read) * time.Second,
|
||||
WriteTimeout: time.Duration(conf.Csgowtfd.Timeout.Write) * time.Second,
|
||||
IdleTimeout: time.Duration(conf.Csgowtfd.Timeout.Idle) * time.Second,
|
||||
Handler: cors(proxyRouter),
|
||||
Handler: r,
|
||||
}
|
||||
_ = srv.Serve(sL)
|
||||
}()
|
||||
@@ -1251,7 +1156,7 @@ func main() {
|
||||
ReadTimeout: time.Duration(conf.Csgowtfd.Timeout.Read) * time.Second,
|
||||
WriteTimeout: time.Duration(conf.Csgowtfd.Timeout.Write) * time.Second,
|
||||
IdleTimeout: time.Duration(conf.Csgowtfd.Timeout.Idle) * time.Second,
|
||||
Handler: cors(proxyRouter),
|
||||
Handler: r,
|
||||
}
|
||||
err = srv.Serve(tL)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user