diff --git a/404.html b/404.html index 6a24a76b..d7e7d1ed 100644 --- a/404.html +++ b/404.html @@ -10,7 +10,7 @@ - +
diff --git a/assets/js/141b670d.976e4019.js b/assets/js/141b670d.976e4019.js deleted file mode 100644 index 5737b22b..00000000 --- a/assets/js/141b670d.976e4019.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[3740],{7605:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>c,contentTitle:()=>a,default:()=>u,frontMatter:()=>t,metadata:()=>r,toc:()=>d});var i=s(5893),o=s(1151);const t={description:"Session middleware"},a="Session",r={id:"middleware/session",title:"Session",description:"Session middleware",source:"@site/docs/middleware/session.md",sourceDirName:"middleware",slug:"/middleware/session",permalink:"/docs/middleware/session",draft:!1,unlisted:!1,editUrl:"https://github.com/labstack/echox/blob/master/website/docs/middleware/session.md",tags:[],version:"current",frontMatter:{description:"Session middleware"},sidebar:"docsSidebar",previous:{title:"Secure",permalink:"/docs/middleware/secure"},next:{title:"Static",permalink:"/docs/middleware/static"}},c={},d=[{value:"Dependencies",id:"dependencies",level:2},{value:"Usage",id:"usage",level:2},{value:"Custom Configuration",id:"custom-configuration",level:2},{value:"Usage",id:"usage-1",level:3},{value:"Configuration",id:"configuration",level:2},{value:"Default Configuration",id:"default-configuration",level:3}];function l(e){const n={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,o.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h1,{id:"session",children:"Session"}),"\n",(0,i.jsxs)(n.p,{children:["Session middleware facilitates HTTP session management backed by ",(0,i.jsx)(n.a,{href:"https://github.com/gorilla/sessions",children:"gorilla sessions"}),". The default implementation provides cookie and\nfilesystem based session store; however, you can take advantage of ",(0,i.jsx)(n.a,{href:"https://github.com/gorilla/sessions#store-implementations",children:"community maintained\nimplementation"})," for various backends."]}),"\n",(0,i.jsx)(n.admonition,{type:"note",children:(0,i.jsx)(n.p,{children:"Echo community contribution"})}),"\n",(0,i.jsx)(n.h2,{id:"dependencies",children:"Dependencies"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:'import (\n "github.com/gorilla/sessions"\n "github.com/labstack/echo-contrib/session"\n)\n'})}),"\n",(0,i.jsx)(n.h2,{id:"usage",children:"Usage"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:'e := echo.New()\ne.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))\n\ne.GET("/", func(c echo.Context) error {\n sess, _ := session.Get("session", c)\n sess.Options = &sessions.Options{\n Path: "/",\n MaxAge: 86400 * 7,\n HttpOnly: true,\n }\n sess.Values["foo"] = "bar"\n sess.Save(c.Request(), c.Response())\n return c.NoContent(http.StatusOK)\n})\n'})}),"\n",(0,i.jsx)(n.h2,{id:"custom-configuration",children:"Custom Configuration"}),"\n",(0,i.jsx)(n.h3,{id:"usage-1",children:"Usage"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:"e := echo.New()\ne.Use(session.MiddlewareWithConfig(session.Config{}))\n"})}),"\n",(0,i.jsx)(n.h2,{id:"configuration",children:"Configuration"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:"Config struct {\n // Skipper defines a function to skip middleware.\n Skipper middleware.Skipper\n\n // Session store.\n // Required.\n Store sessions.Store\n}\n"})}),"\n",(0,i.jsx)(n.h3,{id:"default-configuration",children:"Default Configuration"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:"DefaultConfig = Config{\n Skipper: DefaultSkipper,\n}\n"})})]})}function u(e={}){const{wrapper:n}={...(0,o.a)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(l,{...e})}):l(e)}},1151:(e,n,s)=>{s.d(n,{Z:()=>r,a:()=>a});var i=s(7294);const o={},t=i.createContext(o);function a(e){const n=i.useContext(t);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:a(e.components),i.createElement(t.Provider,{value:n},e.children)}}}]); \ No newline at end of file diff --git a/assets/js/141b670d.a38c4bcd.js b/assets/js/141b670d.a38c4bcd.js new file mode 100644 index 00000000..abcc0cdf --- /dev/null +++ b/assets/js/141b670d.a38c4bcd.js @@ -0,0 +1 @@ +"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[3740],{7605:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>c,contentTitle:()=>r,default:()=>h,frontMatter:()=>i,metadata:()=>a,toc:()=>l});var t=s(5893),o=s(1151);const i={description:"Session middleware"},r="Session",a={id:"middleware/session",title:"Session",description:"Session middleware",source:"@site/docs/middleware/session.md",sourceDirName:"middleware",slug:"/middleware/session",permalink:"/docs/middleware/session",draft:!1,unlisted:!1,editUrl:"https://github.com/labstack/echox/blob/master/website/docs/middleware/session.md",tags:[],version:"current",frontMatter:{description:"Session middleware"},sidebar:"docsSidebar",previous:{title:"Secure",permalink:"/docs/middleware/secure"},next:{title:"Static",permalink:"/docs/middleware/static"}},c={},l=[{value:"Dependencies",id:"dependencies",level:2},{value:"Usage",id:"usage",level:2},{value:"Example usage",id:"example-usage",level:3},{value:"Custom Configuration",id:"custom-configuration",level:2},{value:"Usage",id:"usage-1",level:3},{value:"Configuration",id:"configuration",level:2},{value:"Default Configuration",id:"default-configuration",level:3}];function d(e){const n={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,o.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.h1,{id:"session",children:"Session"}),"\n",(0,t.jsxs)(n.p,{children:["Session middleware facilitates HTTP session management backed by ",(0,t.jsx)(n.a,{href:"https://github.com/gorilla/sessions",children:"gorilla sessions"}),". The default implementation provides cookie and\nfilesystem based session store; however, you can take advantage of ",(0,t.jsx)(n.a,{href:"https://github.com/gorilla/sessions#store-implementations",children:"community maintained\nimplementation"})," for various backends."]}),"\n",(0,t.jsx)(n.admonition,{type:"note",children:(0,t.jsx)(n.p,{children:"Echo community contribution"})}),"\n",(0,t.jsx)(n.h2,{id:"dependencies",children:"Dependencies"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-go",children:'import (\n "github.com/gorilla/sessions"\n "github.com/labstack/echo-contrib/session"\n)\n'})}),"\n",(0,t.jsx)(n.h2,{id:"usage",children:"Usage"}),"\n",(0,t.jsxs)(n.p,{children:["This example exposes two endpoints: ",(0,t.jsx)(n.code,{children:"/create-session"})," creates new session and ",(0,t.jsx)(n.code,{children:"/read-session"})," read value from\nsession if request contains session id."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-go",children:'import (\n "errors"\n "fmt"\n "github.com/gorilla/sessions"\n "github.com/labstack/echo-contrib/session"\n "github.com/labstack/echo/v4"\n "log"\n "net/http"\n)\n\nfunc main() {\n\te := echo.New()\n\te.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))\n\n\te.GET("/create-session", func(c echo.Context) error {\n\t\tsess, err := session.Get("session", c)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tsess.Options = &sessions.Options{\n\t\t\tPath: "/",\n\t\t\tMaxAge: 86400 * 7,\n\t\t\tHttpOnly: true,\n\t\t}\n\t\tsess.Values["foo"] = "bar"\n\t\tif err := sess.Save(c.Request(), c.Response()); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn c.NoContent(http.StatusOK)\n\t})\n\n\te.GET("/read-session", func(c echo.Context) error {\n\t\tsess, err := session.Get("session", c)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn c.String(http.StatusOK, fmt.Sprintf("foo=%v\\n", sess.Values["foo"]))\n\t})\n\n\tif err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {\n\t\tlog.Fatal(err)\n\t}\n}\n'})}),"\n",(0,t.jsx)(n.h3,{id:"example-usage",children:"Example usage"}),"\n",(0,t.jsxs)(n.p,{children:["Requesting ",(0,t.jsx)(n.code,{children:"/read-session"})," without providing session it will output nil as ",(0,t.jsx)(n.code,{children:"foo"})," value"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-bash",children:"x@x:~/$ curl -v http://localhost:8080/read-session\n* processing: http://localhost:8080/read-session\n* Trying [::1]:8080...\n* Connected to localhost (::1) port 8080\n> GET /read-session HTTP/1.1\n> Host: localhost:8080\n> User-Agent: curl/8.2.1\n> Accept: */*\n> \n< HTTP/1.1 200 OK\n< Content-Type: text/plain; charset=UTF-8\n< Date: Thu, 25 Apr 2024 09:15:14 GMT\n< Content-Length: 10\n< \nfoo=import (
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
)
e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
e.GET("/", func(c echo.Context) error {
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
sess.Values["foo"] = "bar"
sess.Save(c.Request(), c.Response())
return c.NoContent(http.StatusOK)
})
This example exposes two endpoints: /create-session
creates new session and /read-session
read value from
+session if request contains session id.
import (
"errors"
"fmt"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"log"
"net/http"
)
func main() {
e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
e.GET("/create-session", func(c echo.Context) error {
sess, err := session.Get("session", c)
if err != nil {
return err
}
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
sess.Values["foo"] = "bar"
if err := sess.Save(c.Request(), c.Response()); err != nil {
return err
}
return c.NoContent(http.StatusOK)
})
e.GET("/read-session", func(c echo.Context) error {
sess, err := session.Get("session", c)
if err != nil {
return err
}
return c.String(http.StatusOK, fmt.Sprintf("foo=%v\n", sess.Values["foo"]))
})
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}
Requesting /read-session
without providing session it will output nil as foo
value
x@x:~/$ curl -v http://localhost:8080/read-session
* processing: http://localhost:8080/read-session
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET /read-session HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.2.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=UTF-8
< Date: Thu, 25 Apr 2024 09:15:14 GMT
< Content-Length: 10
<
foo=<nil>
Requesting /create-session
creates new session
x@x:~/$ curl -v -c cookies.txt http://localhost:8080/create-session
* processing: http://localhost:8080/create-session
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET /create-session HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.2.1
> Accept: */*
>
< HTTP/1.1 200 OK
* Added cookie session="MTcxNDAzNjYyMHxEWDhFQVFMX2dBQUJFQUVRQUFBZ180QUFBUVp6ZEhKcGJtY01CUUFEWm05dkJuTjBjbWx1Wnd3RkFBTmlZWEk9fHJQxR5fJDUEV-6iHSWuyVzjYX2f9F5tVaMGV6pjIE1Y" for domain localhost, path /, expire 1714641420
< Set-Cookie: session=MTcxNDAzNjYyMHxEWDhFQVFMX2dBQUJFQUVRQUFBZ180QUFBUVp6ZEhKcGJtY01CUUFEWm05dkJuTjBjbWx1Wnd3RkFBTmlZWEk9fHJQxR5fJDUEV-6iHSWuyVzjYX2f9F5tVaMGV6pjIE1Y; Path=/; Expires=Thu, 02 May 2024 09:17:00 GMT; Max-Age=604800; HttpOnly
< Date: Thu, 25 Apr 2024 09:17:00 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
Using session cookie from previous response and requesting /read-session
will output foo
value from session.
x@x:~/$ curl -v -b cookies.txt http://localhost:8080/read-session
* processing: http://localhost:8080/read-session
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET /read-session HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.2.1
> Accept: */*
> Cookie: session=MTcxNDAzNjYyMHxEWDhFQVFMX2dBQUJFQUVRQUFBZ180QUFBUVp6ZEhKcGJtY01CUUFEWm05dkJuTjBjbWx1Wnd3RkFBTmlZWEk9fHJQxR5fJDUEV-6iHSWuyVzjYX2f9F5tVaMGV6pjIE1Y
>
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=UTF-8
< Date: Thu, 25 Apr 2024 09:18:56 GMT
< Content-Length: 8
<
foo=bar
* Connection #0 to host localhost left intact
e := echo.New()
e.Use(session.MiddlewareWithConfig(session.Config{}))
Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Session store.
// Required.
Store sessions.Store
}
DefaultConfig = Config{
Skipper: DefaultSkipper,
}
DefaultConfig = Config{
Skipper: DefaultSkipper,
}