Hero Basic

    1. package main
    2. import (
    3. "github.com/kataras/iris"
    4. "github.com/kataras/iris/hero"
    5. )
    6. func main() {
    7. app := iris.New()
    8. // 1.直接把hello函数转化成iris请求处理函数
    9. helloHandler := hero.Handler(hello)
    10. app.Get("/{to:string}", helloHandler)
    11. // 2.把结构体实例注入hero,在把在结构体方法转化成iris请求处理函数
    12. hero.Register(&myTestService{
    13. prefix: "Service: Hello",
    14. })
    15. helloServiceHandler := hero.Handler(helloService)
    16. // 3.注册一个iris请求处理函数,是以from表单格式x-www-form-urlencoded数据类型,以LoginForm类型映射
    17. // 然后把login方法转化成iris请求处理函数
    18. hero.Register(func(ctx iris.Context) (form LoginForm) {
    19. //绑定from方式提交以x-www-form-urlencoded数据格式传输的from数据,并返回相应结构体
    20. return
    21. })
    22. loginHandler := hero.Handler(login)
    23. app.Post("/login", loginHandler)
    24. // http://localhost:8080/your_name
    25. // http://localhost:8080/service/your_name
    26. app.Run(iris.Addr(":8080"))
    27. }
    28. func hello(to string) string {
    29. return "Hello " + to
    30. }
    31. type Service interface {
    32. SayHello(to string) string
    33. type myTestService struct {
    34. prefix string
    35. }
    36. func (s *myTestService) SayHello(to string) string {
    37. return s.prefix + " " + to
    38. func helloService(to string, service Service) string {
    39. return service.SayHello(to)
    40. }
    41. type LoginForm struct {
    42. Username string `form:"username"`
    43. Password string `form:"password"`
    44. }
    45. func login(form LoginForm) string {
    46. return "Hello " + form.Username