1. 前言
通过上册路由可知,项目入口在main.go里调用路由组实现,后续代码直接在routers目录里看,此处为基础部分,业务实现逻辑在routers/base.go
2. 获取API参数
package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
// 基础
func BaseRouter(e *gin.Engine) {
// 使用路由组
r := e.Group("/base")
r.GET("/view/:id/*action", api)
}
// 获取API参数
func api(c *gin.Context) {
id := c.Param("id")
action := c.Param("action")
// 截取/
action = strings.Trim(action, "/")
c.String(http.StatusOK, id+" is "+action)
}
http://127.0.0.1:8080/base/view/123/like
3. 获取url参数
package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
// 基础
func BaseRouter(e *gin.Engine) {
// 使用路由组
r := e.Group("/base")
r.GET("/view", url)
}
// 获取URL参数
func url(c *gin.Context) {
name := c.DefaultQuery("name", "yzx-fjl.cn")
c.String(http.StatusOK, fmt.Sprintf("hello %s", name))
}
http://127.0.0.1:8080/base/view?name=world
4. 获取表单参数
package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
// 基础
func BaseRouter(e *gin.Engine) {
// 使用路由组
r := e.Group("/base")
r.POST("/form", form)
}
// 获取表单参数
func form(c *gin.Context) {
types := c.DefaultPostForm("type", "post")
username := c.PostForm("username")
password := c.PostForm("userpassword")
c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
}
用浏览器打开my-gin/html/base/form.html,可看到表单页面效果
5. 文件
- 获取文件 c.FormFile("file")
- 保存文件 c.SaveUploadedFile(file, path)
5.1. 创建文件夹
package main
import (
"os"
)
func main() {
// 创建文件
createFile("./logs")
createFile("./upload")
}
// 创建文件夹
func createFile(path string) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
os.Mkdir(path, 0777)
}
}
在my-gin/main.go文件中我提前创建了upload、logs文件夹,这里是为了方便讲解特意写在此处,后续自己项目根据实情放置
5.2. 上传单个文件
package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
// 基础
func BaseRouter(e *gin.Engine) {
// 使用路由组
r := e.Group("/base")
// 上传单个文件 打开html/base/upload.html
r.POST("/upload", upload)
}
// 上传单个文件
func upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.String(500, "上传图片出错")
return
}
// 文件大小不能超过5M
if file.Size > 5000000 {
c.String(500, "上传图片超过5M")
return
}
// 上传路径
path := "./upload/" + file.Filename
c.SaveUploadedFile(file, path)
c.String(http.StatusOK, fmt.Sprintf("Filename:%s,Size:%d字节", file.Filename, file.Size))
}
用浏览器打开打开my-gin/html/base/upload.html页面,点击文件上传,可看到my-gin/upload目录下有你提交的文件
5.3. 上传多个文件
package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
// 基础
func BaseRouter(e *gin.Engine) {
// 使用路由组
r := e.Group("/base")
// 上传单个文件 打开html/base/upload.html
r.POST("/upload", upload)
}
// 上传单个文件
func upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.String(500, "上传图片出错")
return
}
// 文件大小不能超过5M
if file.Size > 5000000 {
c.String(500, "上传图片超过5M")
return
}
// 上传路径
path := "./upload/" + file.Filename
c.SaveUploadedFile(file, path)
c.String(http.StatusOK, fmt.Sprintf("Filename:%s,Size:%d字节", file.Filename, file.Size))
}
用浏览器打开打开my-gin/html/base/uploads.html页面
6. 写日志
// 写日志
func witerLog() {
gin.DisableConsoleColor()
// Logging to a file.
// 创建日志路径
f, _ := os.Create("./logs/" + time.Now().Format("2006-01-02") + ".log")
//gin.DefaultWriter = io.MultiWriter(f)
// 如果需要同时将日志写入文件和控制台,请使用以下代码。
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}
在main.go中引用了写日志,可在logs目录下看到有日志文件