generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62acfee
commit 4aebd2c
Showing
7 changed files
with
95 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
displayName: Demo Plugin | ||
displayName: Sticky Session Max Age | ||
type: middleware | ||
iconPath: .assets/icon.png | ||
|
||
import: github.com/traefik/plugindemo | ||
import: github.com/patrick0308/traefik-session-max-age | ||
|
||
summary: '[Demo] Add Request Header' | ||
summary: "Set sticky session cookie's max age" | ||
|
||
testData: | ||
Headers: | ||
X-Demo: test | ||
X-URL: '{{URL}}' | ||
cookieName: _traefik_session | ||
maxAge: 10000 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/traefik/plugindemo | ||
module github.com/patrick0308/traefik-session-max-age | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package session_max_age | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// Config the plugin configuration. | ||
type Config struct { | ||
CookieName string `json:"cookieName,omitempty"` | ||
MaxAge int `json:"maxAge,omitempty"` | ||
} | ||
|
||
// CreateConfig creates the default plugin configuration. | ||
func CreateConfig() *Config { | ||
return &Config{} | ||
} | ||
|
||
type ResponseWriterWrapper struct { | ||
http.ResponseWriter | ||
cookieName string | ||
maxAge int | ||
} | ||
|
||
func (rww ResponseWriterWrapper) Header() http.Header { | ||
return rww.ResponseWriter.Header() | ||
} | ||
|
||
func (rww ResponseWriterWrapper) WriteHeader(code int) { | ||
if rww.cookieName != "" { | ||
res := http.Response{Header: rww.ResponseWriter.Header()} | ||
cookies := res.Cookies() | ||
for _, cookie := range cookies { | ||
if cookie.Name == rww.cookieName { | ||
cookie.MaxAge = rww.maxAge | ||
http.SetCookie(rww.ResponseWriter, cookie) | ||
} | ||
} | ||
} | ||
rww.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
func (rww ResponseWriterWrapper) Write(b []byte) (int, error) { | ||
return rww.ResponseWriter.Write(b) | ||
} | ||
|
||
type HeaderWrapper http.Header | ||
|
||
func (h HeaderWrapper) Add(key, value string) { | ||
h.Add(key, value) | ||
} | ||
|
||
type SessionMaxAge struct { | ||
next http.Handler | ||
cookieName string | ||
maxAge int | ||
name string | ||
} | ||
|
||
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) { | ||
return &SessionMaxAge{ | ||
next: next, | ||
name: name, | ||
maxAge: config.MaxAge, | ||
cookieName: config.CookieName, | ||
}, nil | ||
} | ||
|
||
func (a *SessionMaxAge) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
a.next.ServeHTTP(ResponseWriterWrapper{ResponseWriter: rw, cookieName: a.cookieName, maxAge: a.maxAge}, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package session_max_age_test | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDemo(t *testing.T) { | ||
} |
Oops, something went wrong.