登录
首页 >  Golang >  Go问答

遇到自定义路由器与 wails 资产处理程序的集成问题

来源:stackoverflow

时间:2024-03-17 22:51:31 133浏览 收藏

在 Wails 应用程序中集成自定义路由器和 Wails 资产处理程序时,可能会遇到问题。默认情况下,Wails 使用外部前端开发服务器来处理资源请求,而资产处理程序仅在外部服务器返回 404 或 405 状态代码时才会被调用。这会导致在尝试获取自定义路由时总是返回 index.html 页面。

问题内容

我正在尝试将自定义多路复用器与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()
'\n\n\n  \x3Cscript type="module" src="/@vite/client">\x3C/script>\n\n    \n    \n    \n    \x3Cscript src="/wails/ipc.js">\x3C/script>\n    \x3Cscript src="/wails/runtime.js">\x3C/script>\n    new\n\n\n    

Index!

\n Go To About!\n\n\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 资产处理程序的集成问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>