Как обслуживать статические файлы в Gin
Следуя инструкции, я пытаюсь подключить интерфейс (React) к интерфейсу API API (Gin), но static.Serve не работает, ошибка выдается, как показано ниже:
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
источник:
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()
}
если что-то обновится в джине? Я попытался заменить статический пакет с другими, все тот же. Есть идеи по этому поводу?
1 ответ
Вы забыли загрузить путь к файлу, как
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")