From bfa64705b52dfb47ca02710542ba81af0f94523a Mon Sep 17 00:00:00 2001 From: John Date: Sun, 22 Mar 2020 00:15:59 +0800 Subject: [PATCH] update comment for package gsession --- .example/other/test.go | 74 +++++++++++++-------------------- os/gsession/gsession_storage.go | 16 +++---- 2 files changed, 36 insertions(+), 54 deletions(-) diff --git a/.example/other/test.go b/.example/other/test.go index 786b8c761..3218d008c 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,54 +1,36 @@ package main import ( - "fmt" - "github.com/gogf/gf/encoding/gjson" - "github.com/gogf/gf/text/gstr" + "net/http" + + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" ) -const ( - TraceIdName = "trace-id" -) +func MiddlewareAuth(r *ghttp.Request) { + token := r.Get("token") + if token == "123456" { + r.Response.Writeln("auth") + r.Middleware.Next() + } else { + r.Response.WriteStatus(http.StatusForbidden) + } +} + +func MiddlewareCORS(r *ghttp.Request) { + r.Response.Writeln("cors") + r.Response.CORSDefault() + r.Middleware.Next() +} func main() { - - type Sender struct { - Name string `json:"name"` - Email string `json:"email"` - } - type To struct { - Name string `json:"name"` - Email string `json:"email"` - } - - type SendReq struct { - Sender *Sender `json:"sender"` - //Name string `json:"name"` - HtmlContent string `json:"htmlContent"` - Subject string `json:"subject"` - To []*To `json:"to"` - } - - //url := "emailCampaigns" - sendreq := SendReq{ - Sender: &Sender{ - Name: "123", - Email: "globalclienthelp@gmail.com", - }, - To: []*To{{ - Name: "456", - //Email: order.Email, - Email: "jinmao88@hotmail.com", - }}, - } - subject := "" - htmlcontent := "" - - subject = " Your order:" + gstr.Split("11111", "-")[1] + " , Shipment is on its way." - htmlcontent = "test" - sendreq.Subject = subject - sendreq.HtmlContent = htmlcontent - j, err := gjson.DecodeToJson(sendreq) - fmt.Println(err) - fmt.Println(j) + s := g.Server() + s.Group("/api.v2", func(group *ghttp.RouterGroup) { + group.Middleware(MiddlewareCORS, MiddlewareAuth) + group.ALL("/user/list", func(r *ghttp.Request) { + r.Response.Writeln("list") + }) + }) + s.SetPort(8199) + s.Run() } diff --git a/os/gsession/gsession_storage.go b/os/gsession/gsession_storage.go index 23b2783c1..48565bc11 100644 --- a/os/gsession/gsession_storage.go +++ b/os/gsession/gsession_storage.go @@ -16,22 +16,22 @@ type Storage interface { // This function can be used for custom session creation. New(ttl time.Duration) (id string) - // Get retrieves session value with given key. + // Get retrieves and returns session value with given key. // It returns nil if the key does not exist in the session. Get(id string, key string) interface{} // GetMap retrieves all key-value pairs as map from storage. GetMap(id string) map[string]interface{} - // GetSize retrieves the size of key-value pairs from storage. + // GetSize retrieves and returns the size of key-value pairs from storage. GetSize(id string) int - // Set sets key-value session pair to the storage. - // The parameter specifies the TTL for the session id (not for the key-value pair). + // Set sets one key-value session pair to the storage. + // The parameter specifies the TTL for the session id. Set(id string, key string, value interface{}, ttl time.Duration) error - // SetMap batch sets key-value session pairs with map to the storage. - // The parameter specifies the TTL for the session id(not for the key-value pair). + // SetMap batch sets key-value session pairs as map to the storage. + // The parameter specifies the TTL for the session id. SetMap(id string, data map[string]interface{}, ttl time.Duration) error // Remove deletes key with its value from storage. @@ -42,11 +42,11 @@ type Storage interface { // GetSession returns the session data as *gmap.StrAnyMap for given session id from storage. // - // The parameter specifies the TTL for this session, and it returns nil if the TTL is exceeded. + // The parameter specifies the TTL for this session. // The parameter is the current old session data stored in memory, // and for some storage it might be nil if memory storage is disabled. // - // This function is called ever when session starts. + // This function is called ever when session starts. It returns nil if the TTL is exceeded. GetSession(id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error) // SetSession updates the data for specified session id.