mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
add example for https://github.com/kataras/iris/issues/1671
This commit is contained in:
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
|
||||
|
||||
Reference in New Issue
Block a user