From c81b97188ae01dee3810c86dcc4f1d928ad41eb6 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 13 Nov 2020 00:34:17 +0200 Subject: [PATCH] add example for https://github.com/kataras/iris/issues/1671 --- _examples/README.md | 1 + _examples/auth/jwt/middleware/main.go | 2 +- _examples/auth/jwt/tutorial/api/router.go | 23 +++++++ _examples/auth/jwt/tutorial/main.go | 19 ++---- .../form-query-headers-params-decoder/main.go | 60 +++++++++++++++++++ aliases.go | 11 ++++ core/router/mime.go | 1 + 7 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 _examples/auth/jwt/tutorial/api/router.go create mode 100644 _examples/request-body/form-query-headers-params-decoder/main.go diff --git a/_examples/README.md b/_examples/README.md index 7764b9b1..ec3eaea7 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -171,6 +171,7 @@ * [Bind URL](request-body/read-url/main.go) * [Bind Headers](request-body/read-headers/main.go) * [Bind Body](request-body/read-body/main.go) + * [Add Converter](request-body/form-query-headers-params-decoder/main.go) * [Bind Custom per type](request-body/read-custom-per-type/main.go) * [Bind Custom via Unmarshaler](request-body/read-custom-via-unmarshaler/main.go) * [Bind Many times](request-body/read-many/main.go) diff --git a/_examples/auth/jwt/middleware/main.go b/_examples/auth/jwt/middleware/main.go index 6af83e64..a33a17e1 100644 --- a/_examples/auth/jwt/middleware/main.go +++ b/_examples/auth/jwt/middleware/main.go @@ -9,7 +9,7 @@ import ( var ( sigKey = []byte("signature_hmac_secret_shared_key") - encKey = []byte("GCM_AES_256_secret_shared_key_32") + // encKey = []byte("GCM_AES_256_secret_shared_key_32") ) type fooClaims struct { diff --git a/_examples/auth/jwt/tutorial/api/router.go b/_examples/auth/jwt/tutorial/api/router.go new file mode 100644 index 00000000..506260bc --- /dev/null +++ b/_examples/auth/jwt/tutorial/api/router.go @@ -0,0 +1,23 @@ +package api + +import ( + "myapp/domain/repository" + + "github.com/kataras/iris/v12" +) + +// NewRouter accepts some dependencies +// and returns a function which returns the routes on the given Iris Party (group of routes). +func NewRouter(userRepo repository.UserRepository, todoRepo repository.TodoRepository) func(iris.Party) { + return func(router iris.Party) { + router.Post("/signin", SignIn(userRepo)) + + router.Use(Verify()) // protect the next routes with JWT. + + router.Post("/todos", CreateTodo(todoRepo)) + router.Get("/todos", ListTodos(todoRepo)) + router.Get("/todos/{id}", GetTodo(todoRepo)) + + router.Get("/admin/todos", AllowAdmin, ListAllTodos(todoRepo)) + } +} diff --git a/_examples/auth/jwt/tutorial/main.go b/_examples/auth/jwt/tutorial/main.go index 8be43081..3246e20f 100644 --- a/_examples/auth/jwt/tutorial/main.go +++ b/_examples/auth/jwt/tutorial/main.go @@ -8,28 +8,17 @@ import ( ) var ( - userRepository = repository.NewMemoryUserRepository() - todoRepository = repository.NewMemoryTodoRepository() + userRepo = repository.NewMemoryUserRepository() + todoRepo = repository.NewMemoryTodoRepository() ) func main() { - if err := repository.GenerateSamples(userRepository, todoRepository); err != nil { + if err := repository.GenerateSamples(userRepo, todoRepo); err != nil { panic(err) } app := iris.New() - - app.Post("/signin", api.SignIn(userRepository)) - - verify := api.Verify() - - todosAPI := app.Party("/todos", verify) - todosAPI.Post("/", api.CreateTodo(todoRepository)) - todosAPI.Get("/", api.ListTodos(todoRepository)) - todosAPI.Get("/{id}", api.GetTodo(todoRepository)) - - adminAPI := app.Party("/admin", verify, api.AllowAdmin) - adminAPI.Get("/todos", api.ListAllTodos(todoRepository)) + app.PartyFunc("/", api.NewRouter(userRepo, todoRepo)) // POST http://localhost:8080/signin (Form: username, password) // GET http://localhost:8080/todos diff --git a/_examples/request-body/form-query-headers-params-decoder/main.go b/_examples/request-body/form-query-headers-params-decoder/main.go new file mode 100644 index 00000000..0e764472 --- /dev/null +++ b/_examples/request-body/form-query-headers-params-decoder/main.go @@ -0,0 +1,60 @@ +// package main contains an example on how to register a custom decoder +// for a custom type when using the `ReadQuery/ReadParams/ReadHeaders/ReadForm` methods. +// +// Let's take for example the mongo-driver/primite.ObjectID: +// +// ObjectID is type ObjectID [12]byte. +// You have to register a converter for that custom type. +// ReadJSON works because the ObjectID has a MarshalJSON method +// which the encoding/json pakcage uses as a converter. +// See here: https://godoc.org/go.mongodb.org/mongo-driver/bson/primitive#ObjectID.MarshalJSON. +// +// To register a converter import the github.com/iris-contrib/schema and call +// schema.Query.RegisterConverter(value interface{}, converterFunc Converter). +// +// The Converter is just a type of func(string) reflect.Value. +// +// There is another way, but requires introducing a custom type which will wrap the mongo's ObjectID +// and implements the encoding.TextUnmarshaler e.g. +// func(id *ID) UnmarshalText(text []byte) error){ ...}. +package main + +import ( + "reflect" + + "go.mongodb.org/mongo-driver/bson/primitive" + + "github.com/iris-contrib/schema" + "github.com/kataras/iris/v12" +) + +type MyType struct { + ID primitive.ObjectID `url:"id"` +} + +func main() { + // Register on initialization. + schema.Query.RegisterConverter(primitive.ObjectID{}, func(value string) reflect.Value { + id, err := primitive.ObjectIDFromHex(value) + if err != nil { + return reflect.Value{} + } + return reflect.ValueOf(id) + }) + + app := iris.New() + + app.Get("/", func(ctx iris.Context) { + var t MyType + err := ctx.ReadQuery(&t) + if err != nil && !iris.IsErrPath(err) { + ctx.StopWithError(iris.StatusInternalServerError, err) + return + } + + ctx.Writef("MyType.ID: %q", t.ID.Hex()) + }) + + // http://localhost:8080?id=507f1f77bcf86cd799439011 + app.Listen(":8080") +} diff --git a/aliases.go b/aliases.go index b21ace34..521bd33e 100644 --- a/aliases.go +++ b/aliases.go @@ -14,6 +14,14 @@ import ( "github.com/kataras/iris/v12/view" ) +// SameSite attributes. +const ( + SameSiteDefaultMode = http.SameSiteDefaultMode + SameSiteLaxMode = http.SameSiteLaxMode + SameSiteStrictMode = http.SameSiteStrictMode + SameSiteNoneMode = http.SameSiteNoneMode +) + type ( // Context is the middle-man server's "object" for the clients. // @@ -160,6 +168,9 @@ type ( // // An alias for the `context.CookieOption`. CookieOption = context.CookieOption + // Cookie is a type alias for the standard net/http Cookie struct type. + // See `Context.SetCookie`. + Cookie = http.Cookie // N is a struct which can be passed on the `Context.Negotiate` method. // It contains fields which should be filled based on the `Context.Negotiation()` // server side values. If no matched mime then its "Other" field will be sent, diff --git a/core/router/mime.go b/core/router/mime.go index 1fb50b36..05e6db4c 100644 --- a/core/router/mime.go +++ b/core/router/mime.go @@ -180,6 +180,7 @@ var types = map[string]string{ ".js": context.ContentJavascriptHeaderValue, ".mjs": context.ContentJavascriptHeaderValue, ".json": context.ContentJSONHeaderValue, + ".vue": context.ContentJavascriptHeaderValue, ".jut": "image/jutvision", ".kar": "audio/midi", ".karbon": "application/vnd.kde.karbon",