• fiber极简极快新web框架


     

    Fiber:高效、极速、极简的Web框架

      Go 语言构建的 Web 框架一大堆,Beego、Iris、Gin 等 。今天带来一个足够有吸引力的新的Web框架。不同于其他的框架,他的性能极佳,代码量又足够小,上手和使用都能使你有驾轻就熟的感觉。Fiber是一个基于Express的Web框架,建立在Go语言写的最快的FasthttpHTTP引擎的基础上。皆在简化 零内存分配和提高性能,以便快速开发。

    快速上手

      1、前期准备

    首先, 下载并安装Go。 1.11或更高。使用go get命令完成安装:
    配置环境变量
    export GO111MODULE=on
    export GOPROXY=https://goproxy.cn
    
    下载依赖
    go get -u github.com/gofiber/fiber
    
    
    提示:如果使用Goland,创建go.mod文件,使用go mod tidy命令自动拉取依赖
    

      2、创建main.go

    package main
    
    import "github.com/gofiber/fiber"
    
    func main() {
    	app := fiber.New() // 创建 fiber app引擎
    
    	app.Get("/", func(c *fiber.Ctx) { // 创建一个 get 方法的路由返回 Hello World!
    		c.Send("Hello, World!")
    	})
    
    	app.Listen(3000) // 开启服务,并在 3000 端口监听。
    }
    

      

    路由

    func main() {
      app := fiber.New()
    
      // GET /john
      app.Get("/:name", func(c *fiber.Ctx) {
        fmt.Printf("Hello %s!", c.Params("name"))
        // => Hello john!
      })
    
      // GET /john
      app.Get("/:name/:age?", func(c *fiber.Ctx) {
        fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
        // => Name: john, Age:
      })
    
      // GET /api/register
      app.Get("/api/*", func(c *fiber.Ctx) {
        fmt.Printf("/api/%s", c.Params("*"))
        // => /api/register
      })
    
      app.Listen(3000)
    }
    

      

    静态文件

    func main() {
      app := fiber.New()
    
      app.Static("/", "/public")
      // => http://localhost:3000/js/script.js
      // => http://localhost:3000/css/style.css
    
      app.Static("/prefix", "/public")
      // => http://localhost:3000/prefix/js/script.js
      // => http://localhost:3000/prefix/css/style.css
    
      app.Static("*", "/public/index.html")
      // => http://localhost:3000/any/path/shows/index/html
    
      app.Listen(3000)
    }
    

      

    中间件

    func main() {
      app := fiber.New()
    
      // Match any route
      app.Use(func(c *fiber.Ctx) {
        fmt.Println("First middleware")
        c.Next()
      })
    
      // Match all routes starting with /api
      app.Use("/api", func(c *fiber.Ctx) {
        fmt.Println("Second middleware")
        c.Next()
      })
    
      // GET /api/register
      app.Get("/api/list", func(c *fiber.Ctx) {
        fmt.Println("Last middleware")
        c.Send("Hello, World!")
      })
    
      app.Listen(3000)
    }
    

      

    模版引擎

    import (
      "github.com/gofiber/fiber"
      "github.com/gofiber/template"
    )
    
    func main() {
      // You can setup template engine before initiation app:
      app := fiber.New(&fiber.Settings{
        TemplateEngine:    template.Mustache(),
        TemplateFolder:    "./views",
        TemplateExtension: ".tmpl",
      })
    
      // OR after initiation app at any convenient location:
      app.Settings.TemplateEngine = template.Mustache()
      app.Settings.TemplateFolder = "./views"
      app.Settings.TemplateExtension = ".tmpl"
    
      // And now, you can call template `./views/home.tmpl` like this:
      app.Get("/", func(c *fiber.Ctx) {
        c.Render("home", fiber.Map{
          "title": "Homepage",
          "year":  1999,
        })
      })
    
      // ...
    }
    

      

    链式路由

    func main() {
      app := fiber.New()
    
      // Root API route
      api := app.Group("/api", cors())  // /api
    
      // API v1 routes
      v1 := api.Group("/v1", mysql())   // /api/v1
      v1.Get("/list", handler)          // /api/v1/list
      v1.Get("/user", handler)          // /api/v1/user
    
      // API v2 routes
      v2 := api.Group("/v2", mongodb()) // /api/v2
      v2.Get("/list", handler)          // /api/v2/list
      v2.Get("/user", handler)          // /api/v2/user
    
      // ...
    }
    

      

    websocket

    import (
        "github.com/gofiber/fiber"
        "github.com/gofiber/websocket"
    )
    
    func main() {
      app := fiber.New()
    
      app.Get("/ws", websocket.New(func(c *websocket.Conn) {
        for {
          mt, msg, err := c.ReadMessage()
          if err != nil {
            log.Println("read:", err)
            break
          }
          log.Printf("recv: %s", msg)
          err = c.WriteMessage(mt, msg)
          if err != nil {
            log.Println("write:", err)
            break
          }
        }
      }))
    
      app.Listen(3000)
      // ws://localhost:3000/ws
    }
    

      

    ORM: https://github.com/gofiber/recipes/tree/master/gorm

    简单案例:https://github.com/gofiber/recipes

    官网教程:https://docs.gofiber.io/routing

  • 相关阅读:
    自考新教材--p78
    自考新教材--p76_2
    自考新教材--p76_1
    自考新教材--p75
    自考新教材--p70
    自考新教材--p69
    以HTML5及其衍生技术为基础的B/S架构实时视频监控解决方案
    智能化安防视频监控行业发展是如何转变的?
    摄像头视频监控是如何与流媒体服务器云平台连接的?
    如何实现摄像头监控数据实时存储及传输?
  • 原文地址:https://www.cnblogs.com/double-W/p/13291749.html
Copyright © 2020-2023  润新知