Go Fiber – I try to translate from Go net/http to Go Fiber, how to cenvert it?

huangapple go评论74阅读模式
英文:

Go Fiber - I try to translate from Go net/http to Go Fiber, how to cenvert it?

问题

我是新手学习golang,然后我学习并喜欢上了Go Fiber。我从Go Fiber中学到了很多,我觉得net/http的示例非常酷。然后我尝试将Go net/http的示例转换为Go Fiber。

以下是Go net/http的示例代码:

package main

import (
	"embed"
	"io/fs"
	"log"
	"net/http"
	"runtime/pprof"
)

//go:embed nextjs/dist
//go:embed nextjs/dist/_next
//go:embed nextjs/dist/_next/static/chunks/pages/*.js
//go:embed nextjs/dist/_next/static/*/*.js
var nextFS embed.FS

func main() {
	// 根目录为Next.js应用生成的`dist`文件夹。
	distFS, err := fs.Sub(nextFS, "nextjs/dist")
	if err != nil {
		log.Fatal(err)
	}

	// 静态的Next.js应用将在`/`下提供服务。
	http.Handle("/", http.FileServer(http.FS(distFS)))
	// API将在`/api`下提供服务。
	http.HandleFunc("/api", handleAPI)

	// 在:8080端口启动HTTP服务器。
	log.Println("Starting HTTP server at http://localhost:8080 ...")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func handleAPI(w http.ResponseWriter, _ *http.Request) {
	// 收集内存分配的性能分析。
	profile := pprof.Lookup("allocs")

	// 将分析结果以人类可读的方式写入HTTP响应。
	err := profile.WriteTo(w, 1)
	if err != nil {
		log.Printf("Error: Failed to write allocs profile: %v", err)
	}
}

我将其改为以下代码:

package main

import (
	"embed"
	"log"
	"runtime/pprof"

	"github.com/gofiber/fiber/v2"
)

//go:embed nextjs/dist
//go:embed nextjs/dist/_next
//go:embed nextjs/dist/_next/static/chunks/pages/*.js
//go:embed nextjs/dist/_next/static/*/*.js
var nextFS embed.FS

func main() {
	// 根目录为Next.js应用生成的`dist`文件夹。
	app := fiber.New()

	// 静态的Next.js应用将在`/`下提供服务。
	//http.Handle("/", http.FileServer(http.FS(distFS)))
	app.Static("/", "./nextjs/dist")
	// API将在`/api`下提供服务。

	app.Get("/api", func(c *fiber.Ctx) error {
		profile := pprof.Lookup("allocs")
		return c.SendString(profile.String())
	})
	// 在:8080端口启动HTTP服务器。
	log.Println("Starting HTTP server at http://localhost:8080 ...")
	log.Fatal(app.Listen(":8080"))
}

但是我遇到了以下错误:

# command-line-arguments
./main.go:29:17: cannot use profile (type *pprof.Profile) as type string in argument to c.Get
./main.go:29:17: cannot use c.Get(profile) (type string) as type error in return argument:
	string does not implement error (missing Error method)

请帮忙验证一下,我如何成功使用pprof.Lookup("allocs")?提前感谢!

英文:

I am new to golang then I learn and I love Go fiber.
I learn from Go fiber and I see that net/http example is so cool.
then I try to convert from Go net/http example to Go fiber.

The below is go net/http

package main

import (
	"embed"
	"io/fs"
	"log"
	"net/http"
	"runtime/pprof"
)

//go:embed nextjs/dist
//go:embed nextjs/dist/_next
//go:embed nextjs/dist/_next/static/chunks/pages/*.js
//go:embed nextjs/dist/_next/static/*/*.js
var nextFS embed.FS

func main() {
	// Root at the `dist` folder generated by the Next.js app.
	distFS, err := fs.Sub(nextFS, "nextjs/dist")
	if err != nil {
		log.Fatal(err)
	}

	// The static Next.js app will be served under `/`.
	http.Handle("/", http.FileServer(http.FS(distFS)))
	// The API will be served under `/api`.
	http.HandleFunc("/api", handleAPI)

	// Start HTTP server at :8080.
	log.Println("Starting HTTP server at http://localhost:8080 ...")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func handleAPI(w http.ResponseWriter, _ *http.Request) {
	// Gather memory allocations profile.
	profile := pprof.Lookup("allocs")

	// Write profile (human readable, via debug: 1) to HTTP response.
	err := profile.WriteTo(w, 1)
	if err != nil {
		log.Printf("Error: Failed to write allocs profile: %v", err)
	}
}

I change it to

package main

import (
	"embed"
	"log"
 // "io/fs"
	"runtime/pprof"

  "github.com/gofiber/fiber/v2"
)

//go:embed nextjs/dist
//go:embed nextjs/dist/_next
//go:embed nextjs/dist/_next/static/chunks/pages/*.js
//go:embed nextjs/dist/_next/static/*/*.js
var nextFS embed.FS

func main() {
	// Root at the `dist` folder generated by the Next.js app.
  app := fiber.New()

	// The static Next.js app will be served under `/`.
	//http.Handle("/", http.FileServer(http.FS(distFS)))
  app.Static("/","./nextjs/dist")
	// The API will be served under `/api`.

  app.Get("/api", func(c *fiber.Ctx) error {
    profile := pprof.Lookup("allocs")
    return c.Get(profile)
  })
	// Start HTTP server at :8080.
	log.Println("Starting HTTP server at http://localhost:8080 ...")
	log.Fatal(app.Listen(":8080"))
}

but I got error as follow:

# command-line-arguments
./main.go:29:17: cannot use profile (type *pprof.Profile) as type string in argument to c.Get
./main.go:29:17: cannot use c.Get(profile) (type string) as type error in return argument:
	string does not implement error (missing Error method)

Please help to verify and how can I get the pprof.Lookup("allocs") successfully?
Thanks in advance

答案1

得分: 2

我认为你可以将这段代码替换为:

  app.Get("/api", func(c *fiber.Ctx) error {
    profile := pprof.Lookup("allocs")
    return profile.WriteTo(c, 1)
  })

*fiber.Ctx 实现了 io.Writer 接口,所以你应该可以像在这个实例中使用 http.ResponseWriter 一样使用它。

英文:

I think you can just replace this:

  app.Get("/api", func(c *fiber.Ctx) error {
    profile := pprof.Lookup("allocs")
    return c.Get(profile)
  })

With this:

  app.Get("/api", func(c *fiber.Ctx) error {
    profile := pprof.Lookup("allocs")
    return profile.WriteTo(c, 1)
  })

*fiber.Ctx implements io.Writer, so you should be able to use it like http.ResponseWriter in this instance.

huangapple
  • 本文由 发表于 2022年1月2日 22:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/70556913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定