Skip to content

Commit

Permalink
support set session max-age
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick0308 committed Oct 4, 2023
1 parent 62acfee commit f393ddc
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 358 deletions.
12 changes: 5 additions & 7 deletions .traefik.yml
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/longbridgeapp/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: 1000
66 changes: 0 additions & 66 deletions demo.go

This file was deleted.

49 changes: 0 additions & 49 deletions demo_test.go

This file was deleted.

32 changes: 32 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.3"

services:

traefik:
image: "traefik:v2.10"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--experimental.localPlugins.session-max-age.moduleName=github.com/longbridgeapp/traefik-session-max-age"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- '.:/plugins-local/src/github.com/longbridgeapp/traefik-session-max-age'
whoami:
image: "traefik/whoami"
container_name: "simple-service"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=session-max-age@docker"
- "traefik.http.middlewares.session-max-age.plugin.session-max-age.CookieName=_traefik_session"
- "traefik.http.middlewares.session-max-age.plugin.session-max-age.MaxAge=10000"
- "traefik.http.services.whoami.loadBalancer.sticky.cookie=true"
- "traefik.http.services.whoami.loadBalancer.sticky.cookie.name=_traefik_session"
- "traefik.http.services.whoami.loadbalancer.server.port=80"
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.3"

services:

traefik:
image: "traefik:v2.10"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--experimental.plugins.session-max-age.modulename=github.com/longbridgeapp/traefik-session-max-age"
- "--experimental.plugins.session-max-age.version=v0.1.2"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
whoami:
image: "traefik/whoami"
container_name: "simple-service"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=session-max-age@docker"
- "traefik.http.middlewares.session-max-age.plugin.session-max-age.CookieName=_traefik_session"
- "traefik.http.middlewares.session-max-age.plugin.session-max-age.MaxAge=10000"
- "traefik.http.services.whoami.loadBalancer.sticky.cookie=true"
- "traefik.http.services.whoami.loadBalancer.sticky.cookie.name=_traefik_session"
- "traefik.http.services.whoami.loadbalancer.server.port=80"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/traefik/plugindemo
module github.com/longbridgeapp/traefik-session-max-age

go 1.19
71 changes: 71 additions & 0 deletions max_age.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package traefik_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)
}
8 changes: 8 additions & 0 deletions max_age_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package traefik_session_max_age

import (
"testing"
)

func TestDemo(t *testing.T) {
}
Loading

0 comments on commit f393ddc

Please sign in to comment.