Skip to content

Commit

Permalink
refactor: remove github.com/gorilla/handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Dec 25, 2023
1 parent ad93d48 commit f8565b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ go 1.17
require (
github.com/bluele/gcache v0.0.2
github.com/go-chi/chi/v5 v5.0.11
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/jrudio/go-plex-client v0.0.0-20220106065909-9e1d590b99aa
github.com/xanderstrike/plexhooks v0.0.0-20200926011736-c63bcd35fe3e
)

require (
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ github.com/dgraph-io/badger/v3 v3.2103.2/go.mod h1:RHo4/GmYcKKh5Lxu63wLEMHJ70Pac
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
Expand Down Expand Up @@ -54,8 +52,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
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/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down
6 changes: 6 additions & 0 deletions handler/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ const (
headerPageStart = "X-Plex-Container-Start"
headerToken = "X-Plex-Token"
headerUserId = "X-Plex-User-Id"

headerAccept = "Accept"
headerAcceptLanguage = "Accept-Language"
headerCacheControl = "Cache-Control"
headerContentType = "Content-Type"
headerRange = "Range"
headerVary = "Vary"

headerForwardedFor = "X-Forwarded-For"
headerRealIP = "X-Real-IP"
headerForwardedProto = "X-Forwarded-Proto"
headerForwardedScheme = "X-Forwarded-Scheme"

cachePrefixDynamic = "dynamic"
cachePrefixStatic = "static"
cachePrefixPlex = "plex"
Expand Down
3 changes: 1 addition & 2 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"

"github.com/go-chi/chi/v5/middleware"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)

Expand All @@ -33,7 +32,7 @@ func init() {

func NewRouter() http.Handler {
r := mux.NewRouter()
r.Use(handlers.ProxyHeaders, normalizeMiddleware)
r.Use(normalizeMiddleware)
if !plexClient.NoRequestLogs {
r.Use(middleware.Logger)
}
Expand Down
28 changes: 28 additions & 0 deletions handler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,37 @@ func cloneRequest(r *http.Request, headers http.Header, query url.Values) *http.
nr.URL.RawQuery = query.Encode()
nr.RequestURI = nr.URL.RequestURI()
}
if fwd := getIP(headers); fwd != "" {
nr.RemoteAddr = fwd
}
if scheme := getScheme(headers); scheme != "" {
nr.URL.Scheme = scheme
}
return nr
}

func getIP(headers http.Header) (addr string) {
if fwd := headers.Get(headerForwardedFor); fwd != "" {
s := strings.Index(fwd, ", ")
if s == -1 {
s = len(fwd)
}
addr = fwd[:s]
} else if fwd = headers.Get(headerRealIP); fwd != "" {
addr = fwd
}
return
}

func getScheme(headers http.Header) (scheme string) {
if proto := headers.Get(headerForwardedProto); proto != "" {
scheme = strings.ToLower(proto)
} else if proto = headers.Get(headerForwardedScheme); proto != "" {
scheme = strings.ToLower(proto)
}
return
}

func getAcceptContentType(r *http.Request) string {
accept := r.Header.Get(headerAccept)
if accept == "" {
Expand Down

0 comments on commit f8565b3

Please sign in to comment.