Goweb开发之Iri框架实战,Goweb开发架构设计
一、基础学习
(一)网络请求方法
iris框架 创建的后端应用支持 Get、Post、Head、Options、Patch、Delete、Put方法
-
package irisLearn
-
-
import (
-
"fmt"
-
"github.com/kataras/iris/v12"
-
"github.com/kataras/iris/v12/context"
-
)
-
-
func Methods() {
-
app := iris.Default()
-
-
app.Get("/someGet", getting)
-
app.Post("/somePost", posting)
-
app.Head("/someHead", heading)
-
app.Put("/somePut", putting)
-
app.Delete("/someDelete", deleting)
-
app.Patch("/somePatch", patching)
-
app.Options("/someOptions", optioning)
-
-
app.Listen(":8080")
-
-
}
-
-
func optioning(c *context.Context) {
-
fmt.Println("optioning")
-
}
-
-
func patching(c *context.Context) {
-
fmt.Println("patching")
-
}
-
-
func deleting(c *context.Context) {
-
fmt.Println("deleting")
-
}
-
-
func putting(c *context.Context) {
-
fmt.Println("putting")
-
}
-
-
func heading(c *context.Context) {
-
fmt.Println("heading")
-
}
-
-
func posting(c *context.Context) {
-
fmt.Println("posting")
-
}
-
-
func getting(context *context.Context) {
-
fmt.Println("getting")
-
}
(二)路径中获取参数
在请求路径中可传入参数,但是在路径必须保证参数不为空,否则出现请求路径不一致的情况
使用方法
"/path/{parameter_name:parameter_type}"
实例代码
-
package irisLearn
-
-
import "github.com/kataras/iris/v12"
-
-
func ParametersInPath() {
-
app := iris.Default()
-
-
// This handler will match /user/john but will not match /user/ or /user
-
app.Get("/user/{name}", func(context iris.Context) {
-
name := context.Params().Get("name")
-
-
context.Writef("Hello %s", name)
-
-
})
-
-
app.Get("/user/{name:string}/{action:path}", func(context iris.Context) {
-
name := context.Params().Get("name")
-
action := context.Params().Get("action")
-
-
message := name + action
-
-
context.WriteString(message)
-
-
})
-
-
app.Post("/user/{name:string}/{action:path}", func(context iris.Context) {
-
b := context.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}"
-
context.Writef("%s", b)
-
})
-
-
app.Listen(":8080")
-
}
验证请求
-
-
$ curl -Method Get "http://localhost:8080/user/john/eating"
-
-
// result
-
content:johneating
(三)获取请求路径中的参数
获取方法
-
使用 Context.URLParamDefault("parameter_name","default_string")
-
使用 Context.URLParam("parameter_name")
实例代码
-
package irisLearn
-
-
import "github.com/kataras/iris/v12"
-
-
func QuerystringParameters() {
-
app := iris.Default()
-
-
app.Get("/welcome", func(context iris.Context) {
-
firstName := context.URLParamDefault("firstname", "john")
-
secondName := context.URLParam("secondname")
-
-
context.Writef("Hello %s %s", firstName, secondName)
-
})
-
-
app.Listen(":8080")
-
}
验证请求
-
$ curl "http://localhost:8080/welcome?firstname=lily&secondname=freak"
-
-
// result
-
content:"Hello lily freak"
(四)获取Form表单数据
获取方法
-
Context.PostValue("parameter_name")
-
Context.PostValueDefault("parameter_name","default_value")
实例代码
-
package irisLearn
-
-
import "github.com/kataras/iris/v12"
-
-
func MultipartUrlencodedForm() {
-
-
app := iris.Default()
-
-
app.Post("/form_post", func(context iris.Context) {
-
message := context.PostValue("message")
-
nick := context.PostValueDefault("nick", "anonymous")
-
-
context.JSON(iris.Map{
-
"status": "posted",
-
"message": message,
-
"nick": nick,
-
})
-
})
-
-
app.Listen(":8080")
-
}
验证请求
-
$ curl -Method Post -Body {message="this is a message"} http://localhost:8080/form_post
-
-
-
//result
-
Content : {"message":"\"this is message\"","nick":"anonymous
-
","status":"posted"}
(五)路由分组
获取方法
app.Party("path")
使用上述方法可以将path 路径作为一个分组,后续路径可以在这个分组中定义
实例代码
-
package irisLearn
-
-
import (
-
"github.com/kataras/iris/v12"
-
"github.com/kataras/iris/v12/context"
-
)
-
-
func GroupRoute() {
-
app := iris.Default()
-
-
// v1是一个分组
-
v1 := app.Party("/v1")
-
{
-
v1.Post("/login",loginEndPoint)
-
}
-
-
app.Listen(":8080")
-
-
}
-
-
func loginEndPoint(context *context.Context) {
-
fmt.Println("loginEndPoint")
-
}
-
-
(六)日志打印到文件
使用方法
app.Logger().SetOutput("file")
实例代码
-
package irisLearn
-
-
import (
-
"github.com/kataras/iris/v12"
-
"os"
-
)
-
-
func ApplicationFileLogger() {
-
-
app := iris.Default()
-
-
f, _ := os.Create("iris.log")
-
app.Logger().SetOutput(f)
-
-
app.Get("/ping", func(context iris.Context) {
-
context.WriteString("pong")
-
})
-
-
app.Listen(":8080")
-
}
