This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-11-13 00:34:17 +02:00
parent 7b6a8f1e26
commit c81b97188a
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
7 changed files with 101 additions and 16 deletions

View File

@ -171,6 +171,7 @@
* [Bind URL](request-body/read-url/main.go) * [Bind URL](request-body/read-url/main.go)
* [Bind Headers](request-body/read-headers/main.go) * [Bind Headers](request-body/read-headers/main.go)
* [Bind Body](request-body/read-body/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 per type](request-body/read-custom-per-type/main.go)
* [Bind Custom via Unmarshaler](request-body/read-custom-via-unmarshaler/main.go) * [Bind Custom via Unmarshaler](request-body/read-custom-via-unmarshaler/main.go)
* [Bind Many times](request-body/read-many/main.go) * [Bind Many times](request-body/read-many/main.go)

View File

@ -9,7 +9,7 @@ import (
var ( var (
sigKey = []byte("signature_hmac_secret_shared_key") 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 { type fooClaims struct {

View 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))
}
}

View File

@ -8,28 +8,17 @@ import (
) )
var ( var (
userRepository = repository.NewMemoryUserRepository() userRepo = repository.NewMemoryUserRepository()
todoRepository = repository.NewMemoryTodoRepository() todoRepo = repository.NewMemoryTodoRepository()
) )
func main() { func main() {
if err := repository.GenerateSamples(userRepository, todoRepository); err != nil { if err := repository.GenerateSamples(userRepo, todoRepo); err != nil {
panic(err) panic(err)
} }
app := iris.New() app := iris.New()
app.PartyFunc("/", api.NewRouter(userRepo, todoRepo))
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))
// POST http://localhost:8080/signin (Form: username, password) // POST http://localhost:8080/signin (Form: username, password)
// GET http://localhost:8080/todos // GET http://localhost:8080/todos

View File

@ -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")
}

View File

@ -14,6 +14,14 @@ import (
"github.com/kataras/iris/v12/view" "github.com/kataras/iris/v12/view"
) )
// SameSite attributes.
const (
SameSiteDefaultMode = http.SameSiteDefaultMode
SameSiteLaxMode = http.SameSiteLaxMode
SameSiteStrictMode = http.SameSiteStrictMode
SameSiteNoneMode = http.SameSiteNoneMode
)
type ( type (
// Context is the middle-man server's "object" for the clients. // Context is the middle-man server's "object" for the clients.
// //
@ -160,6 +168,9 @@ type (
// //
// An alias for the `context.CookieOption`. // An alias for the `context.CookieOption`.
CookieOption = 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. // 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()` // 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, // server side values. If no matched mime then its "Other" field will be sent,

View File

@ -180,6 +180,7 @@ var types = map[string]string{
".js": context.ContentJavascriptHeaderValue, ".js": context.ContentJavascriptHeaderValue,
".mjs": context.ContentJavascriptHeaderValue, ".mjs": context.ContentJavascriptHeaderValue,
".json": context.ContentJSONHeaderValue, ".json": context.ContentJSONHeaderValue,
".vue": context.ContentJavascriptHeaderValue,
".jut": "image/jutvision", ".jut": "image/jutvision",
".kar": "audio/midi", ".kar": "audio/midi",
".karbon": "application/vnd.kde.karbon", ".karbon": "application/vnd.kde.karbon",