代码拉取完成,页面将自动刷新
puffer 是一款快速、简洁的Go Web框架,使用极少的代码来开发高性能的Web应用程序,框架遵循Go的编程思量,零入侵。
框架专注于web服务处理,其他工具使用组件或者中间件进行集成
go get -u -v github.com/druidcaesa/puffer
require github.com/druidcaesa/puffer
go >=1.15
package main
import (
"github.com/druidcaesa/puffer"
"net/http"
)
func main() {
//快速创建启动服务
server := puffer.New()
//注册路由,第二个参数是一个函数
server.GET("/hello", func(c *puffer.Context) {
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
})
//监听端口
server.Run(":8080")
}
curl "http://127.0.0.1:8080/hello"
hello , you're at /hello
package main
import (
"github.com/druidcaesa/puffer"
"net/http"
)
func main() {
r := puffer.New()
r.GET("/", func(c *puffer.Context) {
c.HTML(http.StatusOK, "<h1>Hello Puffer</h1>")
})
r.GET("/hello", func(c *puffer.Context) {
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
})
r.Run(":8080")
}
package main
import (
"github.com/druidcaesa/puffer"
"net/http"
)
func main() {
//快速创建启动服务
server := puffer.New()
//一级路由
server.GET("/hello/:name", func(c *puffer.Context) {
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
})
//多级路由测试
server.GET("/assets/*filepath", func(c *puffer.Context) {
c.JSON(http.StatusOK, puffer.H{"filepath": c.Param("filepath")})
})
//监听端口
server.Run(":8080")
}
package main
import (
"github.com/druidcaesa/puffer"
)
func main() {
//快速创建启动服务
server := puffer.New()
//正常路由
server.GET("/login", login)
//分组路由
v1 := server.Group("/v1")
{
v1.GET("/getInfo", getInfo)
}
}
func getInfo(context *puffer.Context) {
}
func login(context *puffer.Context) {
}
2022/06/07 16:01:55 Route GET - /login
2022/06/07 16:01:55 Route GET - /v1/getInfo
package main
import (
"fmt"
"github.com/druidcaesa/puffer"
)
//用户获取get参数,必须使用tag form
type Query struct {
Name string `form:"name"`
}
func main() {
//快速创建启动服务
server := puffer.New()
//正常路由
server.GET("/login", login)
//分组路由
v1 := server.Group("/v1")
{
v1.GET("/getInfo", getInfo)
}
server.Run(":8080")
}
func getInfo(c *puffer.Context) {
q := new(Query)
_, err := c.BindQuery(q)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("获取前端传的参数%s", q.Name)
}
func login(context *puffer.Context) {
}
package main
import (
"fmt"
"github.com/druidcaesa/puffer"
"net/http"
)
//用户获取get参数,必须使用tag form
type Query struct {
Name string `form:"name"`
}
type JSON struct {
UserName string `json:"userName"`
}
func main() {
//快速创建启动服务
server := puffer.New()
//正常路由
server.POST("/login", login)
//分组路由
v1 := server.Group("/v1")
{
v1.GET("/getInfo", getInfo)
}
server.Run(":8080")
}
func getInfo(c *puffer.Context) {
q := new(Query)
_, err := c.BindQuery(q)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("获取前端传的参数%s", q.Name)
}
func login(c *puffer.Context) {
j := new(JSON)
_, err := c.BindJsonBody(j)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("获取前端传的参数%s", j.UserName)
c.JSON(http.StatusOK, puffer.H{
"data": j,
})
}
package main
import (
"github.com/druidcaesa/puffer"
"log"
"net/http"
)
func main() {
engine := puffer.Default()
engine.POST("/testForm", func(c *puffer.Context) {
//获取表单提交数据
userName := c.GetPostForm("userName")
log.Printf("表单提交数据为------->%s",userName)
c.JSON(http.StatusOK,puffer.H{
"message": userName,
})
})
engine.Run(":8080")
}
2022/06/08 09:31:09 表单提交数据为------->张三
2022/06/08 09:31:09 [200] /testForm in 100.905µs
package main
import (
"github.com/druidcaesa/puffer"
"log"
)
func main() {
engine := puffer.Default()
engine.POST("/upload", func(c *puffer.Context) {
file, err := c.FormFile("file")
if err != nil {
return
}
log.Printf("获取到的文件名称--------------->%s",file.Filename)
log.Printf("获取到的文件大小--------------->%d",file.Size)
log.Printf("获取到的文件类型--------------->%T",file.File)
})
engine.Run(":8080")
}
2022/06/08 09:34:01 Route POST - /upload
2022/06/08 09:34:29 获取到的文件名称--------------->monkey-admin.sql
2022/06/08 09:34:29 获取到的文件大小--------------->40011
2022/06/08 09:34:29 获取到的文件类型--------------->*multipart.File
/**
@author: fanyanan
@date: 2022/6/7
@note: //日志中间件
**/
package middlewares
import (
"github.com/druidcaesa/puffer"
"log"
"time"
)
func Logger() puffer.HandlerFunc {
return func(c *puffer.Context) {
//start time
t := time.Now()
//Middleware processing logic
log.Printf("[Status:%d] %s in %v", c.StatusCode, c.Req.RequestURI, time.Since(t))
//Pass to the next middleware
c.Next()
}
}
package main
import (
"fmt"
"github.com/druidcaesa/puffer"
"net/http"
"web-demo/middlewares"
)
//用户获取get参数,必须使用tag form
type Query struct {
Name string `form:"name"`
}
type JSON struct {
UserName string `json:"userName"`
}
func main() {
//快速创建启动服务
server := puffer.New()
//注册全局中间件
server.Use(middlewares.Logger())
//分组路由
v1 := server.Group("/v1")
{
v1.GET("/getInfo", getInfo)
}
server.Run(":8080")
}
func getInfo(c *puffer.Context) {
q := new(Query)
_, err := c.BindQuery(q)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("获取前端传的参数%s", q.Name)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。