Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Nov 12, 2020
1 parent 7b6a8f1 commit c81b971
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 16 deletions.
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion _examples/auth/jwt/middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions _examples/auth/jwt/tutorial/api/router.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
19 changes: 4 additions & 15 deletions _examples/auth/jwt/tutorial/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions _examples/request-body/form-query-headers-params-decoder/main.go
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/router/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c81b971

Please sign in to comment.