Skip to content

Commit

Permalink
feat: dynamicly add query params func
Browse files Browse the repository at this point in the history
  • Loading branch information
huaxiabuluo committed May 11, 2022
1 parent ac530e5 commit 9b4e37c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server-v2/api/studio/etc/studio-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Debug:
Enable: false
Auth:
AccessSecret: "login_secret"
AccessExpire: 7200
AccessExpire: 1800
File:
UploadDir: "./upload/"
8 changes: 2 additions & 6 deletions server-v2/api/studio/pkg/auth/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ func AuthMiddlewareWithConfig(config *config.Config) rest.Middleware {
MaxAge: 1800,
}

query := r.URL.Query()
query.Set("nebulaVersion", string(clientInfo.NebulaVersion))
r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode())
utils.AddQueryParams(r, map[string]string{"nebulaVersion": string(clientInfo.NebulaVersion)})

// w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Set-Cookie", token.String())
Expand All @@ -127,9 +125,7 @@ func AuthMiddlewareWithConfig(config *config.Config) rest.Middleware {
nsidCookie, err := r.Cookie("nsid")

if err == nil {
query := r.URL.Query()
query.Set("nsid", nsidCookie.Value)
r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode())
utils.AddQueryParams(r, map[string]string{"nsid": nsidCookie.Value})
}

w.Header().Set("Set-Cookie", utils.DisabledCookie("token").String())
Expand Down
9 changes: 9 additions & 0 deletions server-v2/api/studio/pkg/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func DisabledCookie(name string) *http.Cookie {
MaxAge: -1,
}
}

// dynamicly add query params to the request
func AddQueryParams(r *http.Request, params map[string]string) {
query := r.URL.Query()
for k, v := range params {
query.Set(k, v)
}
r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode())
}
1 change: 0 additions & 1 deletion server-v2/api/studio/studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func main() {
defer server.Stop()

// global middleware
// server.Use(auth.AuthMiddleware)
server.Use(auth.AuthMiddlewareWithConfig(&c))

// api handlers
Expand Down
2 changes: 2 additions & 0 deletions server-v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
)

require (
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-logr/logr v1.2.2 // indirect
Expand All @@ -36,6 +37,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.opentelemetry.io/otel v1.3.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.3.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions server-v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGn
github.com/alicebob/miniredis/v2 v2.17.0/go.mod h1:gquAfGbzn92jvtrSC69+6zZnwSODVXVpYDRaGhWaL6I=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -360,6 +362,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca h1:NugYot0LIVPxTvN8n+Kvkn6TrbMyxQiuvKdEwFdR9vI=
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU=
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b h1:gQZ0qzfKHQIybLANtM3mBXNUtOfsCFXeTsnBqCsx1KM=
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down

0 comments on commit 9b4e37c

Please sign in to comment.