Goweb开发之Iri框架实战,Goweb开发架构设计

发布时间 2023-11-11 12:11:16作者: 天使angl

Goweb开发之Iri框架实战,Goweb开发架构设计

一、基础学习

(一)网络请求方法

iris框架 创建的后端应用支持 Get、Post、Head、Options、Patch、Delete、Put方法

  1.  
    package irisLearn
  2.  
     
  3.  
    import (
  4.  
    "fmt"
  5.  
    "github.com/kataras/iris/v12"
  6.  
    "github.com/kataras/iris/v12/context"
  7.  
    )
  8.  
     
  9.  
    func Methods() {
  10.  
    app := iris.Default()
  11.  
     
  12.  
    app.Get("/someGet", getting)
  13.  
    app.Post("/somePost", posting)
  14.  
    app.Head("/someHead", heading)
  15.  
    app.Put("/somePut", putting)
  16.  
    app.Delete("/someDelete", deleting)
  17.  
    app.Patch("/somePatch", patching)
  18.  
    app.Options("/someOptions", optioning)
  19.  
     
  20.  
    app.Listen(":8080")
  21.  
     
  22.  
    }
  23.  
     
  24.  
    func optioning(c *context.Context) {
  25.  
    fmt.Println("optioning")
  26.  
    }
  27.  
     
  28.  
    func patching(c *context.Context) {
  29.  
    fmt.Println("patching")
  30.  
    }
  31.  
     
  32.  
    func deleting(c *context.Context) {
  33.  
    fmt.Println("deleting")
  34.  
    }
  35.  
     
  36.  
    func putting(c *context.Context) {
  37.  
    fmt.Println("putting")
  38.  
    }
  39.  
     
  40.  
    func heading(c *context.Context) {
  41.  
    fmt.Println("heading")
  42.  
    }
  43.  
     
  44.  
    func posting(c *context.Context) {
  45.  
    fmt.Println("posting")
  46.  
    }
  47.  
     
  48.  
    func getting(context *context.Context) {
  49.  
    fmt.Println("getting")
  50.  
    }

(二)路径中获取参数

在请求路径中可传入参数,但是在路径必须保证参数不为空,否则出现请求路径不一致的情况

使用方法

"/path/{parameter_name:parameter_type}"
 

实例代码

  1.  
    package irisLearn
  2.  
     
  3.  
    import "github.com/kataras/iris/v12"
  4.  
     
  5.  
    func ParametersInPath() {
  6.  
    app := iris.Default()
  7.  
     
  8.  
    // This handler will match /user/john but will not match /user/ or /user
  9.  
    app.Get("/user/{name}", func(context iris.Context) {
  10.  
    name := context.Params().Get("name")
  11.  
     
  12.  
    context.Writef("Hello %s", name)
  13.  
     
  14.  
    })
  15.  
     
  16.  
    app.Get("/user/{name:string}/{action:path}", func(context iris.Context) {
  17.  
    name := context.Params().Get("name")
  18.  
    action := context.Params().Get("action")
  19.  
     
  20.  
    message := name + action
  21.  
     
  22.  
    context.WriteString(message)
  23.  
     
  24.  
    })
  25.  
     
  26.  
    app.Post("/user/{name:string}/{action:path}", func(context iris.Context) {
  27.  
    b := context.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}"
  28.  
    context.Writef("%s", b)
  29.  
    })
  30.  
     
  31.  
    app.Listen(":8080")
  32.  
    }

验证请求

  1.  
     
  2.  
    $ curl -Method Get "http://localhost:8080/user/john/eating"
  3.  
     
  4.  
    // result
  5.  
    content:johneating

(三)获取请求路径中的参数

获取方法

  • 使用 Context.URLParamDefault("parameter_name","default_string")

  • 使用 Context.URLParam("parameter_name")

实例代码

  1.  
    package irisLearn
  2.  
     
  3.  
    import "github.com/kataras/iris/v12"
  4.  
     
  5.  
    func QuerystringParameters() {
  6.  
    app := iris.Default()
  7.  
     
  8.  
    app.Get("/welcome", func(context iris.Context) {
  9.  
    firstName := context.URLParamDefault("firstname", "john")
  10.  
    secondName := context.URLParam("secondname")
  11.  
     
  12.  
    context.Writef("Hello %s %s", firstName, secondName)
  13.  
    })
  14.  
     
  15.  
    app.Listen(":8080")
  16.  
    }

验证请求

  1.  
    $ curl "http://localhost:8080/welcome?firstname=lily&secondname=freak"
  2.  
     
  3.  
    // result
  4.  
    content:"Hello lily freak"

(四)获取Form表单数据

获取方法

  1.  
    Context.PostValue("parameter_name")
  2.  
    Context.PostValueDefault("parameter_name","default_value")

实例代码

  1.  
    package irisLearn
  2.  
     
  3.  
    import "github.com/kataras/iris/v12"
  4.  
     
  5.  
    func MultipartUrlencodedForm() {
  6.  
     
  7.  
    app := iris.Default()
  8.  
     
  9.  
    app.Post("/form_post", func(context iris.Context) {
  10.  
    message := context.PostValue("message")
  11.  
    nick := context.PostValueDefault("nick", "anonymous")
  12.  
     
  13.  
    context.JSON(iris.Map{
  14.  
    "status": "posted",
  15.  
    "message": message,
  16.  
    "nick": nick,
  17.  
    })
  18.  
    })
  19.  
     
  20.  
    app.Listen(":8080")
  21.  
    }

验证请求

  1.  
    $ curl -Method Post -Body {message="this is a message"} http://localhost:8080/form_post
  2.  
     
  3.  
     
  4.  
    //result
  5.  
    Content : {"message":"\"this is message\"","nick":"anonymous
  6.  
    ","status":"posted"}

(五)路由分组

获取方法

app.Party("path")

使用上述方法可以将path 路径作为一个分组,后续路径可以在这个分组中定义

实例代码

  1.  
    package irisLearn
  2.  
     
  3.  
    import (
  4.  
    "github.com/kataras/iris/v12"
  5.  
    "github.com/kataras/iris/v12/context"
  6.  
    )
  7.  
     
  8.  
    func GroupRoute() {
  9.  
    app := iris.Default()
  10.  
     
  11.  
    // v1是一个分组
  12.  
    v1 := app.Party("/v1")
  13.  
    {
  14.  
    v1.Post("/login",loginEndPoint)
  15.  
    }
  16.  
     
  17.  
    app.Listen(":8080")
  18.  
     
  19.  
    }
  20.  
     
  21.  
    func loginEndPoint(context *context.Context) {
  22.  
    fmt.Println("loginEndPoint")
  23.  
    }
  24.  
     
  25.  
     

(六)日志打印到文件

使用方法

app.Logger().SetOutput("file")

实例代码

  1.  
    package irisLearn
  2.  
     
  3.  
    import (
  4.  
    "github.com/kataras/iris/v12"
  5.  
    "os"
  6.  
    )
  7.  
     
  8.  
    func ApplicationFileLogger() {
  9.  
     
  10.  
    app := iris.Default()
  11.  
     
  12.  
    f, _ := os.Create("iris.log")
  13.  
    app.Logger().SetOutput(f)
  14.  
     
  15.  
    app.Get("/ping", func(context iris.Context) {
  16.  
    context.WriteString("pong")
  17.  
    })
  18.  
     
  19.  
    app.Listen(":8080")
  20.  
    }