Skip to content

Commit

Permalink
feat: add session middleware (#2)
Browse files Browse the repository at this point in the history
* feat: add basic session middleware

* ci: add redis image

* feat: add redis and cookie

* docs: add README.md and README_CN.md

* docs: update translate

* style: change ccontext -> context
remove useless judgment

* feat: update hertz version

* feat: add licenses

* style: change DefaultKey

* style: remove useless import

* style: fix typo

* docs: update README.md

* docs: update README.md

* style: fix typos

* style: fix typos
  • Loading branch information
Skyenought authored Aug 29, 2022
1 parent 52b3b3e commit 915d8b6
Show file tree
Hide file tree
Showing 24 changed files with 1,886 additions and 3 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ on: [push, pull_request]

jobs:
lint-and-ut:
runs-on: self-hosted
runs-on: ubuntu-latest
env:
GO111MODULE: on
GOPROXY: https://proxy.golang.org
steps:
- name: Start Redis
uses: supercharge/[email protected]
with:
redis-version: 4

- uses: actions/checkout@v3

- name: Set up Go
Expand Down
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
.idea/
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# goland
.idea
# vscode
.vscode
35 changes: 35 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
header:
license:
spdx-id: Apache-2.0
copyright-owner: CloudWeGo Authors
content: |
MIT License
Copyright (c) 2019 Gin-Gonic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This file may have been modified by CloudWeGo authors. All CloudWeGo
Modifications are Copyright 2022 CloudWeGo Authors.
paths:
- '**/*.go'
- '**/*.s'
-
comment: on-failure
5 changes: 5 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CloudWeGo
Copyright 2022 CloudWeGo authors.

This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).
181 changes: 181 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# sessions (This is a community driven project)
English | [中文](README_CN.md)

This is a middleware for hertz.

Hertz middleware for session management with multi-backend support:


- [Cookie-based](#cookie-based)
- [Redis](#redis)

## Usage
### Start using it

Download and install it:

```bash
go get github.com/hertz-contrib/sessions
```

Import it in your code:

```go
import "github.com/hertz-contrib/sessions"
```

## Basic Examples

### Single session
```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/hertz-contrib/sessions"
"github.com/hertz-contrib/sessions/cookie"
)

func main() {
h := server.New(server.WithHostPorts(":8000"))
store := cookie.NewStore([]byte("secret"))
h.Use(sessions.Sessions("mysession", store))
h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
session := sessions.Default(c)

if session.Get("hello") != "world" {
session.Set("hello", "world")
session.Save()
}

c.JSON(200, utils.H{"hello": session.Get("hello")})
})
h.Spin()
}
```

### Multiple sessions
```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/hertz-contrib/sessions"
"github.com/hertz-contrib/sessions/cookie"
)

func main() {
h := server.New(server.WithHostPorts(":8000"))
store := cookie.NewStore([]byte("secret"))
sessionNames := []string{"a", "b"}
h.Use(sessions.SessionsMany(sessionNames, store))
h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
sessionA := sessions.DefaultMany(c, "a")
sessionB := sessions.DefaultMany(c, "b")

if sessionA.Get("hello") != "world!" {
sessionA.Set("hello", "world!")
sessionA.Save()
}

if sessionB.Get("hello") != "world?" {
sessionB.Set("hello", "world?")
sessionB.Save()
}

c.JSON(200, utils.H{
"a": sessionA.Get("hello"),
"b": sessionB.Get("hello"),
})
})
h.Spin()
}
```
## Backend Examples

### Cookie-based

```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/hertz-contrib/sessions"
"github.com/hertz-contrib/sessions/cookie"
)

func main() {
h := server.New(server.WithHostPorts(":8000"))
store := cookie.NewStore([]byte("secret"))
h.Use(sessions.Sessions("mysession", store))
h.GET("/incr", func(ctx context.Context, c *app.RequestContext) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, utils.H{"count": count})
})
h.Spin()
}
```

### Redis

```go
package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/hertz-contrib/sessions"
"github.com/hertz-contrib/sessions/redis"
)

func main() {
h := server.Default(server.WithHostPorts(":8000"))
store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
h.Use(sessions.Sessions("mysession", store))

h.GET("/incr", func(ctx context.Context, c *app.RequestContext) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, utils.H{"count": count})
})
h.Spin()
}
```

## License

This project is under Apache License. See the [LICENSE](LICENSE) file for the full license text.
Loading

0 comments on commit 915d8b6

Please sign in to comment.