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

Time:01-03

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

CodePudding user response:

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.

  • Related