用于 Kubernetes 教学演示,如果疑问请联系 [email protected]
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"time"
)
func main() {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// 你的自定义格式
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
r.Use(gin.Recovery())
r.StaticFile("/", "html/index.html")
r.StaticFS("/html", http.Dir("html"))
r.StaticFS("/upload", http.Dir("upload"))
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.GET("/hostname", func(c *gin.Context) {
hostName, _ := os.Hostname()
c.JSON(200, gin.H{
"message": hostName,
})
})
r.MaxMultipartMemory = 64 << 20 // 64 MiB
r.StaticFile("/upload", "html/upload.html")
r.POST("/upload", func(c *gin.Context) {
// 获取上传文件,返回的是multipart.FileHeader对象,代表一个文件,里面包含了文件名之类的详细信息
// file是表单字段名字
file, _ := c.FormFile("file")
// 打印上传的文件名
log.Println(file.Filename)
// 将上传的文件,保存到./data/1111.jpg 文件中
c.SaveUploadedFile(file, fmt.Sprintf("./upload/%v", file.Filename))
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
r.Run(":80")
}
欢迎大家访问我的个人博客,https://cloudmessage.top/ 持续更新云计算相关内容

Content type
Image
Digest
sha256:6321a07cd…
Size
54.1 MB
Last updated
over 2 years ago
docker pull wangyanglinux/myapp:v1.0-nonroot