Skip to content

Commit

Permalink
Use Go's new native mux instead of gorilla/mux.
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Apr 29, 2024
1 parent 861a7ab commit e3233b3
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 138 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/dchest/uniuri v1.2.0
github.com/goburrow/modbus v0.1.0
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/jung-kurt/gofpdf v1.16.2
github.com/mitchellh/mapstructure v1.5.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
Expand Down
10 changes: 3 additions & 7 deletions web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/Team254/cheesy-arena/partner"
"github.com/Team254/cheesy-arena/playoff"
"github.com/Team254/cheesy-arena/websocket"
"github.com/gorilla/mux"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -50,8 +49,7 @@ type allianceMatchup struct {

// Generates a JSON dump of the matches and results.
func (web *Web) matchesApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
matchType, err := model.MatchTypeFromString(vars["type"])
matchType, err := model.MatchTypeFromString(r.PathValue("type"))
if err != nil {
handleWebErr(w, err)
return
Expand Down Expand Up @@ -217,8 +215,7 @@ func (web *Web) arenaWebsocketApiHandler(w http.ResponseWriter, r *http.Request)

// Serves the avatar for a given team, or a default if none exists.
func (web *Web) teamAvatarsApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
teamId, err := strconv.Atoi(vars["teamId"])
teamId, err := strconv.Atoi(r.PathValue("teamId"))
if err != nil {
handleWebErr(w, err)
return
Expand Down Expand Up @@ -250,8 +247,7 @@ func (web *Web) bracketSvgApiHandler(w http.ResponseWriter, r *http.Request) {
}

func (web *Web) gridSvgApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
alliance := vars["alliance"]
alliance := r.PathValue("alliance")
var grid game.Grid
if alliance == "red" {
grid = web.arena.RedRealtimeScore.CurrentScore.Grid
Expand Down
6 changes: 2 additions & 4 deletions web/match_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/gorilla/mux"
)

type MatchLogsListItem struct {
Expand Down Expand Up @@ -135,9 +134,8 @@ func (web *Web) matchLogsViewGetHandler(w http.ResponseWriter, r *http.Request)

// Load the match result for the match referenced in the HTTP query string.
func (web *Web) getMatchLogFromRequest(r *http.Request) (*model.Match, *MatchLogs, bool, error) {
vars := mux.Vars(r)
matchId, _ := strconv.Atoi(vars["matchId"])
stationId := vars["stationId"]
matchId, _ := strconv.Atoi(r.PathValue("matchId"))
stationId := r.PathValue("stationId")
match, err := web.arena.Database.GetMatchById(matchId)

logs := MatchLogs{
Expand Down
7 changes: 2 additions & 5 deletions web/match_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/gorilla/mux"
"net/http"
"strconv"
)
Expand Down Expand Up @@ -150,14 +149,12 @@ func (web *Web) matchReviewEditPostHandler(w http.ResponseWriter, r *http.Reques

// Load the match result for the match referenced in the HTTP query string.
func (web *Web) getMatchResultFromRequest(r *http.Request) (*model.Match, *model.MatchResult, bool, error) {
vars := mux.Vars(r)

// If editing the current match, get it from memory instead of the DB.
if vars["matchId"] == "current" {
if r.PathValue("matchId") == "current" {
return web.arena.CurrentMatch, web.getCurrentMatchResult(), true, nil
}

matchId, _ := strconv.Atoi(vars["matchId"])
matchId, _ := strconv.Atoi(r.PathValue("matchId"))
match, err := web.arena.Database.GetMatchById(matchId)
if err != nil {
return nil, nil, false, err
Expand Down
10 changes: 3 additions & 7 deletions web/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/Team254/cheesy-arena/model"
"github.com/Team254/cheesy-arena/playoff"
"github.com/Team254/cheesy-arena/tournament"
"github.com/gorilla/mux"
"github.com/jung-kurt/gofpdf"
)

Expand Down Expand Up @@ -361,8 +360,7 @@ func drawPdfLogo(pdf gofpdf.Pdf, x float64, y float64, width float64) {

// Generates a CSV-formatted report of the match schedule.
func (web *Web) scheduleCsvReportHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
matchType, err := model.MatchTypeFromString(vars["type"])
matchType, err := model.MatchTypeFromString(r.PathValue("type"))
if err != nil {
handleWebErr(w, err)
return
Expand Down Expand Up @@ -390,8 +388,7 @@ func (web *Web) scheduleCsvReportHandler(w http.ResponseWriter, r *http.Request)

// Generates a PDF-formatted report of the match schedule.
func (web *Web) schedulePdfReportHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
matchType, err := model.MatchTypeFromString(vars["type"])
matchType, err := model.MatchTypeFromString(r.PathValue("type"))
if err != nil {
handleWebErr(w, err)
return
Expand Down Expand Up @@ -792,8 +789,7 @@ func surrogateText(isSurrogate bool) string {

// Generates a PDF-formatted report of the match cycle times.
func (web *Web) cyclePdfReportHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
matchType, err := model.MatchTypeFromString(vars["type"])
matchType, err := model.MatchTypeFromString(r.PathValue("type"))
if err != nil {
handleWebErr(w, err)
return
Expand Down
7 changes: 2 additions & 5 deletions web/scoring_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/Team254/cheesy-arena/websocket"
"github.com/gorilla/mux"
"github.com/mitchellh/mapstructure"
"io"
"log"
Expand All @@ -24,8 +23,7 @@ func (web *Web) scoringPanelHandler(w http.ResponseWriter, r *http.Request) {
return
}

vars := mux.Vars(r)
alliance := vars["alliance"]
alliance := r.PathValue("alliance")
if alliance != "red" && alliance != "blue" {
handleWebErr(w, fmt.Errorf("Invalid alliance '%s'.", alliance))
return
Expand Down Expand Up @@ -55,8 +53,7 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
return
}

vars := mux.Vars(r)
alliance := vars["alliance"]
alliance := r.PathValue("alliance")
if alliance != "red" && alliance != "blue" {
handleWebErr(w, fmt.Errorf("Invalid alliance '%s'.", alliance))
return
Expand Down
10 changes: 3 additions & 7 deletions web/setup_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"github.com/Team254/cheesy-arena/model"
"github.com/dchest/uniuri"
"github.com/gorilla/mux"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -114,8 +113,7 @@ func (web *Web) teamEditGetHandler(w http.ResponseWriter, r *http.Request) {
return
}

vars := mux.Vars(r)
teamId, _ := strconv.Atoi(vars["id"])
teamId, _ := strconv.Atoi(r.PathValue("id"))
team, err := web.arena.Database.GetTeamById(teamId)
if err != nil {
handleWebErr(w, err)
Expand Down Expand Up @@ -148,8 +146,7 @@ func (web *Web) teamEditPostHandler(w http.ResponseWriter, r *http.Request) {
return
}

vars := mux.Vars(r)
teamId, _ := strconv.Atoi(vars["id"])
teamId, _ := strconv.Atoi(r.PathValue("id"))
team, err := web.arena.Database.GetTeamById(teamId)
if err != nil {
handleWebErr(w, err)
Expand Down Expand Up @@ -195,8 +192,7 @@ func (web *Web) teamDeletePostHandler(w http.ResponseWriter, r *http.Request) {
return
}

vars := mux.Vars(r)
teamId, _ := strconv.Atoi(vars["id"])
teamId, _ := strconv.Atoi(r.PathValue("id"))
team, err := web.arena.Database.GetTeamById(teamId)
if err != nil {
handleWebErr(w, err)
Expand Down
Loading

0 comments on commit e3233b3

Please sign in to comment.