-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for https://github.com/kataras/iris/issues/1671
- Loading branch information
Showing
7 changed files
with
101 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
_examples/request-body/form-query-headers-params-decoder/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters