mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
add example for https://github.com/kataras/iris/issues/1671
This commit is contained in:
parent
7b6a8f1e26
commit
c81b97188a
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
23
_examples/auth/jwt/tutorial/api/router.go
Normal file
23
_examples/auth/jwt/tutorial/api/router.go
Normal file
|
@ -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))
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
}
|
11
aliases.go
11
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,
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue
Block a user