首页 > 后端开发 > Golang > 正文

使用自定义路由器使用 wails 资产处理程序时出现问题

WBOY
发布: 2024-02-12 16:30:07
转载
933 人浏览过

使用自定义路由器使用 wails 资产处理程序时出现问题

问题内容

我正在尝试将自定义多路复用器与wails 资产处理程序一起使用,但是当尝试获取任何内容时,我不断获取index.html 页面。我在多路复用器的 servehttp 函数顶部添加了一条 print 语句,但这仅在程序开始时获取 favicon.ico 时被调用一次。

我有以下主文件:

package main

import (
    "embed"
    "fmt"

    "github.com/nigel2392/router/v3"
    "github.com/nigel2392/router/v3/request"
    "github.com/wailsapp/wails/v2"
    "github.com/wailsapp/wails/v2/pkg/options"
    "github.com/wailsapp/wails/v2/pkg/options/assetserver"
)

var router *router.router = router.newrouter(true)

func init() {
    router.get("/about", func(r *request.request) {
        fmt.println("about")
        r.writestring("about")
    })
}

//go:embed all:frontend/dist
var assets embed.fs

func main() {
    // create an instance of the app structure
    app := newapp()

    // create application with options
    err := wails.run(&options.app{
        title:  "new",
        width:  1024,
        height: 768,
        assetserver: &assetserver.options{
            assets:  assets,
            handler: router,
        },
        backgroundcolour: &options.rgba{r: 27, g: 38, b: 54, a: 1},
        onstartup:        app.startup,
        bind: []interface{}{
            app,
        },
    })

    if err != nil {
        println("error:", err.error())
    }
}
登录后复制

我可以在终端中看到以下输出:

deb | [externalassethandler] loading 'http://localhost:3000/favicon.ico'
deb | [externalassethandler] loading 'http://localhost:3000/favicon.ico' failed, using assethandler

# print statement at the top of the servehttp function, 
# prints the requested path, and the available router paths.
path:  /favicon.ico
get /about ->

deb | [externalassethandler] loading 'http://localhost:3000/about'


to develop in the browser and call your bound go methods from javascript, navigate to: http://localhost:34115
deb | [externalassethandler] loading 'http://localhost:3000/@vite/client'
deb | [externalassethandler] loading 'http://localhost:3000/node_modules/vite/dist/client/env.mjs'
deb | [externalassethandler] loading 'http://localhost:3000/about'
deb | [externalassethandler] loading 'http://localhost:3000/@vite/client'
deb | [externalassethandler] loading 'http://localhost:3000/node_modules/vite/dist/client/env.mjs'
deb | [externalassethandler] loading 'http://localhost:3000/about'
登录后复制

当尝试获取有关页面进行测试时,如 wails assetserver 文档中所述,我检索索引页面:

let resp = await fetch("/about")
undefined
await resp.text()
'<!DOCTYPE html>\n<html lang="en">\n<head>\n  \x3Cscript type="module" src="/@vite/client">\x3C/script>\n\n    <meta charset="UTF-8"/>\n    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>\n    <meta name="wails-options" content="noautoinject" />\n    \x3Cscript src="/wails/ipc.js">\x3C/script>\n    \x3Cscript src="/wails/runtime.js">\x3C/script>\n    <title>new</title>\n</head>\n<body>\n    <h1>Index!</h1>\n    <a href="/about">Go To About!</a>\n</body>\n</html>\n'
登录后复制

为什么没有调用servehttp函数?

解决方法

日志显示使用了 externalassethandler。这意味着使用外部前端开发服务器。所有资源请求都会首先转发到外部前端开发服务器。仅当外部前端开发服务器响应 404405 状态代码时,才会使用 assetserver.options 中指定的处理程序。如今,大多数 spa 前端开发服务器都为非资产请求提供 index.html 服务。这就是您看到这种行为的原因。

解决方法是配置外部前端开发服务器以绕过该请求。对于vite,修改配置文件添加如下内容:

export default defineConfig({
  server: {
    proxy: {
      '/about': {
        bypass: function () {
          // Return false to produce a 404 error for the request.
          return false;
        },
      },
      // or for all the requests that start with "/api/"
      '/api/': {
        bypass: function () {
          return false;
        },
      },
    },
  },
});
登录后复制

但我建议不要这样做。因为资产处理程序,顾名思义,是为动态资产提供服务的。 wails 有自己的调用绑定 go 方法的方式。你应该尝试一下。

参考文献:

以上是使用自定义路由器使用 wails 资产处理程序时出现问题的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!