Skip to content

Commit

Permalink
init session max age
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick0308 authored and 白泽 committed Oct 4, 2023
1 parent 62acfee commit 4aebd2c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 357 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/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
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.

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/patrick0308/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 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 session_max_age_test

import (
"testing"
)

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

0 comments on commit 4aebd2c

Please sign in to comment.