2

by following the tutorial I try to connect the frontend (React) to backend API (Gin), but the static.Serve doesn't work, error prompted as below:

cannot use static.Serve("/", static.LocalFile("./views", true)) (type "github.com/gin-gonic/gin".HandlerFunc) as type "github.com/supebirdgz/amgmt/vendor/github.com/gin-gonic/gin".HandlerFunc in argument to router.Use

source:

import (
    "github.com/gin-gonic/gin"
    "github.com/gin-gonic/contrib/static"
)

func main()  {
    router := gin.Default()
    router.Use(static.Serve("/", static.LocalFile("./frontend", true)))
    router.Run()
}

Was something updated in Gin? I tried replacing static package with others, still the same.

Zeng Vincent
  • 83
  • 1
  • 5
  • 2
    Make sure you have vendored both `gin` and `contrib/static`. The error says that `Use` expects the handlerfunc type from `"github.com/supebirdgz/amgmt/vendor/github.com/gin-gonic/gin"` (vendored package) but you are passing one from `"github.com/gin-gonic/gin"` (non vendored package). This suggest that you've only vendored the `gin` package but not the `contrib/static` one. – mkopriva Jan 29 '19 at 06:17
  • 1
    @mkopriva thanks so much, I checked with "govendor list" and did find the non vendored package, it works after adding it. – Zeng Vincent Jan 29 '19 at 06:55

1 Answers1

1

you forget to load file path like

  r := gin.Default()
  r.LoadHTMLGlob("dist/*.html")    // load the built dist path
  r.LoadHTMLFiles("static/*/*") //  load the static path
  r.Static("/static", "./dist/static")  // use the loaded source
  r.StaticFile("/hello/", "dist/index.html")  // use the loaded source

  r.Run(":8080")
fwhez
  • 561
  • 4
  • 10